KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
15
16 import com.sun.mirror.declaration.AnnotationValue;
17 import com.sun.mirror.util.SourcePosition;
18
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
21 import org.eclipse.jdt.apt.core.internal.util.SourcePositionImpl;
22 import org.eclipse.jdt.core.dom.ASTNode;
23 import org.eclipse.jdt.core.dom.ArrayInitializer;
24 import org.eclipse.jdt.core.dom.CompilationUnit;
25
26 public class AnnotationValueImpl implements EclipseMirrorObject, AnnotationValue
27 {
28     /**
29      * Either the annotation that directly contains this annotation value
30      * or an annotation method, which indicates that this is its default value.
31      */

32     private EclipseMirrorObject _parent;
33     private final BaseProcessorEnv _env;
34     /** the annotation value */
35     private final Object JavaDoc _value;
36     /**
37      * The name of the element if this is a value from an annotation member value.
38      * <code>null</code> otherwise
39      */

40     private final String JavaDoc _name;
41     /**
42      * If this is part of an array, then the index into the array.
43      * <code>-1</code> when this doesn't apply
44      */

45     private final int _index;
46
47     /**
48      *
49      * @param value the default value of an annotation element declaration
50      * @param element the annotation element declaration.
51      * @param index zero-based index into the array if the this value is an array element.
52      * <code>-1</code> otherwise.
53      * @param env
54      */

55     public AnnotationValueImpl( final Object JavaDoc value,
56                                 final int index,
57                                 final AnnotationElementDeclarationImpl element,
58                                 final BaseProcessorEnv env)
59     {
60     
61         _value = value;
62         _env = env;
63         _parent = element;
64         _name = null;
65         _index = index;
66         assert _env != null : "missing environment"; //$NON-NLS-1$
67
assert _parent != null : "missing element"; //$NON-NLS-1$
68
}
69     
70     /**
71      *
72      * @param value the annotation value
73      * @param name the name of the element member
74      * @param index zero-based index into the array if the this value is an array element.
75      * <code>-1</code> otherwise.
76      * @param annotation the annotation containing this value
77      * @param env
78      */

79     public AnnotationValueImpl( final Object JavaDoc value,
80                                 final String JavaDoc name,
81                                 final int index,
82                                 final AnnotationMirrorImpl annotation,
83                                 final BaseProcessorEnv env)
84     {
85         assert value != null : "value is null"; //$NON-NLS-1$
86
_value = value;
87         _env = env;
88         _parent = annotation;
89         _name = name;
90         _index = index;
91         assert _env != null : "missing environment"; //$NON-NLS-1$
92
assert _parent != null : "missing element"; //$NON-NLS-1$
93
}
94     
95     @SuppressWarnings JavaDoc("unchecked") // DOM AST API returns raw collections
96
public SourcePosition getPosition()
97     {
98         final MirrorKind kind = _parent.kind();
99         ASTNode astNode = null;
100         switch(kind)
101         {
102         case ANNOTATION_MIRROR:
103             final AnnotationMirrorImpl anno = (AnnotationMirrorImpl)_parent;
104             astNode = anno.getASTNodeForElement(_name);
105             break;
106         case ANNOTATION_ELEMENT:
107             final AnnotationElementDeclarationImpl element = (AnnotationElementDeclarationImpl)_parent;
108             astNode = element.getAstNodeForDefault();
109         default:
110             throw new IllegalStateException JavaDoc(); // should never reach this point.
111
}
112         // did not come from source.
113
if( astNode == null )
114             return null;
115         if( _index >= 0 && astNode.getNodeType() == ASTNode.ARRAY_INITIALIZER ){
116             final ArrayInitializer arrayInit = (ArrayInitializer)astNode;
117             final List JavaDoc exprs = arrayInit.expressions();
118             if (exprs != null && _index < exprs.size() )
119                 astNode = (ASTNode)exprs.get(_index);
120         }
121         if( astNode == null ) return null;
122         
123         final CompilationUnit unit = getCompilationUnit();
124         if( unit == null ) return null;
125         final int offset = astNode.getStartPosition();
126         return new SourcePositionImpl(astNode.getStartPosition(),
127                                       astNode.getLength(),
128                                       unit.getLineNumber(offset),
129                                       unit.getColumnNumber(offset),
130                                       this);
131     }
132     
133     CompilationUnit getCompilationUnit()
134     {
135         final MirrorKind kind = _parent.kind();
136         switch(kind)
137         {
138         case ANNOTATION_MIRROR:
139             return ((AnnotationMirrorImpl)_parent).getCompilationUnit();
140         case ANNOTATION_ELEMENT:
141             if( ((EclipseDeclarationImpl)_parent).isBindingBased() )
142                 return ((AnnotationElementDeclarationImpl)_parent).getCompilationUnit();
143             else
144                 return ((ASTBasedAnnotationElementDeclarationImpl)_parent).getCompilationUnit();
145         default:
146             throw new IllegalStateException JavaDoc(); // should never reach this point.
147
}
148     }
149     
150     public boolean isFromSource()
151     {
152         final MirrorKind kind = _parent.kind();
153         switch(kind)
154         {
155         case ANNOTATION_MIRROR:
156             return ((AnnotationMirrorImpl)_parent).isFromSource();
157         case ANNOTATION_ELEMENT:
158             if( ((EclipseDeclarationImpl)_parent).isBindingBased() )
159                 return ((AnnotationElementDeclarationImpl)_parent).isFromSource();
160             else
161                 return ((ASTBasedAnnotationElementDeclarationImpl)_parent).isFromSource();
162         default:
163             throw new IllegalStateException JavaDoc(); // should never reach this point.
164
}
165     }
166     
167     public IFile getResource()
168     {
169         final MirrorKind kind = _parent.kind();
170         switch(kind)
171         {
172         case ANNOTATION_MIRROR:
173             return ((AnnotationMirrorImpl)_parent).getResource();
174         case ANNOTATION_ELEMENT:
175             if( ((EclipseDeclarationImpl)_parent).isBindingBased() )
176                 return ((AnnotationElementDeclarationImpl)_parent).getResource();
177             else
178                 return ((ASTBasedAnnotationElementDeclarationImpl)_parent).getResource();
179         default:
180             throw new IllegalStateException JavaDoc(); // should never reach this point.
181
}
182     }
183
184     public Object JavaDoc getValue(){ return _value; }
185
186     public MirrorKind kind(){ return MirrorKind.ANNOTATION_VALUE; }
187     
188     public BaseProcessorEnv getEnvironment(){
189         return _env;
190     }
191     
192     public String JavaDoc toString(){ return _value == null ? "" : _value.toString(); } //$NON-NLS-1$
193
}
194
Popular Tags