1 11 package org.eclipse.jdt.apt.core.internal.declaration; 12 13 import java.lang.annotation.Annotation ; 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv; 20 import org.eclipse.jdt.apt.core.internal.util.SourcePositionImpl; 21 import org.eclipse.jdt.core.dom.ASTNode; 22 import org.eclipse.jdt.core.dom.BodyDeclaration; 23 import org.eclipse.jdt.core.dom.CompilationUnit; 24 import org.eclipse.jdt.core.dom.IExtendedModifier; 25 import org.eclipse.jdt.core.dom.IAnnotationBinding; 26 import org.eclipse.jdt.core.dom.SingleVariableDeclaration; 27 import org.eclipse.jdt.core.dom.VariableDeclaration; 28 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 29 30 import com.sun.mirror.declaration.AnnotationMirror; 31 import com.sun.mirror.declaration.Modifier; 32 import com.sun.mirror.util.SourcePosition; 33 34 43 public abstract class ASTBasedDeclarationImpl extends EclipseDeclarationImpl { 44 45 static final String EMPTY_STRING = ""; 47 protected final ASTNode _astNode; 48 49 protected final IFile _file; 50 public ASTBasedDeclarationImpl( 51 ASTNode astNode, 52 IFile file, 53 BaseProcessorEnv env) 54 { 55 super(env); 56 assert astNode != null : "ast node cannot be missing"; assert file != null : "file cannot be missing"; 59 assert astNode instanceof BodyDeclaration || 60 astNode instanceof VariableDeclaration : 61 "ast node must be either a body declaration or a variable declaration"; 63 _astNode = astNode; 64 _file = file; 65 } 66 67 public Collection <Modifier> getModifiers() 68 { 69 int modBits = 0; 70 if( _astNode instanceof BodyDeclaration ) 71 modBits = ((BodyDeclaration)_astNode).getModifiers(); 72 else if( _astNode instanceof SingleVariableDeclaration ) 73 modBits = ((SingleVariableDeclaration)_astNode).getModifiers(); 74 else{ 75 ASTNode parent = ((VariableDeclarationFragment)_astNode).getParent(); 76 if( _astNode instanceof BodyDeclaration ) 77 modBits = ((BodyDeclaration)parent).getModifiers(); 78 } 79 80 return getModifiers(modBits); 81 } 82 83 private Collection <Modifier> getModifiers(int modBits) 84 { 85 final List <Modifier> mods = new ArrayList <Modifier>(4); 86 if( org.eclipse.jdt.core.dom.Modifier.isAbstract(modBits) ) 87 mods.add(Modifier.ABSTRACT); 88 if( org.eclipse.jdt.core.dom.Modifier.isFinal(modBits) ) 89 mods.add(Modifier.FINAL); 90 if( org.eclipse.jdt.core.dom.Modifier.isNative(modBits) ) 91 mods.add(Modifier.NATIVE); 92 if( org.eclipse.jdt.core.dom.Modifier.isPrivate(modBits) ) 93 mods.add(Modifier.PRIVATE); 94 if( org.eclipse.jdt.core.dom.Modifier.isProtected(modBits) ) 95 mods.add(Modifier.PROTECTED); 96 if( org.eclipse.jdt.core.dom.Modifier.isPublic(modBits) ) 97 mods.add(Modifier.PUBLIC); 98 if( org.eclipse.jdt.core.dom.Modifier.isStatic(modBits) ) 99 mods.add(Modifier.STATIC); 100 if( org.eclipse.jdt.core.dom.Modifier.isStrictfp(modBits) ) 101 mods.add(Modifier.STRICTFP); 102 if( org.eclipse.jdt.core.dom.Modifier.isSynchronized(modBits) ) 103 mods.add(Modifier.SYNCHRONIZED); 104 if( org.eclipse.jdt.core.dom.Modifier.isTransient(modBits) ) 105 mods.add(Modifier.TRANSIENT); 106 if( org.eclipse.jdt.core.dom.Modifier.isVolatile(modBits) ) 107 mods.add(Modifier.VOLATILE); 108 return mods; 109 110 } 111 112 public <A extends Annotation > A getAnnotation(Class <A> annotationClass) 113 { 114 final IAnnotationBinding[] instances = getAnnotationInstancesFromAST(); 115 return _getAnnotation(annotationClass, instances); 116 } 117 118 public Collection <AnnotationMirror> getAnnotationMirrors() 119 { 120 final IAnnotationBinding[] instances = getAnnotationInstancesFromAST(); 121 return _getAnnotationMirrors(instances); 122 } 123 124 @SuppressWarnings ("unchecked") private IAnnotationBinding[] getAnnotationInstancesFromAST() 126 { 127 IAnnotationBinding[] instances = null; 128 List extendsMods = null; 129 switch( _astNode.getNodeType() ) 130 { 131 case ASTNode.TYPE_DECLARATION: 132 case ASTNode.ANNOTATION_TYPE_DECLARATION: 133 case ASTNode.ENUM_DECLARATION: 134 case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION: 135 case ASTNode.METHOD_DECLARATION: 136 case ASTNode.FIELD_DECLARATION: 137 case ASTNode.ENUM_CONSTANT_DECLARATION: 138 extendsMods = ((BodyDeclaration)_astNode).modifiers(); 139 break; 140 141 case ASTNode.SINGLE_VARIABLE_DECLARATION: 142 extendsMods = ((SingleVariableDeclaration)_astNode).modifiers(); 143 break; 144 case ASTNode.VARIABLE_DECLARATION_FRAGMENT: 145 final ASTNode parent = ((VariableDeclarationFragment)_astNode).getParent(); 146 if( parent instanceof BodyDeclaration ) 147 extendsMods = ((BodyDeclaration)parent).modifiers(); 148 break; 149 150 default: 151 throw new IllegalStateException (); 152 } 153 if( extendsMods != null ){ 154 int count = 0; 155 for( Object obj : extendsMods ){ 156 final IExtendedModifier extMod = (IExtendedModifier)obj; 157 if( extMod.isAnnotation() ) 158 count ++; 159 } 160 instances = new IAnnotationBinding[count]; 161 int index = 0; 162 for( Object obj : extendsMods ){ 163 final IExtendedModifier extMod = (IExtendedModifier)obj; 164 if( extMod.isAnnotation() ) 165 instances[index ++] = 166 ((org.eclipse.jdt.core.dom.Annotation)extMod).resolveAnnotationBinding(); 167 } 168 } 169 return instances; 170 } 171 172 public boolean isFromSource(){ return true; } 173 174 ASTNode getAstNode(){ 175 return _astNode; 176 } 177 178 CompilationUnit getCompilationUnit(){ 179 return (CompilationUnit)_astNode.getRoot(); 180 } 181 182 public IFile getResource(){ 183 return _file; 184 } 185 186 public SourcePosition getPosition() 187 { 188 final ASTNode node = getRangeNode(); 189 if( node == null ) return null; 190 final CompilationUnit unit = getCompilationUnit(); 191 final int start = node.getStartPosition(); 192 return new SourcePositionImpl( 193 start, 194 node.getLength(), 195 unit.getLineNumber(start), 196 unit.getColumnNumber(start), 197 this); 198 199 } 200 201 public boolean isBindingBased(){ return false; } 202 203 public boolean equals(Object obj) 204 { 205 if(obj instanceof ASTBasedDeclarationImpl) 206 return _astNode == ((ASTBasedDeclarationImpl)obj)._astNode; 207 208 return false; 209 } 210 211 public int hashCode(){ 212 return _astNode.hashCode(); 213 } 214 } 215 | Popular Tags |