KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
15
16 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
17 import org.eclipse.jdt.apt.core.internal.util.SourcePositionImpl;
18 import org.eclipse.jdt.core.dom.*;
19
20 import com.sun.mirror.declaration.AnnotationMirror;
21 import com.sun.mirror.declaration.MemberDeclaration;
22 import com.sun.mirror.util.DeclarationVisitor;
23 import com.sun.mirror.util.SourcePosition;
24
25 public abstract class MemberDeclarationImpl extends DeclarationImpl implements MemberDeclaration, EclipseMirrorObject
26 {
27     MemberDeclarationImpl(final IBinding binding, BaseProcessorEnv env)
28     {
29         super(binding, env);
30     }
31     
32     public void accept(DeclarationVisitor visitor)
33     {
34         visitor.visitMemberDeclaration(this);
35     }
36     
37     public <A extends Annotation JavaDoc> A getAnnotation(Class JavaDoc<A> annotationClass)
38     {
39         final IAnnotationBinding[] instances = getAnnotationInstances();
40         return _getAnnotation(annotationClass, instances);
41     }
42
43     public Collection JavaDoc<AnnotationMirror> getAnnotationMirrors()
44     {
45         final IAnnotationBinding[] instances = getAnnotationInstances();
46         return _getAnnotationMirrors(instances);
47     }
48     
49     private IAnnotationBinding[] getAnnotationInstances()
50     {
51         final IBinding binding = getDeclarationBinding();
52         final IAnnotationBinding[] instances;
53         switch( binding.getKind() )
54         {
55         case IBinding.TYPE:
56             instances = ((ITypeBinding)binding).getAnnotations();
57             break;
58         case IBinding.METHOD:
59             instances = ((IMethodBinding)binding).getAnnotations();
60             break;
61         case IBinding.VARIABLE:
62             instances = ((IVariableBinding)binding).getAnnotations();
63             break;
64         case IBinding.PACKAGE:
65             // TODO: support package annotation
66
return null;
67         default:
68             throw new IllegalStateException JavaDoc();
69         }
70         return instances;
71     }
72
73     public String JavaDoc getDocComment()
74     {
75         if( isFromSource()){
76             final ASTNode node = getAstNode();
77             if(node != null){
78                 if( node instanceof BodyDeclaration )
79                     
80                     return getDocComment((BodyDeclaration)node);
81
82                 else if( node.getNodeType() == ASTNode.VARIABLE_DECLARATION_FRAGMENT ){
83                     final ASTNode parent = node.getParent();
84                     // a field declaration
85
if( parent instanceof BodyDeclaration )
86                         return getDocComment((BodyDeclaration)parent);
87                     
88                 }
89                 return ""; //$NON-NLS-1$
90
}
91         }
92         return null;
93     }
94
95     /**
96      * @return the source position of this declaration.
97      * Return null if this declaration did not come from source or
98      * if the declaration is (or is part of) a secondary type that is defined
99      * outside of the file associated with the environment.
100      */

101     public SourcePosition getPosition()
102     {
103         if( isFromSource() ){
104             final ASTNode node = getRangeNode();
105             if( node == null ) return null;
106             final CompilationUnit unit = getCompilationUnit();
107             final int start = node.getStartPosition();
108             return new SourcePositionImpl(start,
109                     node.getLength(),
110                     unit.getLineNumber(start),
111                     unit.getColumnNumber(start),
112                     this);
113         }
114         return null;
115     }
116 }
117
Popular Tags