1 11 12 package org.eclipse.jdt.internal.compiler.apt.model; 13 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 import java.util.List ; 17 18 import javax.lang.model.element.Element; 19 import javax.lang.model.element.ElementKind; 20 import javax.lang.model.element.ElementVisitor; 21 import javax.lang.model.element.PackageElement; 22 import javax.lang.model.element.TypeParameterElement; 23 import javax.lang.model.type.TypeMirror; 24 25 import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl; 26 import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding; 27 import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment; 28 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding; 29 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; 30 import org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding; 31 32 35 public class TypeParameterElementImpl extends ElementImpl implements TypeParameterElement 36 { 37 private final Element _declaringElement; 38 39 private List <? extends TypeMirror> _bounds = null; 41 42 TypeParameterElementImpl(BaseProcessingEnvImpl env, TypeVariableBinding binding, Element declaringElement) { 43 super(env, binding); 44 _declaringElement = declaringElement; 45 } 46 47 TypeParameterElementImpl(BaseProcessingEnvImpl env, TypeVariableBinding binding) { 48 super(env, binding); 49 _declaringElement = _env.getFactory().newElement(binding.declaringElement); 50 } 51 52 @Override 53 public List <? extends TypeMirror> getBounds() 54 { 55 if (null == _bounds) { 56 _bounds = calculateBounds(); 57 } 58 return _bounds; 59 } 60 61 private List <? extends TypeMirror> calculateBounds() { 63 TypeVariableBinding typeVariableBinding = (TypeVariableBinding)_binding; 64 ReferenceBinding varSuperclass = typeVariableBinding.superclass(); 65 TypeBinding firstClassOrArrayBound = typeVariableBinding.firstBound; 66 int boundsLength = 0; 67 if (firstClassOrArrayBound != null) { 68 if (firstClassOrArrayBound == varSuperclass) { 69 boundsLength++; 70 } else if (firstClassOrArrayBound.isArrayType()) { boundsLength++; 72 } else { 73 firstClassOrArrayBound = null; 74 } 75 } 76 ReferenceBinding[] superinterfaces = typeVariableBinding.superInterfaces(); 77 int superinterfacesLength = 0; 78 if (superinterfaces != null) { 79 superinterfacesLength = superinterfaces.length; 80 boundsLength += superinterfacesLength; 81 } 82 List <TypeMirror> typeBounds = new ArrayList <TypeMirror>(boundsLength); 83 if (boundsLength != 0) { 84 if (firstClassOrArrayBound != null) { 85 TypeMirror typeBinding = _env.getFactory().newTypeMirror(firstClassOrArrayBound); 86 if (typeBinding == null) { 87 return Collections.emptyList(); 88 } 89 typeBounds.add(typeBinding); 90 } 91 if (superinterfaces != null) { 92 for (int i = 0; i < superinterfacesLength; i++) { 93 TypeMirror typeBinding = _env.getFactory().newTypeMirror(superinterfaces[i]); 94 if (typeBinding == null) { 95 return Collections.emptyList(); 96 } 97 typeBounds.add(typeBinding); 98 } 99 } 100 } else { 101 typeBounds.add(_env.getFactory().newTypeMirror(_env.getLookupEnvironment().getType(LookupEnvironment.JAVA_LANG_OBJECT))); 103 } 104 return Collections.unmodifiableList(typeBounds); 105 } 106 107 @Override 108 public Element getGenericElement() 109 { 110 return _declaringElement; 111 } 112 113 @Override 114 public <R, P> R accept(ElementVisitor<R, P> v, P p) 115 { 116 return v.visitTypeParameter(this, p); 117 } 118 119 124 @Override 125 protected AnnotationBinding[] getAnnotationBindings() 126 { 127 return null; 128 } 129 130 135 @Override 136 public List <? extends Element> getEnclosedElements() 137 { 138 return Collections.emptyList(); 139 } 140 141 146 @Override 147 public Element getEnclosingElement() 148 { 149 return null; 150 } 151 152 @Override 153 public ElementKind getKind() 154 { 155 return ElementKind.TYPE_PARAMETER; 156 } 157 158 @Override 159 PackageElement getPackage() 160 { 161 return null; 163 } 164 165 @Override 166 public String toString() { 167 return new String (_binding.readableName()); 168 } 169 } 170 | Popular Tags |