KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > structure > MovedMemberAnalyzer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.structure;
12
13 import org.eclipse.jdt.core.dom.FieldAccess;
14 import org.eclipse.jdt.core.dom.IBinding;
15 import org.eclipse.jdt.core.dom.IMethodBinding;
16 import org.eclipse.jdt.core.dom.ITypeBinding;
17 import org.eclipse.jdt.core.dom.IVariableBinding;
18 import org.eclipse.jdt.core.dom.MemberRef;
19 import org.eclipse.jdt.core.dom.MethodInvocation;
20 import org.eclipse.jdt.core.dom.MethodRef;
21 import org.eclipse.jdt.core.dom.Modifier;
22 import org.eclipse.jdt.core.dom.QualifiedName;
23 import org.eclipse.jdt.core.dom.SimpleName;
24
25 import org.eclipse.jdt.internal.corext.dom.Bindings;
26
27 /**
28  * Updates references in moved static members.
29  * Accepts <code>BodyDeclaration</code>s.
30  */

31 /* package */ class MovedMemberAnalyzer extends MoveStaticMemberAnalyzer {
32 /*
33  * cases:
34  * - access to moved member (or to member of moved member) -> do nothing.
35  * - (static) access to source -> change to source, import source.
36  * - (static) access to target -> change to target.
37  * - access to other type -> do nothing (import is done in MoveStaticMembersRefactoring#getUpdatedMemberSource())
38  */

39 // TW: Adapted references to type
40
// - Reference to type inside moved type:
41
// - if originally resolved by qualification -> no problem
42
// - if originally resolved by import -> must add import in target too (qualify if import ambiguous)
43

44     public MovedMemberAnalyzer(CompilationUnitRewrite cuRewrite,
45             IBinding[] members, ITypeBinding source, ITypeBinding target) {
46         super(cuRewrite, members, source, target);
47     }
48     
49     public boolean targetNeedsSourceImport() {
50         return fNeedsImport;
51     }
52     
53     //---- types and fields --------------------------
54

55     public boolean visit(SimpleName node) {
56         if (node.isDeclaration() || isProcessed(node))
57             return super.visit(node);
58         IBinding binding= node.resolveBinding();
59         if (isMovedMember(binding))
60             return super.visit(node);
61             
62         if (isSourceAccess(binding))
63             rewrite(node, fSource);
64         return super.visit(node);
65     }
66     
67     public boolean visit(QualifiedName node) {
68         IBinding binding= node.resolveBinding();
69         if (isSourceAccess(binding)) {
70             if (isMovedMember(binding)) {
71                 rewrite(node, fTarget);
72                 return false;
73             } else {
74                 rewrite(node, fSource);
75                 return false;
76             }
77         } else if (isTargetAccess(binding)) {
78             // remove qualifier:
79
SimpleName replace= (SimpleName)fCuRewrite.getASTRewrite().createCopyTarget(node.getName());
80             fCuRewrite.getASTRewrite().replace(node, replace, null);
81             fCuRewrite.getImportRemover().registerRemovedNode(node);
82             return false;
83         }
84         return super.visit(node);
85     }
86     
87     public boolean visit(FieldAccess node) {
88         IBinding binding= node.resolveFieldBinding();
89         if (isSourceAccess(binding)) {
90             if (isMovedMember(binding)) {
91                 if (node.getExpression() != null)
92                     rewrite(node, fTarget);
93             } else
94                 rewrite(node, fSource);
95             
96         } else if (isTargetAccess(binding)) {
97             fCuRewrite.getASTRewrite().remove(node.getExpression(), null);
98             fCuRewrite.getImportRemover().registerRemovedNode(node.getExpression());
99         }
100         return super.visit(node);
101     }
102     
103     //---- method invocations ----------------------------------
104

105     public boolean visit(MethodInvocation node) {
106         IBinding binding= node.resolveMethodBinding();
107         if (isSourceAccess(binding)) {
108             if (isMovedMember(binding)) {
109                 if (node.getExpression() != null)
110                     rewrite(node, fTarget);
111             } else
112                 rewrite(node, fSource);
113             
114         } else if (isTargetAccess(binding)) {
115             if (node.getExpression() != null) {
116                 fCuRewrite.getASTRewrite().remove(node.getExpression(), null);
117                 fCuRewrite.getImportRemover().registerRemovedNode(node.getExpression());
118             }
119         }
120         return super.visit(node);
121     }
122     
123     //---- javadoc references ----------------------------------
124

125     public boolean visit(MemberRef node) {
126         IBinding binding= node.resolveBinding();
127         if (isSourceAccess(binding)) {
128             if (isMovedMember(binding)) {
129                 if (node.getQualifier() != null)
130                     rewrite(node, fTarget);
131             } else
132                 rewrite(node, fSource);
133             
134         } else if (isTargetAccess(binding)) {
135             // remove qualifier:
136
SimpleName replace= (SimpleName)fCuRewrite.getASTRewrite().createCopyTarget(node.getName());
137             fCuRewrite.getASTRewrite().replace(node, replace, null);
138             fCuRewrite.getImportRemover().registerRemovedNode(node);
139         }
140         return super.visit(node);
141     }
142     
143     public boolean visit(MethodRef node) {
144         IBinding binding= node.resolveBinding();
145         if (isSourceAccess(binding)) {
146             if (isMovedMember(binding)) {
147                 if (node.getQualifier() != null)
148                     rewrite(node, fTarget);
149             } else
150                 rewrite(node, fSource);
151             
152         } else if (isTargetAccess(binding)) {
153             // remove qualifier:
154
SimpleName replace= (SimpleName)fCuRewrite.getASTRewrite().createCopyTarget(node.getName());
155             fCuRewrite.getASTRewrite().replace(node, replace, null);
156             fCuRewrite.getImportRemover().registerRemovedNode(node);
157         }
158         return super.visit(node);
159     }
160
161     //---- helper methods --------------------------------------
162

163     private boolean isSourceAccess(IBinding binding) {
164         if (binding instanceof IMethodBinding) {
165             IMethodBinding method= (IMethodBinding)binding;
166             return Modifier.isStatic(method.getModifiers()) && Bindings.equals(fSource, method.getDeclaringClass());
167         } else if (binding instanceof ITypeBinding) {
168             ITypeBinding type= (ITypeBinding)binding;
169             return Modifier.isStatic(type.getModifiers()) && Bindings.equals(fSource, type.getDeclaringClass());
170         } else if (binding instanceof IVariableBinding) {
171             IVariableBinding field= (IVariableBinding)binding;
172             return field.isField() && Modifier.isStatic(field.getModifiers()) && Bindings.equals(fSource, field.getDeclaringClass());
173         }
174         return false;
175     }
176     
177     private boolean isTargetAccess(IBinding binding) {
178         if (binding instanceof IMethodBinding) {
179             IMethodBinding method= (IMethodBinding)binding;
180             return Modifier.isStatic(method.getModifiers()) && Bindings.equals(fTarget, method.getDeclaringClass());
181         } else if (binding instanceof ITypeBinding) {
182             ITypeBinding type= (ITypeBinding)binding;
183             return Modifier.isStatic(type.getModifiers()) && Bindings.equals(fTarget, type.getDeclaringClass());
184         } else if (binding instanceof IVariableBinding) {
185             IVariableBinding field= (IVariableBinding)binding;
186             return field.isField() && Modifier.isStatic(field.getModifiers()) && Bindings.equals(fTarget, field.getDeclaringClass());
187         }
188         return false;
189     }
190 }
191
Popular Tags