1 11 package org.eclipse.jdt.internal.compiler.apt.model; 12 13 import java.lang.annotation.Annotation ; 14 import java.lang.reflect.Proxy ; 15 import java.util.Collections ; 16 import java.util.List ; 17 import java.util.Set ; 18 19 import javax.lang.model.element.AnnotationMirror; 20 import javax.lang.model.element.Element; 21 import javax.lang.model.element.Modifier; 22 import javax.lang.model.element.Name; 23 import javax.lang.model.element.PackageElement; 24 import javax.lang.model.type.TypeMirror; 25 import javax.lang.model.util.Elements; 26 27 import org.eclipse.jdt.core.compiler.CharOperation; 28 import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl; 29 import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding; 30 import org.eclipse.jdt.internal.compiler.lookup.Binding; 31 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding; 32 33 37 public abstract class ElementImpl 38 implements javax.lang.model.element.Element, IElementInfo 39 { 40 public final BaseProcessingEnvImpl _env; 41 public final Binding _binding; 42 43 protected ElementImpl(BaseProcessingEnvImpl env, Binding binding) { 44 _env = env; 45 _binding = binding; 46 } 47 48 @Override 49 public TypeMirror asType() { 50 return _env.getFactory().newTypeMirror(_binding); 51 } 52 53 @SuppressWarnings ("unchecked") @Override 55 public <A extends Annotation > A getAnnotation(Class <A> annotationClass) { 56 AnnotationBinding[] annoInstances = getAnnotationBindings(); 57 if( annoInstances == null || annoInstances.length == 0 || annotationClass == null ) 58 return null; 59 60 String annoTypeName = annotationClass.getName(); 61 if( annoTypeName == null ) return null; 62 annoTypeName = annoTypeName.replace('$', '.'); 63 for( AnnotationBinding annoInstance : annoInstances) { 64 if (annoInstance == null) 65 continue; 66 ReferenceBinding binding = annoInstance.getAnnotationType(); 67 if ( binding != null && binding.isAnnotationType() ) { 68 char[] qName; 69 if (binding.isMemberType()) { 70 qName = CharOperation.concatWith(binding.enclosingType().compoundName, binding.sourceName, '.'); 71 CharOperation.replace(qName, '$', '.'); 72 } else { 73 qName = CharOperation.concatWith(binding.compoundName, '.'); 74 } 75 if( annoTypeName.equals(new String (qName)) ){ 76 AnnotationMirrorImpl annoMirror = 77 (AnnotationMirrorImpl)_env.getFactory().newAnnotationMirror(annoInstance); 78 return (A)Proxy.newProxyInstance(annotationClass.getClassLoader(), 79 new Class []{ annotationClass }, annoMirror ); 80 } 81 } 82 } 83 return null; 84 } 85 86 89 protected abstract AnnotationBinding[] getAnnotationBindings(); 90 91 @Override 92 public List <? extends AnnotationMirror> getAnnotationMirrors() { 93 return _env.getFactory().getAnnotationMirrors(getAnnotationBindings()); 94 } 95 96 @Override 97 public Set <Modifier> getModifiers() { 98 return Collections.emptySet(); 101 } 102 103 @Override 104 public Name getSimpleName() { 105 return new NameImpl(_binding.shortReadableName()); 106 } 107 108 @Override 109 public int hashCode() { 110 return _binding.hashCode(); 111 } 112 113 @Override 116 public boolean equals(Object obj) { 117 if (this == obj) 118 return true; 119 if (obj == null) 120 return false; 121 if (getClass() != obj.getClass()) 122 return false; 123 final ElementImpl other = (ElementImpl) obj; 124 if (_binding == null) { 125 if (other._binding != null) 126 return false; 127 } else if (_binding != other._binding) 128 return false; 129 return true; 130 } 131 132 @Override 133 public String toString() { 134 return _binding.toString(); 135 } 136 137 @Override 138 public String getFileName() { 139 return null; 141 } 142 143 147 abstract PackageElement getPackage(); 148 149 155 public boolean hides(Element hidden) 156 { 157 return false; 158 } 159 } 160 | Popular Tags |