KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
18 import org.eclipse.jdt.apt.core.internal.util.Factory;
19 import org.eclipse.jdt.core.dom.ASTNode;
20 import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
21 import org.eclipse.jdt.core.dom.Expression;
22 import org.eclipse.jdt.core.dom.ITypeBinding;
23 import org.eclipse.jdt.core.dom.SimpleName;
24 import org.eclipse.jdt.core.dom.Type;
25 import com.sun.mirror.declaration.AnnotationTypeDeclaration;
26 import com.sun.mirror.declaration.AnnotationTypeElementDeclaration;
27 import com.sun.mirror.declaration.AnnotationValue;
28 import com.sun.mirror.declaration.ParameterDeclaration;
29 import com.sun.mirror.type.TypeMirror;
30 import com.sun.mirror.util.DeclarationVisitor;
31
32 public class ASTBasedAnnotationElementDeclarationImpl
33     extends ASTBasedMethodDeclarationImpl implements AnnotationTypeElementDeclaration{
34     
35     public ASTBasedAnnotationElementDeclarationImpl(
36             final AnnotationTypeMemberDeclaration astNode,
37             final IFile file,
38             final BaseProcessorEnv env)
39     {
40         super(astNode, file, env);
41     }
42
43     public void accept(DeclarationVisitor visitor) {
44         visitor.visitAnnotationTypeElementDeclaration(this);
45     }
46
47     public AnnotationTypeDeclaration getDeclaringType() {
48         return (AnnotationTypeDeclaration) super.getDeclaringType();
49     }
50
51     /**
52      * @return the default value of this annotation element if one exists.
53      * Return null if the annotation element is defined in binary
54      * (feature not available right now). Return null if the annotation
55      * element is part of a seconary type that is defined outside the
56      * file associated with the environment.
57      */

58     public AnnotationValue getDefaultValue() {
59         
60         final AnnotationTypeMemberDeclaration decl = getMemberAstNode();
61         if (decl != null){
62             final Expression defaultExpr = decl.getDefault();
63             if( defaultExpr != null )
64                 return Factory.createDefaultValue(defaultExpr.resolveConstantExpressionValue(), this, _env);
65         }
66
67         return null;
68     }
69
70     public ASTNode getAstNodeForDefault() {
71         final AnnotationTypeMemberDeclaration decl = (AnnotationTypeMemberDeclaration) getAstNode();
72         if (decl != null)
73             return decl.getDefault();
74
75         return null;
76     }
77     
78     public boolean isVarArgs(){ return false; }
79
80     public String JavaDoc getSimpleName()
81     {
82         final AnnotationTypeMemberDeclaration memberAstNode = getMemberAstNode();
83         final SimpleName nameNode = memberAstNode.getName();
84         return nameNode == null ? EMPTY_STRING : nameNode.getIdentifier();
85     }
86     
87     public TypeMirror getReturnType()
88     {
89         final AnnotationTypeMemberDeclaration memberAstNode = getMemberAstNode();
90         final Type retType = memberAstNode.getType();
91         // some funny error case where the return type is missing but it's not a constructor.
92
if( retType == null )
93             return Factory.createErrorClassType(EMPTY_STRING);
94         final ITypeBinding typeBinding = retType.resolveBinding();
95         // This is most likely the reason that we end up with an ast based implementation.
96
if( typeBinding == null ){
97             return Factory.createErrorClassType(retType.toString());
98         }
99         else{
100             final TypeMirror type = Factory.createTypeMirror(typeBinding, _env);
101             if(type == null )
102                 return Factory.createErrorClassType(retType.toString());
103             return type;
104         }
105     }
106     
107     public String JavaDoc toString()
108     {
109         final StringBuilder JavaDoc buffer = new StringBuilder JavaDoc();
110         final AnnotationTypeMemberDeclaration memberAstNode = (AnnotationTypeMemberDeclaration) getAstNode();
111
112         if( memberAstNode.getType() != null )
113             buffer.append(memberAstNode.getType());
114         buffer.append(' ');
115         buffer.append(memberAstNode.getName());
116         buffer.append("()"); //$NON-NLS-1$
117

118         return buffer.toString();
119     }
120
121     public Collection JavaDoc<ParameterDeclaration> getParameters() {
122         return Collections.emptyList();
123     }
124
125     public MirrorKind kind() {
126         return MirrorKind.ANNOTATION_ELEMENT;
127     }
128     
129     private AnnotationTypeMemberDeclaration getMemberAstNode(){
130         return (AnnotationTypeMemberDeclaration)_astNode;
131     }
132 }
133
Popular Tags