1 11 12 package org.eclipse.jdt.apt.core.internal.declaration; 13 14 import java.lang.annotation.Annotation ; 15 import java.lang.reflect.Proxy ; 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.Collections ; 19 import java.util.List ; 20 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.jdt.apt.core.internal.env.AnnotationInvocationHandler; 23 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv; 24 import org.eclipse.jdt.apt.core.internal.util.Factory; 25 import org.eclipse.jdt.core.dom.ASTNode; 26 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 27 import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration; 28 import org.eclipse.jdt.core.dom.BodyDeclaration; 29 import org.eclipse.jdt.core.dom.CompilationUnit; 30 import org.eclipse.jdt.core.dom.EnumConstantDeclaration; 31 import org.eclipse.jdt.core.dom.FieldDeclaration; 32 import org.eclipse.jdt.core.dom.IAnnotationBinding; 33 import org.eclipse.jdt.core.dom.ITypeBinding; 34 import org.eclipse.jdt.core.dom.Javadoc; 35 import org.eclipse.jdt.core.dom.MethodDeclaration; 36 import org.eclipse.jdt.core.dom.SimpleName; 37 import org.eclipse.jdt.core.dom.SingleVariableDeclaration; 38 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 39 40 import com.sun.mirror.declaration.AnnotationMirror; 41 import com.sun.mirror.declaration.Declaration; 42 import com.sun.mirror.util.DeclarationVisitor; 43 44 public abstract class EclipseDeclarationImpl implements Declaration, EclipseMirrorObject 45 { 46 final BaseProcessorEnv _env; 47 48 EclipseDeclarationImpl(final BaseProcessorEnv env) 49 { 50 assert env != null : "missing environment"; _env = env; 52 } 53 54 public void accept(DeclarationVisitor visitor) 55 { 56 visitor.visitDeclaration(this); 57 } 58 59 @SuppressWarnings ("unchecked") 60 <A extends Annotation > A _getAnnotation(Class <A> annotationClass, 61 IAnnotationBinding[] annoInstances) 62 { 63 if( annoInstances == null || annoInstances.length == 0 || annotationClass == null ) 64 return null; 65 66 String annoTypeName = annotationClass.getName(); 67 if( annoTypeName == null ) return null; 68 annoTypeName = annoTypeName.replace('$', '.'); 69 for( IAnnotationBinding annoInstance : annoInstances){ 70 if (annoInstance == null) 71 continue; 72 final ITypeBinding binding = annoInstance.getAnnotationType(); 73 if( binding != null && binding.isAnnotation() ){ 74 final String curTypeName = binding.getQualifiedName(); 75 if( annoTypeName.equals(curTypeName) ){ 76 final AnnotationMirrorImpl annoMirror = 77 (AnnotationMirrorImpl)Factory.createAnnotationMirror(annoInstance, this, _env); 78 final AnnotationInvocationHandler handler = new AnnotationInvocationHandler(annoMirror, annotationClass); 79 return (A)Proxy.newProxyInstance(annotationClass.getClassLoader(), 80 new Class []{ annotationClass }, handler ); 81 } 82 } 83 } 84 return null; 85 } 86 87 Collection <AnnotationMirror> _getAnnotationMirrors(IAnnotationBinding[] annoInstances) 88 { 89 if( annoInstances == null || annoInstances.length == 0 ) 90 return Collections.emptyList(); 91 final List <AnnotationMirror> result = new ArrayList <AnnotationMirror>(annoInstances.length); 92 for(IAnnotationBinding annoInstance : annoInstances){ 93 if (annoInstance != null) { 94 final AnnotationMirrorImpl annoMirror = 95 (AnnotationMirrorImpl)Factory.createAnnotationMirror(annoInstance, this, _env); 96 result.add(annoMirror); 97 } 98 } 99 return result; 100 } 101 102 Collection <AnnotationMirror> _getAnnotationMirrors(List <org.eclipse.jdt.core.dom.Annotation> annoInstances) 103 { 104 if( annoInstances == null || annoInstances.size() == 0 ) return Collections.emptyList(); 105 final List <AnnotationMirror> result = new ArrayList <AnnotationMirror>(annoInstances.size()); 106 for( org.eclipse.jdt.core.dom.Annotation annoInstance : annoInstances){ 107 if (annoInstance != null) { 108 final AnnotationMirrorImpl annoMirror = 109 (AnnotationMirrorImpl)Factory.createAnnotationMirror(annoInstance.resolveAnnotationBinding(), this, _env); 110 result.add(annoMirror); 111 } 112 } 113 return result; 114 } 115 116 121 abstract ASTNode getAstNode(); 122 123 128 abstract CompilationUnit getCompilationUnit(); 129 130 133 abstract public IFile getResource(); 134 135 139 public abstract boolean isFromSource(); 140 141 public abstract boolean isBindingBased(); 142 143 public BaseProcessorEnv getEnvironment(){ return _env; } 144 145 150 protected ASTNode getRangeNode() 151 { 152 final ASTNode node = getAstNode(); 153 if( node == null ) return null; 154 SimpleName name = null; 155 switch( node.getNodeType() ) 156 { 157 case ASTNode.TYPE_DECLARATION: 158 case ASTNode.ANNOTATION_TYPE_DECLARATION: 159 case ASTNode.ENUM_DECLARATION: 160 name = ((AbstractTypeDeclaration)node).getName(); 161 break; 162 case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION: 163 name = ((AnnotationTypeMemberDeclaration)node).getName(); 164 break; 165 case ASTNode.METHOD_DECLARATION: 166 name = ((MethodDeclaration)node).getName(); 167 break; 168 case ASTNode.SINGLE_VARIABLE_DECLARATION: 169 name = ((SingleVariableDeclaration)node).getName(); 170 break; 171 case ASTNode.FIELD_DECLARATION: 172 final String declName = getSimpleName(); 173 if( declName == null ) return node; 174 for(Object obj : ((FieldDeclaration)node).fragments() ){ 175 VariableDeclarationFragment frag = (VariableDeclarationFragment)obj; 176 if( declName.equals(frag.getName()) ){ 177 name = frag.getName(); 178 break; 179 } 180 } 181 break; 182 case ASTNode.ENUM_CONSTANT_DECLARATION: 183 name = ((EnumConstantDeclaration)node).getName(); 184 break; 185 default: 186 return node; 187 } 188 if( name == null ) return node; 189 return name; 190 } 191 192 protected String getDocComment(final BodyDeclaration decl) 193 { 194 final Javadoc javaDoc = decl.getJavadoc(); 195 if( javaDoc == null ) return ""; return javaDoc.toString(); 197 } 198 } 199 | Popular Tags |