KickJava   Java API By Example, From Geeks To Geeks.

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


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
12  package org.eclipse.jdt.apt.core.internal.declaration;
13
14 import java.lang.annotation.Annotation JavaDoc;
15 import java.lang.reflect.Proxy JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.jdt.apt.core.internal.env.AnnotationInvocationHandler;
23 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
24 import org.eclipse.jdt.apt.core.internal.util.Factory;
25 import org.eclipse.jdt.core.dom.ASTNode;
26 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
27 import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
28 import org.eclipse.jdt.core.dom.BodyDeclaration;
29 import org.eclipse.jdt.core.dom.CompilationUnit;
30 import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
31 import org.eclipse.jdt.core.dom.FieldDeclaration;
32 import org.eclipse.jdt.core.dom.IAnnotationBinding;
33 import org.eclipse.jdt.core.dom.ITypeBinding;
34 import org.eclipse.jdt.core.dom.Javadoc;
35 import org.eclipse.jdt.core.dom.MethodDeclaration;
36 import org.eclipse.jdt.core.dom.SimpleName;
37 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
38 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
39
40 import com.sun.mirror.declaration.AnnotationMirror;
41 import com.sun.mirror.declaration.Declaration;
42 import com.sun.mirror.util.DeclarationVisitor;
43
44 public abstract class EclipseDeclarationImpl implements Declaration, EclipseMirrorObject
45 {
46     final BaseProcessorEnv _env;
47
48     EclipseDeclarationImpl(final BaseProcessorEnv env)
49     {
50         assert env != null : "missing environment"; //$NON-NLS-1$
51
_env = env;
52     }
53
54     public void accept(DeclarationVisitor visitor)
55     {
56         visitor.visitDeclaration(this);
57     }
58
59     @SuppressWarnings JavaDoc("unchecked")
60     <A extends Annotation JavaDoc> A _getAnnotation(Class JavaDoc<A> annotationClass,
61                                             IAnnotationBinding[] annoInstances)
62     {
63         if( annoInstances == null || annoInstances.length == 0 || annotationClass == null )
64             return null;
65         
66         String JavaDoc annoTypeName = annotationClass.getName();
67         if( annoTypeName == null ) return null;
68         annoTypeName = annoTypeName.replace('$', '.');
69         for( IAnnotationBinding annoInstance : annoInstances){
70             if (annoInstance == null)
71                 continue;
72             final ITypeBinding binding = annoInstance.getAnnotationType();
73             if( binding != null && binding.isAnnotation() ){
74                 final String JavaDoc curTypeName = binding.getQualifiedName();
75                 if( annoTypeName.equals(curTypeName) ){
76                     final AnnotationMirrorImpl annoMirror =
77                         (AnnotationMirrorImpl)Factory.createAnnotationMirror(annoInstance, this, _env);
78                     final AnnotationInvocationHandler handler = new AnnotationInvocationHandler(annoMirror, annotationClass);
79                     return (A)Proxy.newProxyInstance(annotationClass.getClassLoader(),
80                                                      new Class JavaDoc[]{ annotationClass }, handler );
81                 }
82             }
83         }
84         return null;
85     }
86
87     Collection JavaDoc<AnnotationMirror> _getAnnotationMirrors(IAnnotationBinding[] annoInstances)
88     {
89         if( annoInstances == null || annoInstances.length == 0 )
90             return Collections.emptyList();
91         final List JavaDoc<AnnotationMirror> result = new ArrayList JavaDoc<AnnotationMirror>(annoInstances.length);
92         for(IAnnotationBinding annoInstance : annoInstances){
93             if (annoInstance != null) {
94                 final AnnotationMirrorImpl annoMirror =
95                         (AnnotationMirrorImpl)Factory.createAnnotationMirror(annoInstance, this, _env);
96                 result.add(annoMirror);
97             }
98         }
99         return result;
100     }
101     
102     Collection JavaDoc<AnnotationMirror> _getAnnotationMirrors(List JavaDoc<org.eclipse.jdt.core.dom.Annotation> annoInstances)
103     {
104         if( annoInstances == null || annoInstances.size() == 0 ) return Collections.emptyList();
105         final List JavaDoc<AnnotationMirror> result = new ArrayList JavaDoc<AnnotationMirror>(annoInstances.size());
106         for( org.eclipse.jdt.core.dom.Annotation annoInstance : annoInstances){
107             if (annoInstance != null) {
108                 final AnnotationMirrorImpl annoMirror =
109                     (AnnotationMirrorImpl)Factory.createAnnotationMirror(annoInstance.resolveAnnotationBinding(), this, _env);
110                 result.add(annoMirror);
111             }
112         }
113         return result;
114     }
115     
116     /**
117      * @return the ast node that corresponding to this declaration,
118      * or null if this declaration came from binary.
119      * @see #isFromSource()
120      */

121     abstract ASTNode getAstNode();
122
123     /**
124      * @return the compilation unit that the ast node of this declaration came from
125      * Return null if this declaration came from binary.
126      * @see #isFromSource()
127      */

128     abstract CompilationUnit getCompilationUnit();
129     
130     /**
131      * @return the resource of this declaration if the declaration is from source.
132      */

133     abstract public IFile getResource();
134     
135     /**
136      * @return true iff this declaration came from a source file.
137      * Return false otherwise.
138      */

139     public abstract boolean isFromSource();
140     
141     public abstract boolean isBindingBased();
142     
143     public BaseProcessorEnv getEnvironment(){ return _env; }
144     
145     /**
146      * @return the ast node that holds the range of this member declaration in source.
147      * The default is to find the name of the node and if that fails, return the
148      * node with the smallest range that contains the declaration.
149      */

150     protected ASTNode getRangeNode()
151     {
152         final ASTNode node = getAstNode();
153         if( node == null ) return null;
154         SimpleName name = null;
155         switch( node.getNodeType() )
156         {
157         case ASTNode.TYPE_DECLARATION:
158         case ASTNode.ANNOTATION_TYPE_DECLARATION:
159         case ASTNode.ENUM_DECLARATION:
160             name = ((AbstractTypeDeclaration)node).getName();
161             break;
162         case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
163             name = ((AnnotationTypeMemberDeclaration)node).getName();
164             break;
165         case ASTNode.METHOD_DECLARATION:
166             name = ((MethodDeclaration)node).getName();
167             break;
168         case ASTNode.SINGLE_VARIABLE_DECLARATION:
169             name = ((SingleVariableDeclaration)node).getName();
170             break;
171         case ASTNode.FIELD_DECLARATION:
172             final String JavaDoc declName = getSimpleName();
173             if( declName == null ) return node;
174             for(Object JavaDoc obj : ((FieldDeclaration)node).fragments() ){
175                  VariableDeclarationFragment frag = (VariableDeclarationFragment)obj;
176                  if( declName.equals(frag.getName()) ){
177                      name = frag.getName();
178                      break;
179                  }
180             }
181             break;
182         case ASTNode.ENUM_CONSTANT_DECLARATION:
183             name = ((EnumConstantDeclaration)node).getName();
184             break;
185         default:
186             return node;
187         }
188         if( name == null ) return node;
189         return name;
190     }
191     
192     protected String JavaDoc getDocComment(final BodyDeclaration decl)
193     {
194         final Javadoc javaDoc = decl.getJavadoc();
195         if( javaDoc == null ) return ""; //$NON-NLS-1$
196
return javaDoc.toString();
197     }
198 }
199
Popular Tags