1 11 package org.eclipse.jdt.internal.corext.refactoring.structure; 12 13 import java.util.HashSet ; 14 import java.util.Set ; 15 16 import org.eclipse.jdt.core.dom.AST; 17 import org.eclipse.jdt.core.dom.ASTNode; 18 import org.eclipse.jdt.core.dom.ASTVisitor; 19 import org.eclipse.jdt.core.dom.Expression; 20 import org.eclipse.jdt.core.dom.FieldAccess; 21 import org.eclipse.jdt.core.dom.IBinding; 22 import org.eclipse.jdt.core.dom.IPackageBinding; 23 import org.eclipse.jdt.core.dom.ITypeBinding; 24 import org.eclipse.jdt.core.dom.MemberRef; 25 import org.eclipse.jdt.core.dom.MethodInvocation; 26 import org.eclipse.jdt.core.dom.MethodRef; 27 import org.eclipse.jdt.core.dom.Name; 28 import org.eclipse.jdt.core.dom.QualifiedName; 29 import org.eclipse.jdt.core.dom.SimpleName; 30 import org.eclipse.jdt.core.dom.Type; 31 32 import org.eclipse.jdt.internal.corext.dom.ASTFlattener; 33 import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory; 34 import org.eclipse.jdt.internal.corext.dom.ASTNodes; 35 import org.eclipse.jdt.internal.corext.dom.Bindings; 36 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 37 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext; 38 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 39 40 class MoveStaticMemberAnalyzer extends ASTVisitor { 41 42 protected RefactoringStatus fStatus; 43 44 protected ITypeBinding fSource; 45 protected ITypeBinding fTarget; 46 protected CompilationUnitRewrite fCuRewrite; 47 protected IBinding[] fMembers; 48 49 protected boolean fNeedsImport; 50 51 protected Set fProcessed; 52 53 protected static final String REFERENCE_UPDATE= RefactoringCoreMessages.MoveMembersRefactoring_referenceUpdate; 54 55 public MoveStaticMemberAnalyzer(CompilationUnitRewrite cuRewrite, IBinding[] members, ITypeBinding source, ITypeBinding target) { 56 super(true); 57 fStatus= new RefactoringStatus(); 58 fCuRewrite= cuRewrite; 59 fMembers= members; 60 fSource= source; 61 fTarget= target; 62 fProcessed= new HashSet (); 63 } 64 65 public RefactoringStatus getStatus() { 66 return fStatus; 67 } 68 69 protected boolean isProcessed(ASTNode node) { 70 return fProcessed.contains(node); 71 } 72 73 protected void rewrite(SimpleName node, ITypeBinding type) { 74 AST ast= node.getAST(); 75 Type result= fCuRewrite.getImportRewrite().addImport(type, fCuRewrite.getAST()); 76 fCuRewrite.getImportRemover().registerAddedImport(type.getQualifiedName()); 77 Name dummy= ASTNodeFactory.newName(fCuRewrite.getAST(), ASTFlattener.asString(result)); 78 QualifiedName name= ast.newQualifiedName(dummy, ast.newSimpleName(node.getIdentifier())); 79 fCuRewrite.getASTRewrite().replace(node, name, fCuRewrite.createGroupDescription(REFERENCE_UPDATE)); 80 fCuRewrite.getImportRemover().registerRemovedNode(node); 81 fProcessed.add(node); 82 fNeedsImport= true; 83 } 84 85 protected void rewrite(QualifiedName node, ITypeBinding type) { 86 rewriteName(node.getQualifier(), type); 87 fProcessed.add(node.getName()); 88 } 89 90 protected void rewrite(FieldAccess node, ITypeBinding type) { 91 Expression exp= node.getExpression(); 92 if (exp == null) { 93 Type result= fCuRewrite.getImportRewrite().addImport(type, fCuRewrite.getAST()); 94 fCuRewrite.getImportRemover().registerAddedImport(type.getQualifiedName()); 95 exp= ASTNodeFactory.newName(fCuRewrite.getAST(), ASTFlattener.asString(result)); 96 fCuRewrite.getASTRewrite().set(node, FieldAccess.EXPRESSION_PROPERTY, exp, fCuRewrite.createGroupDescription(REFERENCE_UPDATE)); 97 fNeedsImport= true; 98 } else if (exp instanceof Name) { 99 rewriteName((Name)exp, type); 100 } else { 101 rewriteExpression(node, exp, type); 102 } 103 fProcessed.add(node.getName()); 104 } 105 106 protected void rewrite(MethodInvocation node, ITypeBinding type) { 107 Expression exp= node.getExpression(); 108 if (exp == null) { 109 Type result= fCuRewrite.getImportRewrite().addImport(type, fCuRewrite.getAST()); 110 fCuRewrite.getImportRemover().registerAddedImport(type.getQualifiedName()); 111 exp= ASTNodeFactory.newName(fCuRewrite.getAST(), ASTFlattener.asString(result)); 112 fCuRewrite.getASTRewrite().set(node, MethodInvocation.EXPRESSION_PROPERTY, exp, fCuRewrite.createGroupDescription(REFERENCE_UPDATE)); 113 fNeedsImport= true; 114 } else if (exp instanceof Name) { 115 rewriteName((Name)exp, type); 116 } else { 117 rewriteExpression(node, exp, type); 118 } 119 fProcessed.add(node.getName()); 120 } 121 122 protected void rewrite(MemberRef node, ITypeBinding type) { 123 Name qualifier= node.getQualifier(); 124 if (qualifier == null) { 125 Type result= fCuRewrite.getImportRewrite().addImport(type, fCuRewrite.getAST()); 126 fCuRewrite.getImportRemover().registerAddedImport(type.getQualifiedName()); 127 qualifier= ASTNodeFactory.newName(fCuRewrite.getAST(), ASTFlattener.asString(result)); 128 fCuRewrite.getASTRewrite().set(node, MemberRef.QUALIFIER_PROPERTY, qualifier, fCuRewrite.createGroupDescription(REFERENCE_UPDATE)); 129 fNeedsImport= true; 130 } else { 131 rewriteName(qualifier, type); 132 } 133 fProcessed.add(node.getName()); 134 } 135 136 protected void rewrite(MethodRef node, ITypeBinding type) { 137 Name qualifier= node.getQualifier(); 138 if (qualifier == null) { 139 Type result= fCuRewrite.getImportRewrite().addImport(type, fCuRewrite.getAST()); 140 fCuRewrite.getImportRemover().registerAddedImport(type.getQualifiedName()); 141 qualifier= ASTNodeFactory.newName(fCuRewrite.getAST(), ASTFlattener.asString(result)); 142 fCuRewrite.getASTRewrite().set(node, MethodRef.QUALIFIER_PROPERTY, qualifier, fCuRewrite.createGroupDescription(REFERENCE_UPDATE)); 143 fNeedsImport= true; 144 } else { 145 rewriteName(qualifier, type); 146 } 147 fProcessed.add(node.getName()); 148 } 149 150 private void rewriteName(Name name, ITypeBinding type) { 151 AST creator= name.getAST(); 152 boolean fullyQualified= false; 153 if (name instanceof QualifiedName) { 154 SimpleName left= ASTNodes.getLeftMostSimpleName(name); 155 if (left.resolveBinding() instanceof IPackageBinding) 156 fullyQualified= true; 157 } 158 if (fullyQualified) { 159 fCuRewrite.getASTRewrite().replace( 160 name, 161 ASTNodeFactory.newName(creator, type.getQualifiedName()), 162 fCuRewrite.createGroupDescription(REFERENCE_UPDATE)); 163 fCuRewrite.getImportRemover().registerRemovedNode(name); 164 } else { 165 Type result= fCuRewrite.getImportRewrite().addImport(type, fCuRewrite.getAST()); 166 fCuRewrite.getImportRemover().registerAddedImport(type.getQualifiedName()); 167 Name n= ASTNodeFactory.newName(fCuRewrite.getAST(), ASTFlattener.asString(result)); 168 fCuRewrite.getASTRewrite().replace( 169 name, 170 n, 171 fCuRewrite.createGroupDescription(REFERENCE_UPDATE)); 172 fCuRewrite.getImportRemover().registerRemovedNode(name); 173 fNeedsImport= true; 174 } 175 } 176 177 private void rewriteExpression(ASTNode node, Expression exp, ITypeBinding type) { 178 fCuRewrite.getASTRewrite().replace(exp, fCuRewrite.getImportRewrite().addImport(type, fCuRewrite.getAST()), fCuRewrite.createGroupDescription(REFERENCE_UPDATE)); 179 fCuRewrite.getImportRemover().registerAddedImport(type.getQualifiedName()); 180 fCuRewrite.getImportRemover().registerRemovedNode(exp); 181 fNeedsImport= true; 182 nonStaticAccess(node); 183 } 184 185 protected void nonStaticAccess(ASTNode node) { 186 fStatus.addWarning(RefactoringCoreMessages.MoveStaticMemberAnalyzer_nonStatic, 187 JavaStatusContext.create(fCuRewrite.getCu(), node)); 188 } 189 190 protected boolean isStaticAccess(Expression exp, ITypeBinding type) { 191 if (!(exp instanceof Name)) 192 return false; 193 return Bindings.equals(type, ((Name)exp).resolveBinding()); 194 } 195 196 protected boolean isMovedMember(IBinding binding) { 197 if (binding == null) 198 return false; 199 for (int i= 0; i < fMembers.length; i++) { 200 if (Bindings.equals(fMembers[i], binding)) 201 return true; 202 } 203 return false; 204 } 205 } 206 | Popular Tags |