KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > declaration > ASTBasedDeclarationImpl


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
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  * tyeung@bea.com - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.apt.core.internal.declaration;
12
13 import java.lang.annotation.Annotation JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
20 import org.eclipse.jdt.apt.core.internal.util.SourcePositionImpl;
21 import org.eclipse.jdt.core.dom.ASTNode;
22 import org.eclipse.jdt.core.dom.BodyDeclaration;
23 import org.eclipse.jdt.core.dom.CompilationUnit;
24 import org.eclipse.jdt.core.dom.IExtendedModifier;
25 import org.eclipse.jdt.core.dom.IAnnotationBinding;
26 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
27 import org.eclipse.jdt.core.dom.VariableDeclaration;
28 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
29
30 import com.sun.mirror.declaration.AnnotationMirror;
31 import com.sun.mirror.declaration.Modifier;
32 import com.sun.mirror.util.SourcePosition;
33
34 /**
35  * Most mirror implementation are based on bindings but in some cases because of
36  * incomplete information no bindings will be created at all. This implementation
37  * is to allow clients to get to the partial information that's available on certain
38  * declarations.
39  *
40  * The prefered implementation of the API is to rely on the binding since it is a more
41  * complete and versatile representation of a declaration.
42  */

43 public abstract class ASTBasedDeclarationImpl extends EclipseDeclarationImpl {
44
45     static final String JavaDoc EMPTY_STRING = ""; //$NON-NLS-1$
46
/** either a <code>BodyDeclaration</code> or a <code>VariableDeclaration</code> */
47     protected final ASTNode _astNode;
48     /** the file which this ast node came from */
49     protected final IFile _file;
50     public ASTBasedDeclarationImpl(
51             ASTNode astNode,
52             IFile file,
53             BaseProcessorEnv env)
54     {
55         super(env);
56         assert astNode != null : "ast node cannot be missing"; //$NON-NLS-1$
57
assert file != null : "file cannot be missing";//$NON-NLS-1$
58

59         assert astNode instanceof BodyDeclaration ||
60            astNode instanceof VariableDeclaration :
61            "ast node must be either a body declaration or a variable declaration"; //$NON-NLS-1$
62

63         _astNode = astNode;
64         _file = file;
65     }
66     
67     public Collection JavaDoc<Modifier> getModifiers()
68     {
69         int modBits = 0;
70         if( _astNode instanceof BodyDeclaration )
71             modBits = ((BodyDeclaration)_astNode).getModifiers();
72         else if( _astNode instanceof SingleVariableDeclaration )
73             modBits = ((SingleVariableDeclaration)_astNode).getModifiers();
74         else{
75             ASTNode parent = ((VariableDeclarationFragment)_astNode).getParent();
76             if( _astNode instanceof BodyDeclaration )
77                 modBits = ((BodyDeclaration)parent).getModifiers();
78         }
79         
80         return getModifiers(modBits);
81     }
82     
83     private Collection JavaDoc<Modifier> getModifiers(int modBits)
84     {
85         final List JavaDoc<Modifier> mods = new ArrayList JavaDoc<Modifier>(4);
86         if( org.eclipse.jdt.core.dom.Modifier.isAbstract(modBits) )
87             mods.add(Modifier.ABSTRACT);
88         if( org.eclipse.jdt.core.dom.Modifier.isFinal(modBits) )
89             mods.add(Modifier.FINAL);
90         if( org.eclipse.jdt.core.dom.Modifier.isNative(modBits) )
91             mods.add(Modifier.NATIVE);
92         if( org.eclipse.jdt.core.dom.Modifier.isPrivate(modBits) )
93             mods.add(Modifier.PRIVATE);
94         if( org.eclipse.jdt.core.dom.Modifier.isProtected(modBits) )
95             mods.add(Modifier.PROTECTED);
96         if( org.eclipse.jdt.core.dom.Modifier.isPublic(modBits) )
97             mods.add(Modifier.PUBLIC);
98         if( org.eclipse.jdt.core.dom.Modifier.isStatic(modBits) )
99             mods.add(Modifier.STATIC);
100         if( org.eclipse.jdt.core.dom.Modifier.isStrictfp(modBits) )
101             mods.add(Modifier.STRICTFP);
102         if( org.eclipse.jdt.core.dom.Modifier.isSynchronized(modBits) )
103             mods.add(Modifier.SYNCHRONIZED);
104         if( org.eclipse.jdt.core.dom.Modifier.isTransient(modBits) )
105             mods.add(Modifier.TRANSIENT);
106         if( org.eclipse.jdt.core.dom.Modifier.isVolatile(modBits) )
107             mods.add(Modifier.VOLATILE);
108         return mods;
109         
110     }
111     
112     public <A extends Annotation JavaDoc> A getAnnotation(Class JavaDoc<A> annotationClass)
113     {
114         final IAnnotationBinding[] instances = getAnnotationInstancesFromAST();
115         return _getAnnotation(annotationClass, instances);
116     }
117
118     public Collection JavaDoc<AnnotationMirror> getAnnotationMirrors()
119     {
120         final IAnnotationBinding[] instances = getAnnotationInstancesFromAST();
121         return _getAnnotationMirrors(instances);
122     }
123     
124     @SuppressWarnings JavaDoc("unchecked") // DOM AST API returns raw collections
125
private IAnnotationBinding[] getAnnotationInstancesFromAST()
126     {
127         IAnnotationBinding[] instances = null;
128         List JavaDoc extendsMods = null;
129         switch( _astNode.getNodeType() )
130         {
131         case ASTNode.TYPE_DECLARATION:
132         case ASTNode.ANNOTATION_TYPE_DECLARATION:
133         case ASTNode.ENUM_DECLARATION:
134         case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
135         case ASTNode.METHOD_DECLARATION:
136         case ASTNode.FIELD_DECLARATION:
137         case ASTNode.ENUM_CONSTANT_DECLARATION:
138             extendsMods = ((BodyDeclaration)_astNode).modifiers();
139             break;
140             
141         case ASTNode.SINGLE_VARIABLE_DECLARATION:
142             extendsMods = ((SingleVariableDeclaration)_astNode).modifiers();
143             break;
144         case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
145             final ASTNode parent = ((VariableDeclarationFragment)_astNode).getParent();
146             if( parent instanceof BodyDeclaration )
147                 extendsMods = ((BodyDeclaration)parent).modifiers();
148             break;
149             
150         default:
151             throw new IllegalStateException JavaDoc();
152         }
153         if( extendsMods != null ){
154             int count = 0;
155             for( Object JavaDoc obj : extendsMods ){
156                 final IExtendedModifier extMod = (IExtendedModifier)obj;
157                 if( extMod.isAnnotation() )
158                     count ++;
159             }
160             instances = new IAnnotationBinding[count];
161             int index = 0;
162             for( Object JavaDoc obj : extendsMods ){
163                 final IExtendedModifier extMod = (IExtendedModifier)obj;
164                 if( extMod.isAnnotation() )
165                     instances[index ++] =
166                         ((org.eclipse.jdt.core.dom.Annotation)extMod).resolveAnnotationBinding();
167             }
168         }
169         return instances;
170     }
171     
172     public boolean isFromSource(){ return true; }
173     
174     ASTNode getAstNode(){
175         return _astNode;
176     }
177
178     CompilationUnit getCompilationUnit(){
179         return (CompilationUnit)_astNode.getRoot();
180     }
181
182     public IFile getResource(){
183         return _file;
184     }
185     
186     public SourcePosition getPosition()
187     {
188         final ASTNode node = getRangeNode();
189         if( node == null ) return null;
190         final CompilationUnit unit = getCompilationUnit();
191         final int start = node.getStartPosition();
192         return new SourcePositionImpl(
193                 start,
194                 node.getLength(),
195                 unit.getLineNumber(start),
196                 unit.getColumnNumber(start),
197                 this);
198     
199     }
200     
201     public boolean isBindingBased(){ return false; }
202     
203     public boolean equals(Object JavaDoc obj)
204     {
205         if(obj instanceof ASTBasedDeclarationImpl)
206             return _astNode == ((ASTBasedDeclarationImpl)obj)._astNode;
207             
208         return false;
209     }
210     
211     public int hashCode(){
212         return _astNode.hashCode();
213     }
214 }
215
Popular Tags