1 11 12 package org.eclipse.jdt.apt.core.internal.declaration; 13 14 import java.util.List ; 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 32 private EclipseMirrorObject _parent; 33 private final BaseProcessorEnv _env; 34 35 private final Object _value; 36 40 private final String _name; 41 45 private final int _index; 46 47 55 public AnnotationValueImpl( final Object 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"; assert _parent != null : "missing element"; } 69 70 79 public AnnotationValueImpl( final Object value, 80 final String name, 81 final int index, 82 final AnnotationMirrorImpl annotation, 83 final BaseProcessorEnv env) 84 { 85 assert value != null : "value is null"; _value = value; 87 _env = env; 88 _parent = annotation; 89 _name = name; 90 _index = index; 91 assert _env != null : "missing environment"; assert _parent != null : "missing element"; } 94 95 @SuppressWarnings ("unchecked") 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 (); } 112 if( astNode == null ) 114 return null; 115 if( _index >= 0 && astNode.getNodeType() == ASTNode.ARRAY_INITIALIZER ){ 116 final ArrayInitializer arrayInit = (ArrayInitializer)astNode; 117 final List 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 (); } 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 (); } 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 (); } 182 } 183 184 public Object 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 toString(){ return _value == null ? "" : _value.toString(); } } 194 | Popular Tags |