KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > codeassist > complete > CompletionNodeDetector


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.codeassist.complete;
12
13 import org.eclipse.jdt.internal.compiler.*;
14 import org.eclipse.jdt.internal.compiler.ast.*;
15 import org.eclipse.jdt.internal.compiler.lookup.*;
16
17 /**
18  * Detect the presence of a node in expression
19  */

20 public class CompletionNodeDetector extends ASTVisitor {
21     private ASTNode searchedNode;
22     private ASTNode parent;
23     private boolean result;
24     
25     public CompletionNodeDetector(ASTNode searchedNode, ASTNode visitedAst){
26         this.searchedNode = searchedNode;
27         this.result = false;
28         
29         if(searchedNode != null && visitedAst != null) {
30             visitedAst.traverse(this, null);
31         }
32     }
33     
34     public boolean containsCompletionNode() {
35         return this.result;
36     }
37     
38     public ASTNode getCompletionNodeParent() {
39         return this.parent;
40     }
41     public void endVisit(AllocationExpression allocationExpression, BlockScope scope) {
42         endVisit(allocationExpression);
43     }
44     public void endVisit(AND_AND_Expression and_and_Expression, BlockScope scope) {
45         endVisit(and_and_Expression);
46     }
47     public void endVisit(ArrayAllocationExpression arrayAllocationExpression, BlockScope scope) {
48         endVisit(arrayAllocationExpression);
49     }
50     public void endVisit(ArrayInitializer arrayInitializer, BlockScope scope) {
51         endVisit(arrayInitializer);
52     }
53     public void endVisit(ArrayQualifiedTypeReference arrayQualifiedTypeReference, BlockScope scope) {
54         endVisit(arrayQualifiedTypeReference);
55     }
56     public void endVisit(ArrayQualifiedTypeReference arrayQualifiedTypeReference, ClassScope scope) {
57         endVisit(arrayQualifiedTypeReference);
58     }
59     public void endVisit(ArrayReference arrayReference, BlockScope scope) {
60         endVisit(arrayReference);
61     }
62     public void endVisit(ArrayTypeReference arrayTypeReference, BlockScope scope) {
63         endVisit(arrayTypeReference);
64     }
65     public void endVisit(ArrayTypeReference arrayTypeReference, ClassScope scope) {
66         endVisit(arrayTypeReference);
67     }
68     public void endVisit(Assignment assignment, BlockScope scope) {
69         endVisit(assignment);
70     }
71     public void endVisit(BinaryExpression binaryExpression, BlockScope scope) {
72         endVisit(binaryExpression);
73     }
74     public void endVisit(CastExpression castExpression, BlockScope scope) {
75         endVisit(castExpression);
76     }
77     public void endVisit(CompoundAssignment compoundAssignment, BlockScope scope) {
78         endVisit(compoundAssignment);
79     }
80     public void endVisit(ConditionalExpression conditionalExpression, BlockScope scope) {
81         endVisit(conditionalExpression);
82     }
83     public void endVisit(EqualExpression equalExpression, BlockScope scope) {
84         endVisit(equalExpression);
85     }
86     public void endVisit(ExplicitConstructorCall explicitConstructor, BlockScope scope) {
87         endVisit(explicitConstructor);
88     }
89     public void endVisit(FieldReference fieldReference, BlockScope scope) {
90         endVisit(fieldReference);
91     }
92     public void endVisit(InstanceOfExpression instanceOfExpression, BlockScope scope) {
93         endVisit(instanceOfExpression);
94     }
95     public void endVisit(MessageSend messageSend, BlockScope scope) {
96         endVisit(messageSend);
97     }
98     public void endVisit(OR_OR_Expression or_or_Expression, BlockScope scope) {
99         endVisit(or_or_Expression);
100     }
101     public void endVisit(ParameterizedQualifiedTypeReference parameterizedQualifiedTypeReference, BlockScope scope) {
102         endVisit(parameterizedQualifiedTypeReference);
103     }
104     public void endVisit(ParameterizedQualifiedTypeReference parameterizedQualifiedTypeReference, ClassScope scope) {
105         endVisit(parameterizedQualifiedTypeReference);
106     }
107     public void endVisit(ParameterizedSingleTypeReference parameterizedSingleTypeReference, BlockScope scope) {
108         endVisit(parameterizedSingleTypeReference);
109     }
110     public void endVisit(ParameterizedSingleTypeReference parameterizedSingleTypeReference, ClassScope scope) {
111         endVisit(parameterizedSingleTypeReference);
112     }
113     public void endVisit(PostfixExpression postfixExpression, BlockScope scope) {
114         endVisit(postfixExpression);
115     }
116     public void endVisit(PrefixExpression prefixExpression, BlockScope scope) {
117         endVisit(prefixExpression);
118     }
119     public void endVisit(QualifiedAllocationExpression qualifiedAllocationExpression, BlockScope scope) {
120         endVisit(qualifiedAllocationExpression);
121     }
122     public void endVisit(QualifiedNameReference qualifiedNameReference, BlockScope scope) {
123         endVisit(qualifiedNameReference);
124     }
125     public void endVisit(QualifiedSuperReference qualifiedSuperReference, BlockScope scope) {
126         endVisit(qualifiedSuperReference);
127     }
128     public void endVisit(QualifiedThisReference qualifiedThisReference, BlockScope scope) {
129         endVisit(qualifiedThisReference);
130     }
131     public void endVisit(QualifiedTypeReference qualifiedTypeReference, BlockScope scope) {
132         endVisit(qualifiedTypeReference);
133     }
134     public void endVisit(QualifiedTypeReference qualifiedTypeReference, ClassScope scope) {
135         endVisit(qualifiedTypeReference);
136     }
137     public void endVisit(SingleNameReference singleNameReference, BlockScope scope) {
138         endVisit(singleNameReference);
139     }
140     public void endVisit(SingleTypeReference singleTypeReference, BlockScope scope) {
141         endVisit(singleTypeReference);
142     }
143     public void endVisit(SingleTypeReference singleTypeReference, ClassScope scope) {
144         endVisit(singleTypeReference);
145     }
146     public void endVisit(SuperReference superReference, BlockScope scope) {
147         endVisit(superReference);
148     }
149     public void endVisit(ThisReference thisReference, BlockScope scope) {
150         endVisit(thisReference);
151     }
152     public void endVisit(UnaryExpression unaryExpression, BlockScope scope) {
153         endVisit(unaryExpression);
154     }
155     public void endVisit(MemberValuePair pair, BlockScope scope) {
156         endVisit(pair);
157     }
158     public void endVisit(MemberValuePair pair, CompilationUnitScope scope) {
159         endVisit(pair);
160     }
161     public boolean visit(AllocationExpression allocationExpression, BlockScope scope) {
162         return this.visit(allocationExpression);
163     }
164     public boolean visit(AND_AND_Expression and_and_Expression, BlockScope scope) {
165         return this.visit(and_and_Expression);
166     }
167     public boolean visit(ArrayAllocationExpression arrayAllocationExpression, BlockScope scope) {
168         return this.visit(arrayAllocationExpression);
169     }
170     public boolean visit(ArrayInitializer arrayInitializer, BlockScope scope) {
171         return this.visit(arrayInitializer);
172     }
173     public boolean visit(ArrayQualifiedTypeReference arrayQualifiedTypeReference, BlockScope scope) {
174         return this.visit(arrayQualifiedTypeReference);
175     }
176     public boolean visit(ArrayQualifiedTypeReference arrayQualifiedTypeReference, ClassScope scope) {
177         return this.visit(arrayQualifiedTypeReference);
178     }
179     public boolean visit(ArrayReference arrayReference, BlockScope scope) {
180         return this.visit(arrayReference);
181     }
182     public boolean visit(ArrayTypeReference arrayTypeReference, BlockScope scope) {
183         return this.visit(arrayTypeReference);
184     }
185     public boolean visit(ArrayTypeReference arrayTypeReference, ClassScope scope) {
186         return this.visit(arrayTypeReference);
187     }
188     public boolean visit(Assignment assignment, BlockScope scope) {
189         return this.visit(assignment);
190     }
191     public boolean visit(BinaryExpression binaryExpression, BlockScope scope) {
192         return this.visit(binaryExpression);
193     }
194     public boolean visit(CastExpression castExpression, BlockScope scope) {
195         return this.visit(castExpression);
196     }
197     public boolean visit(CompoundAssignment compoundAssignment, BlockScope scope) {
198         return this.visit(compoundAssignment);
199     }
200     public boolean visit(ConditionalExpression conditionalExpression, BlockScope scope) {
201         return this.visit(conditionalExpression);
202     }
203     public boolean visit(EqualExpression equalExpression, BlockScope scope) {
204         return this.visit(equalExpression);
205     }
206     public boolean visit(ExplicitConstructorCall explicitConstructor, BlockScope scope) {
207         return this.visit(explicitConstructor);
208     }
209     public boolean visit(FieldReference fieldReference, BlockScope scope) {
210         return this.visit(fieldReference);
211     }
212     public boolean visit(InstanceOfExpression instanceOfExpression, BlockScope scope) {
213         return this.visit(instanceOfExpression);
214     }
215     public boolean visit(MessageSend messageSend, BlockScope scope) {
216         return this.visit(messageSend);
217     }
218     public boolean visit(OR_OR_Expression or_or_Expression, BlockScope scope) {
219         return this.visit(or_or_Expression);
220     }
221     public boolean visit(ParameterizedQualifiedTypeReference parameterizedQualifiedTypeReference, BlockScope scope) {
222         return this.visit(parameterizedQualifiedTypeReference);
223     }
224     public boolean visit(ParameterizedQualifiedTypeReference parameterizedQualifiedTypeReference, ClassScope scope) {
225         return this.visit(parameterizedQualifiedTypeReference);
226     }
227     public boolean visit(ParameterizedSingleTypeReference parameterizedSingleTypeReference, BlockScope scope) {
228         return this.visit(parameterizedSingleTypeReference);
229     }
230     public boolean visit(ParameterizedSingleTypeReference parameterizedSingleTypeReference, ClassScope scope) {
231         return this.visit(parameterizedSingleTypeReference);
232     }
233     public boolean visit(PostfixExpression postfixExpression, BlockScope scope) {
234         return this.visit(postfixExpression);
235     }
236     public boolean visit(PrefixExpression prefixExpression, BlockScope scope) {
237         return this.visit(prefixExpression);
238     }
239     public boolean visit(QualifiedAllocationExpression qualifiedAllocationExpression, BlockScope scope) {
240         return this.visit(qualifiedAllocationExpression);
241     }
242     public boolean visit(QualifiedNameReference qualifiedNameReference, BlockScope scope) {
243         return this.visit(qualifiedNameReference);
244     }
245     public boolean visit(QualifiedSuperReference qualifiedSuperReference, BlockScope scope) {
246         return this.visit(qualifiedSuperReference);
247     }
248     public boolean visit(QualifiedThisReference qualifiedThisReference, BlockScope scope) {
249         return this.visit(qualifiedThisReference);
250     }
251     public boolean visit(QualifiedTypeReference qualifiedTypeReference, BlockScope scope) {
252         return this.visit(qualifiedTypeReference);
253     }
254     public boolean visit(QualifiedTypeReference qualifiedTypeReference, ClassScope scope) {
255         return this.visit(qualifiedTypeReference);
256     }
257     public boolean visit(SingleNameReference singleNameReference, BlockScope scope) {
258         return this.visit(singleNameReference);
259     }
260     public boolean visit(SingleTypeReference singleTypeReference, BlockScope scope) {
261         return this.visit(singleTypeReference);
262     }
263     public boolean visit(SingleTypeReference singleTypeReference, ClassScope scope) {
264         return this.visit(singleTypeReference);
265     }
266     public boolean visit(StringLiteral stringLiteral, BlockScope scope) {
267         return this.visit(stringLiteral);
268     }
269     public boolean visit(SuperReference superReference, BlockScope scope) {
270         return this.visit(superReference);
271     }
272     public boolean visit(ThisReference thisReference, BlockScope scope) {
273         return this.visit(thisReference);
274     }
275     public boolean visit(UnaryExpression unaryExpression, BlockScope scope) {
276         return this.visit(unaryExpression);
277     }
278     public boolean visit(MemberValuePair pair, BlockScope scope) {
279         return this.visit(pair);
280     }
281     public boolean visit(MemberValuePair pair, CompilationUnitScope scope) {
282         return this.visit(pair);
283     }
284     private void endVisit(ASTNode astNode) {
285         if(this.result && this.parent == null && astNode != this.searchedNode) {
286             if(!(astNode instanceof AllocationExpression && ((AllocationExpression) astNode).type == this.searchedNode)
287                 && !(astNode instanceof ConditionalExpression && ((ConditionalExpression) astNode).valueIfTrue == this.searchedNode)
288                 && !(astNode instanceof ConditionalExpression && ((ConditionalExpression) astNode).valueIfFalse == this.searchedNode)) {
289                 this.parent = astNode;
290             }
291         }
292     }
293     private boolean visit(ASTNode astNode) {
294         if(astNode == this.searchedNode) {
295             this.result = true;
296         }
297         return !this.result;
298     }
299 }
300
Popular Tags