KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > apt > dispatch > AnnotationDiscoveryVisitor


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.compiler.apt.dispatch;
12
13 import javax.lang.model.element.Element;
14 import javax.lang.model.element.TypeElement;
15
16 import org.eclipse.jdt.internal.compiler.ASTVisitor;
17 import org.eclipse.jdt.internal.compiler.apt.model.Factory;
18 import org.eclipse.jdt.internal.compiler.apt.util.ManyToMany;
19 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
20 import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
21 import org.eclipse.jdt.internal.compiler.ast.Annotation;
22 import org.eclipse.jdt.internal.compiler.ast.Argument;
23 import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
24 import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
25 import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
26 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
27 import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding;
28 import org.eclipse.jdt.internal.compiler.lookup.Binding;
29 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
30 import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
31 import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
32 import org.eclipse.jdt.internal.compiler.lookup.MethodScope;
33
34 /**
35  * This class is used to visit the JDT compiler internal AST to discover annotations,
36  * in the course of dispatching to annotation processors.
37  */

38 public class AnnotationDiscoveryVisitor extends ASTVisitor {
39     final BaseProcessingEnvImpl _env;
40     final Factory _factory;
41     /**
42      * Collects a many-to-many map of annotation types to
43      * the elements they appear on.
44      */

45     final ManyToMany<TypeElement, Element> _annoToElement;
46
47     public AnnotationDiscoveryVisitor(BaseProcessingEnvImpl env) {
48         _env = env;
49         _factory = env.getFactory();
50         _annoToElement = new ManyToMany<TypeElement, Element>();
51     }
52
53     @Override JavaDoc
54     public boolean visit(Argument argument, BlockScope scope) {
55         Annotation[] annotations = argument.annotations;
56         if (annotations != null) {
57             TypeDeclaration typeDeclaration = scope.referenceType();
58             typeDeclaration.binding.resolveTypesFor(((AbstractMethodDeclaration) scope.referenceContext()).binding);
59             this.resolveAnnotations(
60                     scope,
61                     annotations,
62                     argument.binding);
63         }
64         return false;
65     }
66
67     @Override JavaDoc
68     public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) {
69         Annotation[] annotations = constructorDeclaration.annotations;
70         if (annotations != null) {
71             this.resolveAnnotations(
72                     constructorDeclaration.scope,
73                     annotations,
74                     constructorDeclaration.binding);
75         }
76         Argument[] arguments = constructorDeclaration.arguments;
77         if (arguments != null) {
78             int argumentLength = arguments.length;
79             for (int i = 0; i < argumentLength; i++) {
80                 arguments[i].traverse(this, constructorDeclaration.scope);
81             }
82         }
83         return false;
84     }
85
86     @Override JavaDoc
87     public boolean visit(FieldDeclaration fieldDeclaration, MethodScope scope) {
88         Annotation[] annotations = fieldDeclaration.annotations;
89         if (annotations != null) {
90             this.resolveAnnotations(scope, annotations, fieldDeclaration.binding);
91         }
92         return false;
93     }
94
95     @Override JavaDoc
96     public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
97         Annotation[] annotations = methodDeclaration.annotations;
98         if (annotations != null) {
99             this.resolveAnnotations(
100                     methodDeclaration.scope,
101                     annotations,
102                     methodDeclaration.binding);
103         }
104
105         Argument[] arguments = methodDeclaration.arguments;
106         if (arguments != null) {
107             int argumentLength = arguments.length;
108             for (int i = 0; i < argumentLength; i++) {
109                 arguments[i].traverse(this, methodDeclaration.scope);
110             }
111         }
112         return false;
113     }
114
115     @Override JavaDoc
116     public boolean visit(TypeDeclaration memberTypeDeclaration, ClassScope scope) {
117         Annotation[] annotations = memberTypeDeclaration.annotations;
118         if (annotations != null) {
119             this.resolveAnnotations(
120                     memberTypeDeclaration.staticInitializerScope,
121                     annotations,
122                     memberTypeDeclaration.binding);
123         }
124         return true;
125     }
126
127     @Override JavaDoc
128     public boolean visit(TypeDeclaration typeDeclaration, CompilationUnitScope scope) {
129         Annotation[] annotations = typeDeclaration.annotations;
130         if (annotations != null) {
131             this.resolveAnnotations(
132                     typeDeclaration.staticInitializerScope,
133                     annotations,
134                     typeDeclaration.binding);
135         }
136         return true;
137     }
138
139     private void resolveAnnotations(
140             BlockScope scope,
141             Annotation[] annotations,
142             Binding currentBinding) {
143         ASTNode.resolveAnnotations(scope, annotations, currentBinding);
144         
145         for (Annotation annotation : annotations) {
146             AnnotationBinding binding = annotation.getCompilerAnnotation();
147             if (binding != null) { // binding should be resolved, but in case it's not, ignore it
148
TypeElement anno = (TypeElement)_factory.newElement(binding.getAnnotationType());
149                 Element element = _factory.newElement(currentBinding);
150                 _annoToElement.put(anno, element);
151             }
152         }
153     }
154 }
155
Popular Tags