]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ConstantChecks.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / code / ConstantChecks.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 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 *******************************************************************************/
11package org.eclipse.jdt.internal.corext.refactoring.code;
12
13import org.eclipse.core.runtime.Assert;
14
15import org.eclipse.jdt.core.dom.ASTVisitor;
16import org.eclipse.jdt.core.dom.FieldAccess;
17import org.eclipse.jdt.core.dom.IBinding;
18import org.eclipse.jdt.core.dom.IMethodBinding;
19import org.eclipse.jdt.core.dom.ITypeBinding;
20import org.eclipse.jdt.core.dom.IVariableBinding;
21import org.eclipse.jdt.core.dom.MethodInvocation;
22import org.eclipse.jdt.core.dom.Modifier;
23import org.eclipse.jdt.core.dom.Name;
24import org.eclipse.jdt.core.dom.QualifiedName;
25import org.eclipse.jdt.core.dom.SimpleName;
26import org.eclipse.jdt.core.dom.SuperFieldAccess;
27import org.eclipse.jdt.core.dom.SuperMethodInvocation;
28import org.eclipse.jdt.core.dom.ThisExpression;
29
30import org.eclipse.jdt.internal.corext.dom.fragments.ASTFragmentFactory;
31import org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment;
32
33class ConstantChecks {
34 private static abstract class ExpressionChecker extends ASTVisitor {
35
36 private final IExpressionFragment fExpression;
37 protected boolean fResult= true;
38
39 public ExpressionChecker(IExpressionFragment ex) {
40 fExpression= ex;
41 }
42 public boolean check() {
43 fResult= true;
44 fExpression.getAssociatedNode().accept(this);
45 return fResult;
46 }
47 }
48
49 private static class LoadTimeConstantChecker extends ExpressionChecker {
50 public LoadTimeConstantChecker(IExpressionFragment ex) {
51 super(ex);
52 }
53
54 @Override
55 public boolean visit(SuperFieldAccess node) {
56 fResult= false;
57 return false;
58 }
59 @Override
60 public boolean visit(SuperMethodInvocation node) {
61 fResult= false;
62 return false;
63 }
64 @Override
65 public boolean visit(ThisExpression node) {
66 fResult= false;
67 return false;
68 }
69 @Override
70 public boolean visit(FieldAccess node) {
71 fResult&= new LoadTimeConstantChecker((IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(node.getExpression())).check();
72 return false;
73 }
74 @Override
75 public boolean visit(MethodInvocation node) {
76 if(node.getExpression() == null) {
77 visitName(node.getName());
78 } else {
79 fResult&= new LoadTimeConstantChecker((IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(node.getExpression())).check();
80 }
81
82 return false;
83 }
84 @Override
85 public boolean visit(QualifiedName node) {
86 return visitName(node);
87 }
88 @Override
89 public boolean visit(SimpleName node) {
90 return visitName(node);
91 }
92
93 private boolean visitName(Name name) {
94 fResult&= checkName(name);
95 return false; //Do not descend further
96 }
97
98 private boolean checkName(Name name) {
99 IBinding binding= name.resolveBinding();
100 if (binding == null)
101 return true; /* If the binding is null because of compile errors etc.,
102 scenarios which may have been deemed unacceptable in
103 the presence of semantic information will be admitted. */
104
105 // If name represents a member:
106 if (binding instanceof IVariableBinding || binding instanceof IMethodBinding)
107 return isMemberReferenceValidInClassInitialization(name);
108 else if (binding instanceof ITypeBinding)
109 return ! ((ITypeBinding) binding).isTypeVariable();
110 else {
111 /* IPackageBinding is not expected, as a package name not
112 used as a type name prefix is not expected in such an
113 expression. Other types are not expected either.
114 */
115 Assert.isTrue(false);
116 return true;
117 }
118 }
119
120 private boolean isMemberReferenceValidInClassInitialization(Name name) {
121 IBinding binding= name.resolveBinding();
122 Assert.isTrue(binding instanceof IVariableBinding || binding instanceof IMethodBinding);
123
124 if(name instanceof SimpleName)
125 return Modifier.isStatic(binding.getModifiers());
126 else {
127 Assert.isTrue(name instanceof QualifiedName);
128 return checkName(((QualifiedName) name).getQualifier());
129 }
130 }
131 }
132
133 private static class StaticFinalConstantChecker extends ExpressionChecker {
134 public StaticFinalConstantChecker(IExpressionFragment ex) {
135 super(ex);
136 }
137
138 @Override
139 public boolean visit(SuperFieldAccess node) {
140 fResult= false;
141 return false;
142 }
143 @Override
144 public boolean visit(SuperMethodInvocation node) {
145 fResult= false;
146 return false;
147 }
148 @Override
149 public boolean visit(ThisExpression node) {
150 fResult= false;
151 return false;
152 }
153
154 @Override
155 public boolean visit(QualifiedName node) {
156 return visitName(node);
157 }
158 @Override
159 public boolean visit(SimpleName node) {
160 return visitName(node);
161 }
162 private boolean visitName(Name name) {
163 IBinding binding= name.resolveBinding();
164 if(binding == null) {
165 /* If the binding is null because of compile errors etc.,
166 scenarios which may have been deemed unacceptable in
167 the presence of semantic information will be admitted.
168 Descend deeper.
169 */
170 return true;
171 }
172
173 int modifiers= binding.getModifiers();
174 if(binding instanceof IVariableBinding) {
175 if (!(Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers))) {
176 fResult= false;
177 return false;
178 }
179 } else if(binding instanceof IMethodBinding) {
180 if (!Modifier.isStatic(modifiers)) {
181 fResult= false;
182 return false;
183 }
184 } else if(binding instanceof ITypeBinding) {
185 return false; // It's o.k. Don't descend deeper.
186
187 } else {
188 /* IPackageBinding is not expected, as a package name not
189 used as a type name prefix is not expected in such an
190 expression. Other types are not expected either.
191 */
192 Assert.isTrue(false);
193 return false;
194 }
195
196 //Descend deeper:
197 return true;
198 }
199 }
200
201 public static boolean isStaticFinalConstant(IExpressionFragment ex) {
202 return new StaticFinalConstantChecker(ex).check();
203 }
204
205 public static boolean isLoadTimeConstant(IExpressionFragment ex) {
206 return new LoadTimeConstantChecker(ex).check();
207 }
208}