KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > matching > MatchLocatorParser


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.core.search.matching;
12
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.ast.*;
15 import org.eclipse.jdt.internal.compiler.lookup.*;
16 import org.eclipse.jdt.internal.compiler.parser.Parser;
17 import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
18
19 /**
20  * A parser that locates ast nodes that match a given search pattern.
21  */

22 public class MatchLocatorParser extends Parser {
23
24 MatchingNodeSet nodeSet;
25 PatternLocator patternLocator;
26 private ASTVisitor localDeclarationVisitor;
27
28 public static MatchLocatorParser createParser(ProblemReporter problemReporter, MatchLocator locator) {
29     if ((locator.matchContainer & PatternLocator.COMPILATION_UNIT_CONTAINER) != 0)
30         return new ImportMatchLocatorParser(problemReporter, locator);
31     return new MatchLocatorParser(problemReporter, locator);
32 }
33
34 /**
35  * An ast visitor that visits local type declarations.
36  */

37 public class NoClassNoMethodDeclarationVisitor extends ASTVisitor {
38     public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) {
39         return (constructorDeclaration.bits & ASTNode.HasLocalType) != 0; // continue only if it has local type
40
}
41     public boolean visit(FieldDeclaration fieldDeclaration, MethodScope scope) {
42         return (fieldDeclaration.bits & ASTNode.HasLocalType) != 0; // continue only if it has local type;
43
}
44     public boolean visit(Initializer initializer, MethodScope scope) {
45         return (initializer.bits & ASTNode.HasLocalType) != 0; // continue only if it has local type
46
}
47     public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
48         return (methodDeclaration.bits & ASTNode.HasLocalType) != 0; // continue only if it has local type
49
}
50 }
51 public class MethodButNoClassDeclarationVisitor extends NoClassNoMethodDeclarationVisitor {
52     public boolean visit(TypeDeclaration localTypeDeclaration, BlockScope scope) {
53         patternLocator.match(localTypeDeclaration, nodeSet);
54         return true;
55     }
56 }
57 public class ClassButNoMethodDeclarationVisitor extends ASTVisitor {
58     public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) {
59         patternLocator.match(constructorDeclaration, nodeSet);
60         return (constructorDeclaration.bits & ASTNode.HasLocalType) != 0; // continue only if it has local type
61
}
62     public boolean visit(FieldDeclaration fieldDeclaration, MethodScope scope) {
63         patternLocator.match(fieldDeclaration, nodeSet);
64         return (fieldDeclaration.bits & ASTNode.HasLocalType) != 0; // continue only if it has local type;
65
}
66     public boolean visit(Initializer initializer, MethodScope scope) {
67         patternLocator.match(initializer, nodeSet);
68         return (initializer.bits & ASTNode.HasLocalType) != 0; // continue only if it has local type
69
}
70     public boolean visit(TypeDeclaration memberTypeDeclaration, ClassScope scope) {
71         patternLocator.match(memberTypeDeclaration, nodeSet);
72         return true;
73     }
74     public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
75         patternLocator.match(methodDeclaration, nodeSet);
76         return (methodDeclaration.bits & ASTNode.HasLocalType) != 0; // continue only if it has local type
77
}
78     public boolean visit(AnnotationMethodDeclaration methodDeclaration, ClassScope scope) {
79         patternLocator.match(methodDeclaration, nodeSet);
80         return false; // no local type for annotation type members
81
}
82 }
83 public class ClassAndMethodDeclarationVisitor extends ClassButNoMethodDeclarationVisitor {
84     public boolean visit(TypeDeclaration localTypeDeclaration, BlockScope scope) {
85         patternLocator.match(localTypeDeclaration, nodeSet);
86         return true;
87     }
88 }
89
90 protected MatchLocatorParser(ProblemReporter problemReporter, MatchLocator locator) {
91     super(problemReporter, true);
92     this.reportOnlyOneSyntaxError = true;
93     this.patternLocator = locator.patternLocator;
94     if ((locator.matchContainer & PatternLocator.CLASS_CONTAINER) != 0) {
95         this.localDeclarationVisitor = (locator.matchContainer & PatternLocator.METHOD_CONTAINER) != 0
96             ? new ClassAndMethodDeclarationVisitor()
97             : new ClassButNoMethodDeclarationVisitor();
98     } else {
99         this.localDeclarationVisitor = (locator.matchContainer & PatternLocator.METHOD_CONTAINER) != 0
100             ? new MethodButNoClassDeclarationVisitor()
101             : new NoClassNoMethodDeclarationVisitor();
102     }
103 }
104 public void checkComment() {
105     super.checkComment();
106     if (this.javadocParser.checkDocComment && this.javadoc != null) {
107
108         // Search for pattern locator matches in javadoc comment parameters @param tags
109
JavadocSingleNameReference[] paramReferences = this.javadoc.paramReferences;
110         if (paramReferences != null) {
111             for (int i=0, length=paramReferences.length; i < length; i++) {
112                 this.patternLocator.match(paramReferences[i], this.nodeSet);
113             }
114         }
115
116         // Search for pattern locator matches in javadoc comment type parameters @param tags
117
JavadocSingleTypeReference[] paramTypeParameters = this.javadoc.paramTypeParameters;
118         if (paramTypeParameters != null) {
119             for (int i=0, length=paramTypeParameters.length; i < length; i++) {
120                 this.patternLocator.match(paramTypeParameters[i], this.nodeSet);
121             }
122         }
123
124         // Search for pattern locator matches in javadoc comment @throws/@exception tags
125
TypeReference[] thrownExceptions = this.javadoc.exceptionReferences;
126         if (thrownExceptions != null) {
127             for (int i=0, length=thrownExceptions.length; i < length; i++) {
128                 this.patternLocator.match(thrownExceptions[i], this.nodeSet);
129             }
130         }
131
132         // Search for pattern locator matches in javadoc comment @see tags
133
Expression[] references = this.javadoc.seeReferences;
134         if (references != null) {
135             for (int i=0, length=references.length; i < length; i++) {
136                 Expression reference = references[i];
137                 if (reference instanceof TypeReference) {
138                     TypeReference typeRef = (TypeReference) reference;
139                     this.patternLocator.match(typeRef, this.nodeSet);
140                 } else if (reference instanceof JavadocFieldReference) {
141                     JavadocFieldReference fieldRef = (JavadocFieldReference) reference;
142                     this.patternLocator.match(fieldRef, this.nodeSet);
143                     if (fieldRef.receiver instanceof TypeReference && !fieldRef.receiver.isThis()) {
144                         TypeReference typeRef = (TypeReference) fieldRef.receiver;
145                         this.patternLocator.match(typeRef, this.nodeSet);
146                     }
147                 } else if (reference instanceof JavadocMessageSend) {
148                     JavadocMessageSend messageSend = (JavadocMessageSend) reference;
149                     this.patternLocator.match(messageSend, this.nodeSet);
150                     if (messageSend.receiver instanceof TypeReference && !messageSend.receiver.isThis()) {
151                         TypeReference typeRef = (TypeReference) messageSend.receiver;
152                         this.patternLocator.match(typeRef, this.nodeSet);
153                     }
154                     if (messageSend.arguments != null) {
155                         for (int a=0,al=messageSend.arguments.length; a<al; a++) {
156                             JavadocArgumentExpression argument = (JavadocArgumentExpression) messageSend.arguments[a];
157                             if (argument.argument != null && argument.argument.type != null) {
158                                 this.patternLocator.match(argument.argument.type, this.nodeSet);
159                             }
160                         }
161                     }
162                 } else if (reference instanceof JavadocAllocationExpression) {
163                     JavadocAllocationExpression constructor = (JavadocAllocationExpression) reference;
164                     this.patternLocator.match(constructor, this.nodeSet);
165                     if (constructor.type != null && !constructor.type.isThis()) {
166                         this.patternLocator.match(constructor.type, this.nodeSet);
167                     }
168                     if (constructor.arguments != null) {
169                         for (int a=0,al=constructor.arguments.length; a<al; a++) {
170                             this.patternLocator.match(constructor.arguments[a], this.nodeSet);
171                             JavadocArgumentExpression argument = (JavadocArgumentExpression) constructor.arguments[a];
172                             if (argument.argument != null && argument.argument.type != null) {
173                                 this.patternLocator.match(argument.argument.type, this.nodeSet);
174                             }
175                         }
176                     }
177                 }
178             }
179         }
180     }
181 }
182 protected void classInstanceCreation(boolean alwaysQualified) {
183     super.classInstanceCreation(alwaysQualified);
184     this.patternLocator.match(this.expressionStack[this.expressionPtr], this.nodeSet);
185 }
186 protected void consumeAssignment() {
187     super.consumeAssignment();
188     this.patternLocator.match(this.expressionStack[this.expressionPtr], this.nodeSet);
189 }
190 protected void consumeClassInstanceCreationExpressionQualifiedWithTypeArguments() {
191     super.consumeClassInstanceCreationExpressionWithTypeArguments();
192     this.patternLocator.match(this.expressionStack[this.expressionPtr], this.nodeSet);
193 }
194 protected void consumeClassInstanceCreationExpressionWithTypeArguments() {
195     super.consumeClassInstanceCreationExpressionWithTypeArguments();
196     this.patternLocator.match(this.expressionStack[this.expressionPtr], this.nodeSet);
197 }
198 protected void consumeExplicitConstructorInvocation(int flag, int recFlag) {
199     super.consumeExplicitConstructorInvocation(flag, recFlag);
200     this.patternLocator.match(this.astStack[this.astPtr], this.nodeSet);
201 }
202 protected void consumeExplicitConstructorInvocationWithTypeArguments(int flag, int recFlag) {
203     super.consumeExplicitConstructorInvocationWithTypeArguments(flag, recFlag);
204     this.patternLocator.match(this.astStack[this.astPtr], this.nodeSet);
205 }
206 protected void consumeFieldAccess(boolean isSuperAccess) {
207     super.consumeFieldAccess(isSuperAccess);
208
209     // this is always a Reference
210
this.patternLocator.match((Reference) this.expressionStack[this.expressionPtr], this.nodeSet);
211 }
212 protected void consumeFormalParameter(boolean isVarArgs) {
213     super.consumeFormalParameter(isVarArgs);
214
215     // this is always a LocalDeclaration
216
this.patternLocator.match((LocalDeclaration) this.astStack[this.astPtr], this.nodeSet);
217 }
218 protected void consumeLocalVariableDeclaration() {
219     super.consumeLocalVariableDeclaration();
220
221     // this is always a LocalDeclaration
222
this.patternLocator.match((LocalDeclaration) this.astStack[this.astPtr], this.nodeSet);
223 }
224 protected void consumeMarkerAnnotation() {
225     super.consumeMarkerAnnotation();
226     // this is always an Annotation
227
Annotation annotation = (Annotation) expressionStack[expressionPtr];
228     this.patternLocator.match(annotation, nodeSet);
229 }
230 protected void consumeMemberValuePair() {
231     super.consumeMemberValuePair();
232
233     // this is always a MemberValuePair
234
this.patternLocator.match((MemberValuePair) this.astStack[this.astPtr], this.nodeSet);
235 }
236 protected void consumeMethodInvocationName() {
237     super.consumeMethodInvocationName();
238
239     // this is always a MessageSend
240
this.patternLocator.match((MessageSend) this.expressionStack[this.expressionPtr], this.nodeSet);
241 }
242 protected void consumeMethodInvocationNameWithTypeArguments() {
243     super.consumeMethodInvocationNameWithTypeArguments();
244
245     // this is always a MessageSend
246
this.patternLocator.match((MessageSend) this.expressionStack[this.expressionPtr], this.nodeSet);
247 }
248 protected void consumeMethodInvocationPrimary() {
249     super.consumeMethodInvocationPrimary();
250
251     // this is always a MessageSend
252
this.patternLocator.match((MessageSend) this.expressionStack[this.expressionPtr], this.nodeSet);
253 }
254 protected void consumeMethodInvocationPrimaryWithTypeArguments() {
255     super.consumeMethodInvocationPrimaryWithTypeArguments();
256
257     // this is always a MessageSend
258
this.patternLocator.match((MessageSend) this.expressionStack[this.expressionPtr], this.nodeSet);
259 }
260 protected void consumeMethodInvocationSuper() {
261     super.consumeMethodInvocationSuper();
262
263     // this is always a MessageSend
264
this.patternLocator.match((MessageSend) this.expressionStack[this.expressionPtr], this.nodeSet);
265 }
266 protected void consumeMethodInvocationSuperWithTypeArguments() {
267     super.consumeMethodInvocationSuperWithTypeArguments();
268
269     // this is always a MessageSend
270
this.patternLocator.match((MessageSend) this.expressionStack[this.expressionPtr], this.nodeSet);
271 }
272 protected void consumeNormalAnnotation() {
273     super.consumeNormalAnnotation();
274     // this is always an Annotation
275
Annotation annotation = (Annotation) expressionStack[expressionPtr];
276     this.patternLocator.match(annotation, nodeSet);
277 }
278 protected void consumePrimaryNoNewArray() {
279     // pop parenthesis positions (and don't update expression positions
280
// (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=23329)
281
intPtr--;
282     intPtr--;
283 }
284
285 protected void consumePrimaryNoNewArrayWithName() {
286     // PrimaryNoNewArray ::= PushLPAREN Expression PushRPAREN
287
pushOnExpressionStack(getUnspecifiedReferenceOptimized());
288     // pop parenthesis positions (and don't update expression positions
289
// (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=23329)
290
intPtr--;
291     intPtr--;
292 }
293 protected void consumeSingleMemberAnnotation() {
294     super.consumeSingleMemberAnnotation();
295     // this is always an Annotation
296
Annotation annotation = (Annotation) expressionStack[expressionPtr];
297     this.patternLocator.match(annotation, nodeSet);
298 }
299 protected void consumeTypeArgument() {
300     super.consumeTypeArgument();
301     patternLocator.match((TypeReference)genericsStack[genericsPtr], nodeSet);
302 }
303 protected void consumeTypeParameterHeader() {
304     super.consumeTypeParameterHeader();
305     patternLocator.match((TypeParameter)genericsStack[genericsPtr], nodeSet);
306 }
307 protected void consumeUnaryExpression(int op, boolean post) {
308     super.consumeUnaryExpression(op, post);
309     this.patternLocator.match(this.expressionStack[this.expressionPtr], this.nodeSet);
310 }
311 protected TypeReference copyDims(TypeReference typeRef, int dim) {
312     TypeReference result = super.copyDims(typeRef, dim);
313      if (this.nodeSet.removePossibleMatch(typeRef) != null)
314         this.nodeSet.addPossibleMatch(result);
315      else if (this.nodeSet.removeTrustedMatch(typeRef) != null)
316         this.nodeSet.addTrustedMatch(result, true);
317     return result;
318 }
319 protected TypeReference getTypeReference(int dim) {
320     TypeReference typeRef = super.getTypeReference(dim);
321     this.patternLocator.match(typeRef, this.nodeSet); // NB: Don't check container since type reference can happen anywhere
322
return typeRef;
323 }
324 protected NameReference getUnspecifiedReference() {
325     NameReference nameRef = super.getUnspecifiedReference();
326     this.patternLocator.match(nameRef, this.nodeSet); // NB: Don't check container since unspecified reference can happen anywhere
327
return nameRef;
328 }
329 protected NameReference getUnspecifiedReferenceOptimized() {
330     NameReference nameRef = super.getUnspecifiedReferenceOptimized();
331     this.patternLocator.match(nameRef, this.nodeSet); // NB: Don't check container since unspecified reference can happen anywhere
332
return nameRef;
333 }
334 /**
335  * Parses the method bodies in the given compilation unit
336  * @param unit CompilationUnitDeclaration
337  */

338 public void parseBodies(CompilationUnitDeclaration unit) {
339     TypeDeclaration[] types = unit.types;
340     if (types == null) return;
341
342     for (int i = 0; i < types.length; i++) {
343         TypeDeclaration type = types[i];
344         this.patternLocator.match(type, this.nodeSet);
345         this.parseBodies(type, unit);
346     }
347 }
348 /**
349  * Parses the member bodies in the given type.
350  * @param type TypeDeclaration
351  * @param unit CompilationUnitDeclaration
352  */

353 protected void parseBodies(TypeDeclaration type, CompilationUnitDeclaration unit) {
354     FieldDeclaration[] fields = type.fields;
355     if (fields != null) {
356         for (int i = 0; i < fields.length; i++) {
357             FieldDeclaration field = fields[i];
358             if (field instanceof Initializer)
359                 this.parse((Initializer) field, type, unit);
360             field.traverse(localDeclarationVisitor, null);
361         }
362     }
363
364     AbstractMethodDeclaration[] methods = type.methods;
365     if (methods != null) {
366         for (int i = 0; i < methods.length; i++) {
367             AbstractMethodDeclaration method = methods[i];
368             if (method.sourceStart >= type.bodyStart) { // if not synthetic
369
if (method instanceof MethodDeclaration) {
370                     MethodDeclaration methodDeclaration = (MethodDeclaration) method;
371                     this.parse(methodDeclaration, unit);
372                     methodDeclaration.traverse(localDeclarationVisitor, (ClassScope) null);
373                 } else if (method instanceof ConstructorDeclaration) {
374                     ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) method;
375                     this.parse(constructorDeclaration, unit);
376                     constructorDeclaration.traverse(localDeclarationVisitor, (ClassScope) null);
377                 }
378             } else if (method.isDefaultConstructor()) {
379                 method.parseStatements(this, unit);
380             }
381         }
382     }
383
384     TypeDeclaration[] memberTypes = type.memberTypes;
385     if (memberTypes != null) {
386         for (int i = 0; i < memberTypes.length; i++) {
387             TypeDeclaration memberType = memberTypes[i];
388             this.parseBodies(memberType, unit);
389             memberType.traverse(localDeclarationVisitor, (ClassScope) null);
390         }
391     }
392 }
393 }
394
395
Popular Tags