1 11 package org.eclipse.jdt.internal.compiler.apt.model; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.List ; 16 import java.util.Set ; 17 18 import javax.lang.model.element.AnnotationValue; 19 import javax.lang.model.element.Element; 20 import javax.lang.model.element.ElementKind; 21 import javax.lang.model.element.ElementVisitor; 22 import javax.lang.model.element.ExecutableElement; 23 import javax.lang.model.element.Modifier; 24 import javax.lang.model.element.Name; 25 import javax.lang.model.element.PackageElement; 26 import javax.lang.model.element.TypeElement; 27 import javax.lang.model.element.TypeParameterElement; 28 import javax.lang.model.element.VariableElement; 29 import javax.lang.model.type.TypeMirror; 30 31 import org.eclipse.jdt.core.compiler.CharOperation; 32 import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl; 33 import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; 34 import org.eclipse.jdt.internal.compiler.ast.Argument; 35 import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding; 36 import org.eclipse.jdt.internal.compiler.lookup.AnnotationHolder; 37 import org.eclipse.jdt.internal.compiler.lookup.BinaryLocalVariableBinding; 38 import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers; 39 import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment; 40 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; 41 import org.eclipse.jdt.internal.compiler.lookup.MethodVerifier; 42 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding; 43 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; 44 import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; 45 import org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding; 46 47 public class ExecutableElementImpl extends ElementImpl implements 48 ExecutableElement { 49 50 private Name _name = null; 51 52 ExecutableElementImpl(BaseProcessingEnvImpl env, MethodBinding binding) { 53 super(env, binding); 54 } 55 56 @Override 57 public <R, P> R accept(ElementVisitor<R, P> v, P p) 58 { 59 return v.visitExecutable(this, p); 60 } 61 62 @Override 63 protected AnnotationBinding[] getAnnotationBindings() 64 { 65 return ((MethodBinding)_binding).getAnnotations(); 66 } 67 68 @Override 69 public AnnotationValue getDefaultValue() { 70 MethodBinding binding = (MethodBinding)_binding; 71 Object defaultValue = binding.getDefaultValue(); 72 if (defaultValue != null) return new AnnotationValueImpl(_env, defaultValue, binding.returnType); 73 return null; 74 } 75 76 @Override 77 public List <? extends Element> getEnclosedElements() { 78 return Collections.emptyList(); 79 } 80 81 @Override 82 public Element getEnclosingElement() { 83 MethodBinding binding = (MethodBinding)_binding; 84 if (null == binding.declaringClass) { 85 return null; 86 } 87 return _env.getFactory().newElement(binding.declaringClass); 88 } 89 90 @Override 91 public String getFileName() { 92 ReferenceBinding dc = ((MethodBinding)_binding).declaringClass; 93 char[] name = dc.getFileName(); 94 if (name == null) 95 return null; 96 return new String (name); 97 } 98 99 @Override 100 public ElementKind getKind() { 101 MethodBinding binding = (MethodBinding)_binding; 102 if (binding.isConstructor()) { 103 return ElementKind.CONSTRUCTOR; 104 } 105 else if (CharOperation.equals(binding.selector, TypeConstants.CLINIT)) { 106 return ElementKind.STATIC_INIT; 107 } 108 else if (CharOperation.equals(binding.selector, TypeConstants.INIT)) { 109 return ElementKind.INSTANCE_INIT; 110 } 111 else { 112 return ElementKind.METHOD; 113 } 114 } 115 116 @Override 117 public Set <Modifier> getModifiers() { 118 MethodBinding binding = (MethodBinding)_binding; 119 return Factory.getModifiers(binding.modifiers, getKind()); 120 } 121 122 @Override 123 PackageElement getPackage() 124 { 125 MethodBinding binding = (MethodBinding)_binding; 126 if (null == binding.declaringClass) { 127 return null; 128 } 129 return _env.getFactory().newPackageElement(binding.declaringClass.fPackage); 130 } 131 132 @Override 133 public List <? extends VariableElement> getParameters() { 134 MethodBinding binding = (MethodBinding)_binding; 135 int length = binding.parameters == null ? 0 : binding.parameters.length; 136 if (0 != length) { 137 AbstractMethodDeclaration methodDeclaration = binding.sourceMethod(); 138 List <VariableElement> params = new ArrayList <VariableElement>(length); 139 if (methodDeclaration != null) { 140 for (Argument argument : methodDeclaration.arguments) { 141 VariableElement param = new VariableElementImpl(_env, argument.binding); 142 params.add(param); 143 } 144 } else { 145 boolean isEnumConstructor = binding.isConstructor() 147 && binding.declaringClass.isEnum() 148 && binding.declaringClass.isBinaryBinding() 149 && ((binding.modifiers & ExtraCompilerModifiers.AccGenericSignature) == 0); 150 AnnotationBinding[][] parameterAnnotationBindings = null; 151 AnnotationHolder annotationHolder = binding.declaringClass.retrieveAnnotationHolder(binding, false); 152 if (annotationHolder != null) { 153 parameterAnnotationBindings = annotationHolder.getParameterAnnotations(); 154 } 155 if (isEnumConstructor) { 157 if (length == 2) { 158 return Collections.emptyList(); 160 } 161 for (int i = 2; i < length; i++) { 162 TypeBinding typeBinding = binding.parameters[i]; 163 StringBuilder builder = new StringBuilder ("arg"); builder.append(i - 2); 165 VariableElement param = new VariableElementImpl(_env, 166 new BinaryLocalVariableBinding( 167 String.valueOf(builder).toCharArray(), 168 typeBinding, 169 0, 170 null)); 171 params.add(param); 172 } 173 } else { 174 int i = 0; 175 for (TypeBinding typeBinding : binding.parameters) { 176 StringBuilder builder = new StringBuilder ("arg"); builder.append(i); 178 VariableElement param = new VariableElementImpl(_env, 179 new BinaryLocalVariableBinding( 180 String.valueOf(builder).toCharArray(), 181 typeBinding, 182 0, 183 parameterAnnotationBindings != null ? parameterAnnotationBindings[i] : null)); 184 params.add(param); 185 i++; 186 } 187 } 188 } 189 return Collections.unmodifiableList(params); 190 } 191 return Collections.emptyList(); 192 } 193 194 @Override 195 public TypeMirror getReturnType() { 196 MethodBinding binding = (MethodBinding)_binding; 197 if (binding.returnType == null) { 198 return null; 199 } 200 else return _env.getFactory().newTypeMirror(binding.returnType); 201 } 202 203 @Override 204 public Name getSimpleName() { 205 MethodBinding binding = (MethodBinding)_binding; 206 if (_name == null) { 207 _name = new NameImpl(binding.selector); 208 } 209 return _name; 210 } 211 212 @Override 213 public List <? extends TypeMirror> getThrownTypes() { 214 MethodBinding binding = (MethodBinding)_binding; 215 if (binding.thrownExceptions.length == 0) { 216 return Collections.emptyList(); 217 } 218 List <TypeMirror> list = new ArrayList <TypeMirror>(binding.thrownExceptions.length); 219 for (ReferenceBinding exception : binding.thrownExceptions) { 220 list.add(_env.getFactory().newTypeMirror(exception)); 221 } 222 return list; 223 } 224 225 @Override 226 public List <? extends TypeParameterElement> getTypeParameters() { 227 MethodBinding binding = (MethodBinding)_binding; 228 TypeVariableBinding[] variables = binding.typeVariables(); 229 if (variables.length == 0) { 230 return Collections.emptyList(); 231 } 232 List <TypeParameterElement> params = new ArrayList <TypeParameterElement>(variables.length); 233 for (TypeVariableBinding variable : variables) { 234 params.add(_env.getFactory().newTypeParameterElement(variable, this)); 235 } 236 return Collections.unmodifiableList(params); 237 } 238 239 @Override 240 public boolean hides(Element hidden) 241 { 242 if (!(hidden instanceof ExecutableElementImpl)) { 243 return false; 244 } 245 MethodBinding hiderBinding = (MethodBinding)_binding; 246 MethodBinding hiddenBinding = (MethodBinding)((ExecutableElementImpl)hidden)._binding; 247 if (hiderBinding == hiddenBinding) { 248 return false; 249 } 250 if (hiddenBinding.isPrivate()) { 251 return false; 252 } 253 if (!hiderBinding.isStatic() || !hiddenBinding.isStatic()) { 255 return false; 256 } 257 if (!CharOperation.equals(hiddenBinding.selector, hiderBinding.selector)) { 259 return false; 260 } 261 if (!_env.getLookupEnvironment().methodVerifier().doesMethodOverride(hiderBinding, hiddenBinding)) { 263 return false; 264 } 265 return null != hiderBinding.declaringClass.findSuperTypeWithSameErasure(hiddenBinding.declaringClass); 266 } 267 268 @Override 269 public boolean isVarArgs() { 270 return ((MethodBinding) _binding).isVarargs(); 271 } 272 273 291 public boolean overrides(ExecutableElement overridden, TypeElement type) 292 { 293 MethodBinding overriddenBinding = (MethodBinding)((ExecutableElementImpl) overridden)._binding; 294 ReferenceBinding overriderContext = (ReferenceBinding)((TypeElementImpl)type)._binding; 295 if ((MethodBinding)_binding == overriddenBinding) 296 return false; 297 if (overriddenBinding.isPrivate()) { 298 return false; 299 } 300 char[] selector = ((MethodBinding)_binding).selector; 301 if (!CharOperation.equals(selector, overriddenBinding.selector)) 302 return false; 303 304 if (null == overriderContext.findSuperTypeWithSameErasure(((MethodBinding)_binding).declaringClass) && 308 null == ((MethodBinding)_binding).declaringClass.findSuperTypeWithSameErasure(overriderContext)) { 309 return false; 310 } 311 MethodBinding overriderBinding = new MethodBinding((MethodBinding)_binding, overriderContext); 312 if (overriderBinding.isPrivate()) { 313 return false; 317 } 318 319 TypeBinding match = overriderBinding.declaringClass.findSuperTypeWithSameErasure(overriddenBinding.declaringClass); 320 if (!(match instanceof ReferenceBinding)) return false; 321 322 org.eclipse.jdt.internal.compiler.lookup.MethodBinding[] superMethods = ((ReferenceBinding)match).getMethods(selector); 323 for (int i = 0, length = superMethods.length; i < length; i++) { 324 if (superMethods[i].original() == overriddenBinding) { 325 LookupEnvironment lookupEnvironment = _env.getLookupEnvironment(); 326 if (lookupEnvironment == null) return false; 327 MethodVerifier methodVerifier = lookupEnvironment.methodVerifier(); 328 org.eclipse.jdt.internal.compiler.lookup.MethodBinding superMethod = superMethods[i]; 329 return !superMethod.isPrivate() 330 && !(superMethod.isDefault() && (superMethod.declaringClass.getPackage()) != overriderBinding.declaringClass.getPackage()) 331 && methodVerifier.doesMethodOverride(overriderBinding, superMethod); 332 } 333 } 334 return false; 335 } 336 337 } 338 | Popular Tags |