KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > code > ConstantChecks


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.code;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jdt.core.dom.ASTVisitor;
16 import org.eclipse.jdt.core.dom.FieldAccess;
17 import org.eclipse.jdt.core.dom.IBinding;
18 import org.eclipse.jdt.core.dom.IMethodBinding;
19 import org.eclipse.jdt.core.dom.ITypeBinding;
20 import org.eclipse.jdt.core.dom.IVariableBinding;
21 import org.eclipse.jdt.core.dom.MethodInvocation;
22 import org.eclipse.jdt.core.dom.Modifier;
23 import org.eclipse.jdt.core.dom.Name;
24 import org.eclipse.jdt.core.dom.QualifiedName;
25 import org.eclipse.jdt.core.dom.SimpleName;
26 import org.eclipse.jdt.core.dom.SuperFieldAccess;
27 import org.eclipse.jdt.core.dom.SuperMethodInvocation;
28 import org.eclipse.jdt.core.dom.ThisExpression;
29 import org.eclipse.jdt.internal.corext.dom.fragments.ASTFragmentFactory;
30 import org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment;
31
32 class ConstantChecks {
33     private static abstract class ExpressionChecker extends ASTVisitor {
34
35         private final IExpressionFragment fExpression;
36         protected boolean fResult= true;
37
38         public ExpressionChecker(IExpressionFragment ex) {
39             fExpression= ex;
40         }
41         public boolean check() {
42             fResult= true;
43             fExpression.getAssociatedNode().accept(this);
44             return fResult;
45         }
46     }
47
48     private static class LoadTimeConstantChecker extends ExpressionChecker {
49         public LoadTimeConstantChecker(IExpressionFragment ex) {
50             super(ex);
51         }
52
53         public boolean visit(SuperFieldAccess node) {
54             fResult= false;
55             return false;
56         }
57         public boolean visit(SuperMethodInvocation node) {
58             fResult= false;
59             return false;
60         }
61         public boolean visit(ThisExpression node) {
62             fResult= false;
63             return false;
64         }
65         public boolean visit(FieldAccess node) {
66             fResult= new LoadTimeConstantChecker((IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(node.getExpression())).check();
67             return false;
68         }
69         public boolean visit(MethodInvocation node) {
70             if(node.getExpression() == null) {
71                 visitName(node.getName());
72             } else {
73                 fResult= new LoadTimeConstantChecker((IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(node.getExpression())).check();
74             }
75             
76             return false;
77         }
78         public boolean visit(QualifiedName node) {
79             return visitName(node);
80         }
81         public boolean visit(SimpleName node) {
82             return visitName(node);
83         }
84         
85         private boolean visitName(Name name) {
86             fResult= checkName(name);
87             return false; //Do not descend further
88
}
89         
90         private boolean checkName(Name name) {
91             IBinding binding= name.resolveBinding();
92             if (binding == null)
93                 return true; /* If the binding is null because of compile errors etc.,
94                                   scenarios which may have been deemed unacceptable in
95                                   the presence of semantic information will be admitted. */

96             
97             // If name represents a member:
98
if (binding instanceof IVariableBinding || binding instanceof IMethodBinding)
99                 return isMemberReferenceValidInClassInitialization(name);
100             else if (binding instanceof ITypeBinding)
101                 return ! ((ITypeBinding) binding).isTypeVariable();
102             else {
103                     /* IPackageBinding is not expected, as a package name not
104                         used as a type name prefix is not expected in such an
105                         expression. Other types are not expected either.
106                      */

107                     Assert.isTrue(false);
108                     return true;
109             }
110         }
111
112         private boolean isMemberReferenceValidInClassInitialization(Name name) {
113             IBinding binding= name.resolveBinding();
114             Assert.isTrue(binding instanceof IVariableBinding || binding instanceof IMethodBinding);
115
116             if(name instanceof SimpleName)
117                 return Modifier.isStatic(binding.getModifiers());
118             else {
119                 Assert.isTrue(name instanceof QualifiedName);
120                 return checkName(((QualifiedName) name).getQualifier());
121             }
122         }
123     }
124
125     private static class StaticFinalConstantChecker extends ExpressionChecker {
126         public StaticFinalConstantChecker(IExpressionFragment ex) {
127             super(ex);
128         }
129         
130         public boolean visit(SuperFieldAccess node) {
131             fResult= false;
132             return false;
133         }
134         public boolean visit(SuperMethodInvocation node) {
135             fResult= false;
136             return false;
137         }
138         public boolean visit(ThisExpression node) {
139             fResult= false;
140             return false;
141         }
142
143         public boolean visit(QualifiedName node) {
144             return visitName(node);
145         }
146         public boolean visit(SimpleName node) {
147             return visitName(node);
148         }
149         private boolean visitName(Name name) {
150             IBinding binding= name.resolveBinding();
151             if(binding == null) {
152                 /* If the binding is null because of compile errors etc.,
153                    scenarios which may have been deemed unacceptable in
154                    the presence of semantic information will be admitted.
155                    Descend deeper.
156                  */

157                  return true;
158             }
159             
160             int modifiers= binding.getModifiers();
161             if(binding instanceof IVariableBinding) {
162                 if (!(Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers))) {
163                     fResult= false;
164                     return false;
165                 }
166             } else if(binding instanceof IMethodBinding) {
167                 if (!Modifier.isStatic(modifiers)) {
168                     fResult= false;
169                     return false;
170                 }
171             } else if(binding instanceof ITypeBinding) {
172                 return false; // It's o.k. Don't descend deeper.
173

174             } else {
175                     /* IPackageBinding is not expected, as a package name not
176                         used as a type name prefix is not expected in such an
177                         expression. Other types are not expected either.
178                      */

179                     Assert.isTrue(false);
180                     return false;
181             }
182             
183             //Descend deeper:
184
return true;
185         }
186     }
187     
188     public static boolean isStaticFinalConstant(IExpressionFragment ex) {
189         return new StaticFinalConstantChecker(ex).check();
190     }
191
192     public static boolean isLoadTimeConstant(IExpressionFragment ex) {
193         return new LoadTimeConstantChecker(ex).check();
194     }
195 }
196
Popular Tags