下面列出了怎么用org.eclipse.jdt.core.dom.AbstractTypeDeclaration的API类实例代码及写法,或者点击链接到github查看源代码。
private Boolean processExtractSuperType(final AbstractTypeDeclaration type){
List<SDRefactoring> listRenames = new ArrayList<SDRefactoring>();
if(this.refactorings.containsKey(RefactoringType.EXTRACT_SUPERCLASS)){
listRenames.addAll(this.refactorings.get(RefactoringType.EXTRACT_SUPERCLASS));
}
if(this.refactorings.containsKey(RefactoringType.EXTRACT_INTERFACE)){
listRenames.addAll(this.refactorings.get(RefactoringType.EXTRACT_INTERFACE));
}
if(listRenames != null){
for(SDRefactoring ref : listRenames){
if(UtilTools.getPath(type).equals(ref.getEntityBefore().fullName())){
this.typesWithPathChanged.add(ref.getEntityAfter().fullName());
this.addChange(type, Category.TYPE_EXTRACT_SUPERTYPE, false, ""); //TODO: create description
return true;
}
}
}
return false;
}
@Nullable
private String getFullyQualifiedTypeName(AbstractTypeDeclaration node) {
LinkedList<String> nameParts = new LinkedList<>();
nameParts.add(node.getName().toString());
ASTNode parent = node.getParent();
while (!(parent instanceof CompilationUnit)) {
if (parent instanceof AbstractTypeDeclaration) {
nameParts.addFirst(((AbstractTypeDeclaration) parent).getName().toString());
parent = parent.getParent();
} else if (parent instanceof AnonymousClassDeclaration) {
// If this is defined in an anonymous class, then there is no meaningful fully qualified
// name.
return null;
} else {
throw new RuntimeException("Unexpected parent " + parent + " for " + node);
}
}
// A Java file might not have a package. Hopefully all of ours do though...
PackageDeclaration packageDecl = ((CompilationUnit) parent).getPackage();
if (packageDecl != null) {
nameParts.addFirst(packageDecl.getName().toString());
}
return Joiner.on(".").join(nameParts);
}
private void addEnclosingInstanceTypeParameters(final ITypeBinding[] parameters, final AbstractTypeDeclaration declaration, final ASTRewrite rewrite) {
Assert.isNotNull(parameters);
Assert.isNotNull(declaration);
Assert.isNotNull(rewrite);
if (declaration instanceof TypeDeclaration) {
final TypeDeclaration type= (TypeDeclaration) declaration;
final List<TypeParameter> existing= type.typeParameters();
final Set<String> names= new HashSet<String>();
TypeParameter parameter= null;
for (final Iterator<TypeParameter> iterator= existing.iterator(); iterator.hasNext();) {
parameter= iterator.next();
names.add(parameter.getName().getIdentifier());
}
final ListRewrite rewriter= rewrite.getListRewrite(type, TypeDeclaration.TYPE_PARAMETERS_PROPERTY);
String name= null;
for (int index= 0; index < parameters.length; index++) {
name= parameters[index].getName();
if (!names.contains(name)) {
parameter= type.getAST().newTypeParameter();
parameter.setName(type.getAST().newSimpleName(name));
rewriter.insertLast(parameter, null);
}
}
}
}
private static AbstractTypeDeclaration getSelectedTypeDeclaration(ICompilationUnit unit, CodeActionParams params) {
int start = DiagnosticsHelper.getStartOffset(unit, params.getRange());
int end = DiagnosticsHelper.getEndOffset(unit, params.getRange());
InnovationContext context = new InnovationContext(unit, start, end - start);
context.setASTRoot(CodeActionHandler.getASTRoot(unit));
ASTNode node = context.getCoveredNode();
if (node == null) {
node = context.getCoveringNode();
}
while (node != null && !(node instanceof AbstractTypeDeclaration)) {
node = node.getParent();
}
return (AbstractTypeDeclaration) node;
}
private String getLabel(ASTNode node) {
if (node instanceof AbstractTypeDeclaration) {
return ((AbstractTypeDeclaration)node).getName().getIdentifier();
} else if (node instanceof AnonymousClassDeclaration) {
if (node.getLocationInParent() == ClassInstanceCreation.ANONYMOUS_CLASS_DECLARATION_PROPERTY) {
ClassInstanceCreation creation= (ClassInstanceCreation)node.getParent();
return Messages.format(
RefactoringMessages.ExtractMethodInputPage_anonymous_type_label,
BasicElementLabels.getJavaElementName(ASTNodes.asString(creation.getType())));
} else if (node.getLocationInParent() == EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY) {
EnumConstantDeclaration decl= (EnumConstantDeclaration)node.getParent();
return decl.getName().getIdentifier();
}
}
return "UNKNOWN"; //$NON-NLS-1$
}
private void addMethodStubForAbstractMethod(final IMethod sourceMethod, final CompilationUnit declaringCuNode, final AbstractTypeDeclaration typeToCreateStubIn, final ICompilationUnit newCu, final CompilationUnitRewrite rewriter, final Map<IMember, IncomingMemberVisibilityAdjustment> adjustments, final IProgressMonitor monitor, final RefactoringStatus status) throws CoreException {
final MethodDeclaration methodToCreateStubFor= ASTNodeSearchUtil.getMethodDeclarationNode(sourceMethod, declaringCuNode);
final AST ast= rewriter.getRoot().getAST();
final MethodDeclaration newMethod= ast.newMethodDeclaration();
newMethod.setBody(createMethodStub(methodToCreateStubFor, ast));
newMethod.setConstructor(false);
copyExtraDimensions(methodToCreateStubFor, newMethod);
newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiersWithUpdatedVisibility(sourceMethod, JdtFlags.clearFlag(Modifier.NATIVE | Modifier.ABSTRACT, methodToCreateStubFor.getModifiers()), adjustments, new SubProgressMonitor(monitor, 1), false, status)));
newMethod.setName(((SimpleName) ASTNode.copySubtree(ast, methodToCreateStubFor.getName())));
final TypeVariableMaplet[] mapping= TypeVariableUtil.composeMappings(TypeVariableUtil.subTypeToSuperType(getDeclaringType(), getDestinationType()), TypeVariableUtil.superTypeToInheritedType(getDestinationType(), ((IType) typeToCreateStubIn.resolveBinding().getJavaElement())));
copyReturnType(rewriter.getASTRewrite(), getDeclaringType().getCompilationUnit(), methodToCreateStubFor, newMethod, mapping);
copyParameters(rewriter.getASTRewrite(), getDeclaringType().getCompilationUnit(), methodToCreateStubFor, newMethod, mapping);
copyThrownExceptions(methodToCreateStubFor, newMethod);
newMethod.setJavadoc(createJavadocForStub(typeToCreateStubIn.getName().getIdentifier(), methodToCreateStubFor, newMethod, newCu, rewriter.getASTRewrite()));
ImportRewriteContext context= new ContextSensitiveImportRewriteContext(typeToCreateStubIn, rewriter.getImportRewrite());
ImportRewriteUtil.addImports(rewriter, context, newMethod, new HashMap<Name, String>(), new HashMap<Name, String>(), false);
rewriter.getASTRewrite().getListRewrite(typeToCreateStubIn, typeToCreateStubIn.getBodyDeclarationsProperty()).insertAt(newMethod, ASTNodes.getInsertionIndex(newMethod, typeToCreateStubIn.bodyDeclarations()), rewriter.createCategorizedGroupDescription(RefactoringCoreMessages.PullUpRefactoring_add_method_stub, SET_PULL_UP));
}
/**
* Returns the declaration node for the originally selected node.
* @param name the name of the node
*
* @return the declaration node
*/
private static ASTNode getDeclarationNode(SimpleName name) {
ASTNode parent= name.getParent();
if (!(parent instanceof AbstractTypeDeclaration)) {
parent= parent.getParent();
if (parent instanceof ParameterizedType || parent instanceof Type)
parent= parent.getParent();
if (parent instanceof ClassInstanceCreation) {
final ClassInstanceCreation creation= (ClassInstanceCreation) parent;
parent= creation.getAnonymousClassDeclaration();
}
}
return parent;
}
@Override public void translateFile(SourceFile sourceFile) {
CompilationUnit compilationUnit = sourceFile.getCompilationUnit();
AbstractTypeDeclaration mainTypeDeclaration = (AbstractTypeDeclaration) compilationUnit.types().get(0);
String typeName = mainTypeDeclaration.getName().getIdentifier();
String fileName = typeName + ".cs";
File file = new File(getPackageDirectory(mainTypeDeclaration), fileName);
try (FileWriter writer = new FileWriter(file)) {
CSharpSourceFileWriter cSharpSourceFileWriter = new CSharpSourceFileWriter(this, sourceFile, writer);
cSharpSourceFileWriter.writeRootNode(compilationUnit);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String getCatchBodyContent(ICompilationUnit cu, String exceptionType, String variableName, ASTNode locationInAST, String lineDelimiter) throws CoreException {
String enclosingType= ""; //$NON-NLS-1$
String enclosingMethod= ""; //$NON-NLS-1$
if (locationInAST != null) {
MethodDeclaration parentMethod= ASTResolving.findParentMethodDeclaration(locationInAST);
if (parentMethod != null) {
enclosingMethod= parentMethod.getName().getIdentifier();
locationInAST= parentMethod;
}
ASTNode parentType= ASTResolving.findParentType(locationInAST);
if (parentType instanceof AbstractTypeDeclaration) {
enclosingType= ((AbstractTypeDeclaration)parentType).getName().getIdentifier();
}
}
return getCatchBodyContent(cu, exceptionType, variableName, enclosingType, enclosingMethod, lineDelimiter);
}
public static ASTNode getSelectedTypeNode(CompilationUnit root, IProblemLocation problem) {
ASTNode selectedNode= problem.getCoveringNode(root);
if (selectedNode == null)
return null;
if (selectedNode.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION) { // bug 200016
selectedNode= selectedNode.getParent();
}
if (selectedNode.getLocationInParent() == EnumConstantDeclaration.NAME_PROPERTY) {
selectedNode= selectedNode.getParent();
}
if (selectedNode.getNodeType() == ASTNode.SIMPLE_NAME && selectedNode.getParent() instanceof AbstractTypeDeclaration) {
return selectedNode.getParent();
} else if (selectedNode.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
return ((ClassInstanceCreation) selectedNode).getAnonymousClassDeclaration();
} else if (selectedNode.getNodeType() == ASTNode.ENUM_CONSTANT_DECLARATION) {
EnumConstantDeclaration enumConst= (EnumConstantDeclaration) selectedNode;
if (enumConst.getAnonymousClassDeclaration() != null)
return enumConst.getAnonymousClassDeclaration();
return enumConst;
} else {
return null;
}
}
public boolean resolveInClassInitializer() {
if (fInClassInitializerRequested)
return fInClassInitializer;
fInClassInitializerRequested= true;
resolveSelectedNodes();
ASTNode node= getStartNode();
if (node == null) {
fInClassInitializer= true;
} else {
while (node != null) {
int nodeType= node.getNodeType();
if (node instanceof AbstractTypeDeclaration) {
fInClassInitializer= false;
break;
} else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
fInClassInitializer= false;
break;
} else if (nodeType == ASTNode.INITIALIZER) {
fInClassInitializer= true;
break;
}
node= node.getParent();
}
}
return fInClassInitializer;
}
public static IType getSelectionType(IInvocationContext context) {
ICompilationUnit unit = context.getCompilationUnit();
ASTNode node = context.getCoveredNode();
if (node == null) {
node = context.getCoveringNode();
}
ITypeBinding typeBinding = null;
while (node != null && !(node instanceof CompilationUnit)) {
if (node instanceof AbstractTypeDeclaration) {
typeBinding = ((AbstractTypeDeclaration) node).resolveBinding();
break;
} else if (node instanceof AnonymousClassDeclaration) { // Anonymous
typeBinding = ((AnonymousClassDeclaration) node).resolveBinding();
break;
}
node = node.getParent();
}
if (typeBinding != null && typeBinding.getJavaElement() instanceof IType) {
return (IType) typeBinding.getJavaElement();
}
return unit.findPrimaryType();
}
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot= fContext.getASTRoot();
AST ast= astRoot.getAST();
ASTRewrite rewrite= ASTRewrite.create(ast);
AbstractTypeDeclaration decl= findTypeDeclaration(astRoot.types(), fOldName);
if (decl != null) {
ASTNode[] sameNodes= LinkedNodeFinder.findByNode(astRoot, decl.getName());
for (int i= 0; i < sameNodes.length; i++) {
rewrite.replace(sameNodes[i], ast.newSimpleName(fNewName), null);
}
}
return rewrite;
}
protected final void adjustTypeVisibility(final ITypeBinding binding) throws JavaModelException {
Assert.isNotNull(binding);
final IJavaElement element= binding.getJavaElement();
if (element instanceof IType) {
final IType type= (IType) element;
if (!type.isBinary() && !type.isReadOnly() && !Flags.isPublic(type.getFlags())) {
boolean same= false;
final CompilationUnitRewrite rewrite= getCompilationUnitRewrite(fRewrites, type.getCompilationUnit());
final AbstractTypeDeclaration declaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(type, rewrite.getRoot());
if (declaration != null) {
final ITypeBinding declaring= declaration.resolveBinding();
if (declaring != null && Bindings.equals(declaring.getPackage(), fTarget.getType().getPackage()))
same= true;
final Modifier.ModifierKeyword keyword= same ? null : Modifier.ModifierKeyword.PUBLIC_KEYWORD;
final String modifier= same ? RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_default : RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_public;
if (MemberVisibilityAdjustor.hasLowerVisibility(binding.getModifiers(), same ? Modifier.NONE : keyword == null ? Modifier.NONE : keyword.toFlagValue()) && MemberVisibilityAdjustor.needsVisibilityAdjustments(type, keyword, fAdjustments))
fAdjustments.put(type, new MemberVisibilityAdjustor.OutgoingMemberVisibilityAdjustment(type, keyword, RefactoringStatus.createWarningStatus(Messages.format(RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_type_warning, new String[] { BindingLabelProvider.getBindingLabel(declaration.resolveBinding(), JavaElementLabels.ALL_FULLY_QUALIFIED), modifier }), JavaStatusContext.create(type.getCompilationUnit(), declaration))));
}
}
}
}
/**
* Creates a new add unimplemented constructors operation.
*
* @param astRoot the compilation unit AST node
* @param type the type to add the methods to
* @param constructorsToImplement the method binding keys to implement
* @param insertPos the insertion point, or <code>-1</code>
* @param imports <code>true</code> if the import edits should be applied, <code>false</code> otherwise
* @param apply <code>true</code> if the resulting edit should be applied, <code>false</code> otherwise
* @param save <code>true</code> if the changed compilation unit should be saved, <code>false</code> otherwise
*/
public AddUnimplementedConstructorsOperation(CompilationUnit astRoot, ITypeBinding type, IMethodBinding[] constructorsToImplement, int insertPos, final boolean imports, final boolean apply, final boolean save) {
if (astRoot == null || !(astRoot.getJavaElement() instanceof ICompilationUnit)) {
throw new IllegalArgumentException("AST must not be null and has to be created from a ICompilationUnit"); //$NON-NLS-1$
}
if (type == null) {
throw new IllegalArgumentException("The type must not be null"); //$NON-NLS-1$
}
ASTNode node= astRoot.findDeclaringNode(type);
if (!(node instanceof AnonymousClassDeclaration || node instanceof AbstractTypeDeclaration)) {
throw new IllegalArgumentException("type has to map to a type declaration in the AST"); //$NON-NLS-1$
}
fType= type;
fInsertPos= insertPos;
fASTRoot= astRoot;
fConstructorsToImplement= constructorsToImplement;
fSave= save;
fApply= apply;
fImports= imports;
fCreateComments= StubUtility.doAddComments(astRoot.getJavaElement().getJavaProject());
fVisibility= Modifier.PUBLIC;
fOmitSuper= false;
}
@Override
public String translateNode(SourceFile sourceFile, ASTNode astNode) {
try (StringWriter writer = new StringWriter()) {
CPlusPlusSourceFileWriter cPlusPlusSourceFileWriter = new CPlusPlusSourceFileWriter(this, sourceFile, writer,
OutputType.SOURCE);
// Set the type declaration part of the context
AbstractTypeDeclaration typeDeclaration = (AbstractTypeDeclaration) sourceFile.getCompilationUnit().types().get(0);
cPlusPlusSourceFileWriter.getContext().setTypeDeclaration(typeDeclaration);
cPlusPlusSourceFileWriter.writeRootNode(astNode);
return writer.getBuffer().toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void visitType(AbstractTypeDeclaration node) {
SimpleName name = node.getName();
int openBraceIndex = (name == null ? this.tokenManager.firstIndexIn(node, TerminalTokens.TokenNameLBRACE)
: this.tokenManager.firstIndexAfter(name, TerminalTokens.TokenNameLBRACE));
Token openBraceToken = this.tokenManager.get(openBraceIndex);
openBraceToken.clearLineBreaksAfter();
openBraceToken.putLineBreaksAfter(2);
int closeBraceIndex = this.tokenManager.lastIndexIn(node, TerminalTokens.TokenNameRBRACE);
Token closeBraceToken = this.tokenManager.get(closeBraceIndex);
closeBraceToken.clearLineBreaksBefore();
closeBraceToken.putLineBreaksBefore(2);
}
@Override
public final void endVisit(final SimpleName node) {
final ASTNode parent= node.getParent();
if (!(parent instanceof ImportDeclaration) && !(parent instanceof PackageDeclaration) && !(parent instanceof AbstractTypeDeclaration)) {
final IBinding binding= node.resolveBinding();
if (binding instanceof IVariableBinding && !(parent instanceof MethodDeclaration))
endVisit((IVariableBinding) binding, null, node);
else if (binding instanceof ITypeBinding && parent instanceof MethodDeclaration)
endVisit((ITypeBinding) binding, node);
}
}
/**
* @param method
* @param type
* @return true, method is deprecated or type is deprecated
*/
private Boolean isDeprecated(MethodDeclaration method, AbstractTypeDeclaration type){
Boolean isMethodDeprecated = (method != null && method.resolveBinding() != null && method.resolveBinding().isDeprecated()) ? true: false;
Boolean isTypeDeprecated = (type != null && type.resolveBinding() != null && type.resolveBinding().isDeprecated()) ? true: false;
return isMethodDeprecated || isTypeDeprecated;
}
private boolean classNameHidesEnclosingType() {
ITypeBinding type= ((AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class)).resolveBinding();
while (type != null) {
if (fClassName.equals(type.getName()))
return true;
type= type.getDeclaringClass();
}
return false;
}
private Boolean processRenameType(final AbstractTypeDeclaration type){
List<SDRefactoring> listRenames = this.refactorings.get(RefactoringType.RENAME_CLASS);
if(listRenames != null){
for(SDRefactoring ref : listRenames){
if(UtilTools.getPath(type).equals(ref.getEntityBefore().fullName())){
String description = this.description.rename(ref.getEntityBefore().fullName(), ref.getEntityAfter().fullName());
this.typesWithPathChanged.add(ref.getEntityAfter().fullName());
this.addChange(type, Category.TYPE_RENAME, true, description);
return true;
}
}
}
return false;
}
private boolean needsImport(ITypeBinding typeBinding, SimpleName ref) {
if (!typeBinding.isTopLevel() && !typeBinding.isMember() || typeBinding.isRecovered()) {
return false; // no imports for anonymous, local, primitive types or parameters types
}
int modifiers= typeBinding.getModifiers();
if (Modifier.isPrivate(modifiers)) {
return false; // imports for privates are not required
}
ITypeBinding currTypeBinding= Bindings.getBindingOfParentType(ref);
if (currTypeBinding == null) {
if (ASTNodes.getParent(ref, ASTNode.PACKAGE_DECLARATION) != null) {
return true; // reference in package-info.java
}
return false; // not in a type
}
if (!Modifier.isPublic(modifiers)) {
if (!currTypeBinding.getPackage().getName().equals(typeBinding.getPackage().getName())) {
return false; // not visible
}
}
ASTNode parent= ref.getParent();
while (parent instanceof Type) {
parent= parent.getParent();
}
if (parent instanceof AbstractTypeDeclaration && parent.getParent() instanceof CompilationUnit) {
return true;
}
if (typeBinding.isMember()) {
if (fAnalyzer.isDeclaredInScope(typeBinding, ref, ScopeAnalyzer.TYPES | ScopeAnalyzer.CHECK_VISIBILITY))
return false;
}
return true;
}
@Override
public boolean visit(final CompilationUnit it) {
boolean _isDummyType = this._aSTFlattenerUtils.isDummyType(IterableExtensions.<AbstractTypeDeclaration>head(it.types()));
boolean _not = (!_isDummyType);
if (_not) {
PackageDeclaration _package = it.getPackage();
if (_package!=null) {
_package.accept(this);
}
this.visitAll(it.imports());
}
this.visitAll(it.types());
return false;
}
/**
* Creates a new type signature of a subtype.
*
* @param subRewrite
* the compilation unit rewrite of a subtype
* @param declaration
* the type declaration of a subtype
* @param extractedType
* the extracted super type
* @param extractedBinding
* the binding of the extracted super type
* @param monitor
* the progress monitor to use
* @throws JavaModelException
* if the type parameters cannot be retrieved
*/
protected final void createTypeSignature(final CompilationUnitRewrite subRewrite, final AbstractTypeDeclaration declaration, final IType extractedType, final ITypeBinding extractedBinding, final IProgressMonitor monitor) throws JavaModelException {
Assert.isNotNull(subRewrite);
Assert.isNotNull(declaration);
Assert.isNotNull(extractedType);
Assert.isNotNull(monitor);
try {
monitor.beginTask(RefactoringCoreMessages.ExtractSupertypeProcessor_preparing, 10);
final AST ast= subRewrite.getAST();
Type type= null;
if (extractedBinding != null) {
type= subRewrite.getImportRewrite().addImport(extractedBinding, ast);
} else {
subRewrite.getImportRewrite().addImport(extractedType.getFullyQualifiedName('.'));
type= ast.newSimpleType(ast.newSimpleName(extractedType.getElementName()));
}
subRewrite.getImportRemover().registerAddedImport(extractedType.getFullyQualifiedName('.'));
if (type != null) {
final ITypeParameter[] parameters= extractedType.getTypeParameters();
if (parameters.length > 0) {
final ParameterizedType parameterized= ast.newParameterizedType(type);
for (int index= 0; index < parameters.length; index++)
parameterized.typeArguments().add(ast.newSimpleType(ast.newSimpleName(parameters[index].getElementName())));
type= parameterized;
}
}
final ASTRewrite rewriter= subRewrite.getASTRewrite();
if (type != null && declaration instanceof TypeDeclaration) {
final TypeDeclaration extended= (TypeDeclaration) declaration;
final Type superClass= extended.getSuperclassType();
if (superClass != null) {
rewriter.replace(superClass, type, subRewrite.createCategorizedGroupDescription(RefactoringCoreMessages.ExtractSupertypeProcessor_add_supertype, SET_EXTRACT_SUPERTYPE));
subRewrite.getImportRemover().registerRemovedNode(superClass);
} else
rewriter.set(extended, TypeDeclaration.SUPERCLASS_TYPE_PROPERTY, type, subRewrite.createCategorizedGroupDescription(RefactoringCoreMessages.ExtractSupertypeProcessor_add_supertype, SET_EXTRACT_SUPERTYPE));
}
} finally {
monitor.done();
}
}
private void findChangedSuperTypes() {
for(AbstractTypeDeclaration accessibleTypeVersion1 : version1.getTypesPublicAndProtected()){
AbstractTypeDeclaration accessibleTypeVersion2 = version2.getVersionAccessibleType(accessibleTypeVersion1);
if(accessibleTypeVersion2 != null){
String super1 = this.getNameSuperClass(accessibleTypeVersion1);
String super2 = this.getNameSuperClass(accessibleTypeVersion2);
if(super1 != null && super2 != null){
Boolean isBreakingChange = true;
String nameClassComplete = UtilTools.getPath(accessibleTypeVersion2);
String description = "";
if(this.containsSuperClass(accessibleTypeVersion1) && !this.containsSuperClass(accessibleTypeVersion2)){
description = this.description.changeSuperType(nameClassComplete, super1, "");
this.addChange(accessibleTypeVersion2, Category.TYPE_REMOVE_SUPERCLASS, isBreakingChange, description);
}
if(!this.containsSuperClass(accessibleTypeVersion1) && this.containsSuperClass(accessibleTypeVersion2)){
description = this.description.changeSuperType(nameClassComplete, "", super2);
this.addChange(accessibleTypeVersion2, Category.TYPE_ADD_SUPER_CLASS, false, description);
}
if(this.containsSuperClass(accessibleTypeVersion1) && this.containsSuperClass(accessibleTypeVersion2) && !super1.equals(super2)){
description = this.description.changeSuperType(nameClassComplete, super1, super2);
this.addChange(accessibleTypeVersion2, Category.TYPE_CHANGE_SUPERCLASS, false, description);
}
}
}
}
}
/**
* Busca classes que foram depreciadas [CATEGORY_TYPE_DEPRECIATED].
* @param version1
* @param version2
*/
private void findAddTypeDeprecate() {
for(AbstractTypeDeclaration accessibleTypeVersion1 : version1.getTypesPublicAndProtected()){
AbstractTypeDeclaration accessibleTypeVersion2 = version2.getVersionAccessibleType(accessibleTypeVersion1);
if(accessibleTypeVersion2 != null){
if(!this.isDeprecated(accessibleTypeVersion1) && this.isDeprecated(accessibleTypeVersion2)){
String description = this.description.deprecate(UtilTools.getPath(accessibleTypeVersion2));
this.addChange(accessibleTypeVersion1, Category.TYPE_DEPRECATED, false, description);
}
}
}
}
private AbstractTypeDeclaration findTypeDeclaration(List<AbstractTypeDeclaration> types, String name) {
for (Iterator<AbstractTypeDeclaration> iter= types.iterator(); iter.hasNext();) {
AbstractTypeDeclaration decl= iter.next();
if (name.equals(decl.getName().getIdentifier())) {
return decl;
}
}
return null;
}
private static ITypeBinding getTypeBindingForTypeDeclaration(ASTNode declaration) {
if (declaration instanceof AnonymousClassDeclaration)
return ((AnonymousClassDeclaration) declaration).resolveBinding();
if (declaration instanceof AbstractTypeDeclaration)
return ((AbstractTypeDeclaration) declaration).resolveBinding();
Assert.isTrue(false);
return null;
}
@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
ASTParser p= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
p.setSource(getDestinationCu());
CompilationUnit cuNode= (CompilationUnit) p.createAST(pm);
ASTRewrite rewrite= ASTRewrite.create(cuNode.getAST());
TypedSource source= null;
for (int i= fSources.length - 1; i >= 0; i--) {
source= fSources[i];
final ASTNode destination= getDestinationNodeForSourceElement(fDestination, source.getType(), cuNode);
if (destination != null) {
if (destination instanceof CompilationUnit)
insertToCu(rewrite, createNewNodeToInsertToCu(source, rewrite), (CompilationUnit) destination);
else if (destination instanceof AbstractTypeDeclaration)
insertToType(rewrite, createNewNodeToInsertToType(source, rewrite), (AbstractTypeDeclaration) destination);
}
}
final CompilationUnitChange result= new CompilationUnitChange(ReorgMessages.PasteAction_change_name, getDestinationCu());
try {
ITextFileBuffer buffer= RefactoringFileBuffers.acquire(getDestinationCu());
TextEdit rootEdit= rewrite.rewriteAST(buffer.getDocument(), fDestination.getJavaProject().getOptions(true));
if (getDestinationCu().isWorkingCopy())
result.setSaveMode(TextFileChange.LEAVE_DIRTY);
TextChangeCompatibility.addTextEdit(result, ReorgMessages.PasteAction_edit_name, rootEdit);
} finally {
RefactoringFileBuffers.release(getDestinationCu());
}
return result;
}
private ASTNode getEnclosingTypeDeclaration(ASTNode node) {
while (node != null) {
if (node instanceof AbstractTypeDeclaration) {
return node;
} else if (node instanceof AnonymousClassDeclaration) {
return node;
}
node= node.getParent();
}
return null;
}