List of usage examples for org.eclipse.jdt.internal.compiler.lookup ReferenceBinding getMethods
public MethodBinding[] getMethods(char[] selector, int suggestedParameterLength)
From source file:com.android.tools.lint.EcjParser.java
License:Apache License
/** Computes the super method, if any, given a method binding */ private static MethodBinding findSuperMethodBinding(@NonNull MethodBinding binding) { try {/* ww w .j a va 2 s .co m*/ ReferenceBinding superclass = binding.declaringClass.superclass(); while (superclass != null) { MethodBinding[] methods = superclass.getMethods(binding.selector, binding.parameters.length); for (MethodBinding method : methods) { if (method.areParameterErasuresEqual(binding)) { return method; } } superclass = superclass.superclass(); } } catch (Exception ignore) { // Work around ECJ bugs; see https://code.google.com/p/android/issues/detail?id=172268 } return null; }
From source file:com.android.tools.lint.psi.EcjPsiManager.java
License:Apache License
/** Computes the super method, if any, given a method binding */ @SuppressWarnings("SameParameterValue") @Nullable/* ww w . j a v a2s .co m*/ static MethodBinding findSuperMethodBinding(@NonNull MethodBinding binding, boolean allowStatic, boolean allowPrivate) { if (binding.isConstructor()) { return null; } if (!allowPrivate && binding.isPrivate()) { return null; } if (!allowStatic && binding.isStatic()) { return null; } try { ReferenceBinding superclass = binding.declaringClass.superclass(); while (superclass != null) { MethodBinding[] methods = superclass.getMethods(binding.selector, binding.parameters.length); for (MethodBinding method : methods) { if (method.isStatic() != binding.isStatic()) { continue; } if (method.areParameterErasuresEqual(binding)) { if (method.isPrivate()) { if (method.declaringClass.outermostEnclosingType() == binding.declaringClass .outermostEnclosingType()) { return method; } else { return null; } } else { return method; } } } // TODO: Check interfaces too! superclass = superclass.superclass(); } } catch (Exception ignore) { // Work around ECJ bugs; see https://code.google.com/p/android/issues/detail?id=172268 } return null; }
From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java
License:Open Source License
public MethodBinding findMethod(ReferenceBinding receiverType, char[] selector, TypeBinding[] argumentTypes, InvocationSite invocationSite, boolean inStaticContext) { ReferenceBinding currentType = receiverType; boolean receiverTypeIsInterface = receiverType.isInterface(); ObjectVector found = new ObjectVector(3); CompilationUnitScope unitScope = compilationUnitScope(); unitScope.recordTypeReferences(argumentTypes); if (receiverTypeIsInterface) { unitScope.recordTypeReference(receiverType); MethodBinding[] receiverMethods = receiverType.getMethods(selector, argumentTypes.length); if (receiverMethods.length > 0) found.addAll(receiverMethods); findMethodInSuperInterfaces(receiverType, selector, found, invocationSite); currentType = getJavaLangObject(); }/*from www .ja v a 2 s . co m*/ // superclass lookup long complianceLevel = compilerOptions().complianceLevel; boolean isCompliant14 = complianceLevel >= ClassFileConstants.JDK1_4; boolean isCompliant15 = complianceLevel >= ClassFileConstants.JDK1_5; ReferenceBinding classHierarchyStart = currentType; MethodVerifier verifier = environment().methodVerifier(); while (currentType != null) { unitScope.recordTypeReference(currentType); currentType = (ReferenceBinding) currentType.capture(this, invocationSite == null ? 0 : invocationSite.sourceEnd()); MethodBinding[] currentMethods = currentType.getMethods(selector, argumentTypes.length); int currentLength = currentMethods.length; if (currentLength > 0) { if (isCompliant14 && (receiverTypeIsInterface || found.size > 0)) { nextMethod: for (int i = 0, l = currentLength; i < l; i++) { // currentLength can be modified inside the loop MethodBinding currentMethod = currentMethods[i]; if (currentMethod == null) continue nextMethod; if (receiverTypeIsInterface && !currentMethod.isPublic()) { // only public methods from Object are visible to interface receiverTypes currentLength--; currentMethods[i] = null; continue nextMethod; } // if 1.4 compliant, must filter out redundant protected methods from superclasses // protected method need to be checked only - default access is already dealt with in #canBeSeen implementation // when checking that p.C -> q.B -> p.A cannot see default access members from A through B. // if ((currentMethod.modifiers & AccProtected) == 0) continue nextMethod; // BUT we can also ignore any overridden method since we already know the better match (fixes 80028) for (int j = 0, max = found.size; j < max; j++) { MethodBinding matchingMethod = (MethodBinding) found.elementAt(j); MethodBinding matchingOriginal = matchingMethod.original(); MethodBinding currentOriginal = matchingOriginal .findOriginalInheritedMethod(currentMethod); if (currentOriginal != null && verifier.isParameterSubsignature(matchingOriginal, currentOriginal)) { if (isCompliant15) { if (matchingMethod.isBridge() && !currentMethod.isBridge()) continue nextMethod; // keep inherited methods to find concrete method over a bridge method } currentLength--; currentMethods[i] = null; continue nextMethod; } } } } if (currentLength > 0) { // append currentMethods, filtering out null entries if (currentMethods.length == currentLength) { found.addAll(currentMethods); } else { for (int i = 0, max = currentMethods.length; i < max; i++) { MethodBinding currentMethod = currentMethods[i]; if (currentMethod != null) found.add(currentMethod); } } } } currentType = currentType.superclass(); } // if found several candidates, then eliminate those not matching argument types int foundSize = found.size; MethodBinding[] candidates = null; int candidatesCount = 0; MethodBinding problemMethod = null; boolean searchForDefaultAbstractMethod = isCompliant14 && !receiverTypeIsInterface && (receiverType.isAbstract() || receiverType.isTypeVariable()); if (foundSize > 0) { // argument type compatibility check for (int i = 0; i < foundSize; i++) { MethodBinding methodBinding = (MethodBinding) found.elementAt(i); MethodBinding compatibleMethod = computeCompatibleMethod(methodBinding, argumentTypes, invocationSite); if (compatibleMethod != null) { if (compatibleMethod.isValidBinding()) { if (foundSize == 1 && compatibleMethod.canBeSeenBy(receiverType, invocationSite, this)) { // return the single visible match now if (searchForDefaultAbstractMethod) return findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite, classHierarchyStart, found, compatibleMethod); unitScope.recordTypeReferences(compatibleMethod.thrownExceptions); return compatibleMethod; } if (candidatesCount == 0) candidates = new MethodBinding[foundSize]; candidates[candidatesCount++] = compatibleMethod; } else if (problemMethod == null) { problemMethod = compatibleMethod; } } } } // no match was found if (candidatesCount == 0) { if (problemMethod != null) { switch (problemMethod.problemId()) { case ProblemReasons.TypeArgumentsForRawGenericMethod: case ProblemReasons.TypeParameterArityMismatch: return problemMethod; } } // abstract classes may get a match in interfaces; for non abstract // classes, reduces secondary errors since missing interface method // error is already reported MethodBinding interfaceMethod = findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite, classHierarchyStart, found, null); if (interfaceMethod != null) return interfaceMethod; if (found.size == 0) return null; if (problemMethod != null) return problemMethod; // still no match; try to find a close match when the parameter // order is wrong or missing some parameters // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=69471 // bad guesses are foo(), when argument types have been supplied // and foo(X, Y), when the argument types are (int, float, Y) // so answer the method with the most argType matches and least parameter type mismatches int bestArgMatches = -1; MethodBinding bestGuess = (MethodBinding) found.elementAt(0); // if no good match so just use the first one found int argLength = argumentTypes.length; foundSize = found.size; nextMethod: for (int i = 0; i < foundSize; i++) { MethodBinding methodBinding = (MethodBinding) found.elementAt(i); TypeBinding[] params = methodBinding.parameters; int paramLength = params.length; int argMatches = 0; next: for (int a = 0; a < argLength; a++) { TypeBinding arg = argumentTypes[a]; for (int p = a == 0 ? 0 : a - 1; p < paramLength && p < a + 1; p++) { // look one slot before & after to see if the type matches if (params[p] == arg) { argMatches++; continue next; } } } if (argMatches < bestArgMatches) continue nextMethod; if (argMatches == bestArgMatches) { int diff1 = paramLength < argLength ? 2 * (argLength - paramLength) : paramLength - argLength; int bestLength = bestGuess.parameters.length; int diff2 = bestLength < argLength ? 2 * (argLength - bestLength) : bestLength - argLength; if (diff1 >= diff2) continue nextMethod; } bestArgMatches = argMatches; bestGuess = methodBinding; } return new ProblemMethodBinding(bestGuess, bestGuess.selector, argumentTypes, ProblemReasons.NotFound); } // tiebreak using visibility check int visiblesCount = 0; if (receiverTypeIsInterface) { if (candidatesCount == 1) { unitScope.recordTypeReferences(candidates[0].thrownExceptions); return candidates[0]; } visiblesCount = candidatesCount; } else { for (int i = 0; i < candidatesCount; i++) { MethodBinding methodBinding = candidates[i]; if (methodBinding.canBeSeenBy(receiverType, invocationSite, this)) { if (visiblesCount != i) { candidates[i] = null; candidates[visiblesCount] = methodBinding; } visiblesCount++; } } switch (visiblesCount) { case 0: MethodBinding interfaceMethod = findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite, classHierarchyStart, found, null); if (interfaceMethod != null) return interfaceMethod; return new ProblemMethodBinding(candidates[0], candidates[0].selector, candidates[0].parameters, ProblemReasons.NotVisible); case 1: if (searchForDefaultAbstractMethod) return findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite, classHierarchyStart, found, candidates[0]); unitScope.recordTypeReferences(candidates[0].thrownExceptions); return candidates[0]; default: break; } } if (complianceLevel <= ClassFileConstants.JDK1_3) { ReferenceBinding declaringClass = candidates[0].declaringClass; return !declaringClass.isInterface() ? mostSpecificClassMethodBinding(candidates, visiblesCount, invocationSite) : mostSpecificInterfaceMethodBinding(candidates, visiblesCount, invocationSite); } // check for duplicate parameterized methods if (compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5) { for (int i = 0; i < visiblesCount; i++) { MethodBinding candidate = candidates[i]; if (candidate instanceof ParameterizedGenericMethodBinding) candidate = ((ParameterizedGenericMethodBinding) candidate).originalMethod; if (candidate.hasSubstitutedParameters()) { for (int j = i + 1; j < visiblesCount; j++) { MethodBinding otherCandidate = candidates[j]; if (otherCandidate.hasSubstitutedParameters()) { if (otherCandidate == candidate || (candidate.declaringClass == otherCandidate.declaringClass && candidate.areParametersEqual(otherCandidate))) { return new ProblemMethodBinding(candidates[i], candidates[i].selector, candidates[i].parameters, ProblemReasons.Ambiguous); } } } } } } if (inStaticContext) { MethodBinding[] staticCandidates = new MethodBinding[visiblesCount]; int staticCount = 0; for (int i = 0; i < visiblesCount; i++) if (candidates[i].isStatic()) staticCandidates[staticCount++] = candidates[i]; if (staticCount == 1) return staticCandidates[0]; if (staticCount > 1) return mostSpecificMethodBinding(staticCandidates, staticCount, argumentTypes, invocationSite, receiverType); } MethodBinding mostSpecificMethod = mostSpecificMethodBinding(candidates, visiblesCount, argumentTypes, invocationSite, receiverType); if (searchForDefaultAbstractMethod) { // search interfaces for a better match if (mostSpecificMethod.isValidBinding()) // see if there is a better match in the interfaces - see AutoBoxingTest 99, LookupTest#81 return findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite, classHierarchyStart, found, mostSpecificMethod); // see if there is a match in the interfaces - see LookupTest#84 MethodBinding interfaceMethod = findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite, classHierarchyStart, found, null); if (interfaceMethod != null && interfaceMethod.isValidBinding() /* else return the same error as before */) return interfaceMethod; } return mostSpecificMethod; }
From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java
License:Open Source License
public MethodBinding getConstructor(ReferenceBinding receiverType, TypeBinding[] argumentTypes, InvocationSite invocationSite) { CompilationUnitScope unitScope = compilationUnitScope(); LookupEnvironment env = unitScope.environment; try {/*from www . ja v a2s . co m*/ env.missingClassFileLocation = invocationSite; unitScope.recordTypeReference(receiverType); unitScope.recordTypeReferences(argumentTypes); MethodBinding methodBinding = receiverType.getExactConstructor(argumentTypes); if (methodBinding != null && methodBinding.canBeSeenBy(invocationSite, this)) { // targeting a non generic constructor with type arguments ? if (invocationSite.genericTypeArguments() != null) methodBinding = computeCompatibleMethod(methodBinding, argumentTypes, invocationSite); return methodBinding; } MethodBinding[] methods = receiverType.getMethods(TypeConstants.INIT, argumentTypes.length); if (methods == Binding.NO_METHODS) return new ProblemMethodBinding(TypeConstants.INIT, argumentTypes, ProblemReasons.NotFound); MethodBinding[] compatible = new MethodBinding[methods.length]; int compatibleIndex = 0; MethodBinding problemMethod = null; for (int i = 0, length = methods.length; i < length; i++) { MethodBinding compatibleMethod = computeCompatibleMethod(methods[i], argumentTypes, invocationSite); if (compatibleMethod != null) { if (compatibleMethod.isValidBinding()) compatible[compatibleIndex++] = compatibleMethod; else if (problemMethod == null) problemMethod = compatibleMethod; } } if (compatibleIndex == 0) { if (problemMethod == null) return new ProblemMethodBinding(methods[0], TypeConstants.INIT, argumentTypes, ProblemReasons.NotFound); return problemMethod; } // need a more descriptive error... cannot convert from X to Y MethodBinding[] visible = new MethodBinding[compatibleIndex]; int visibleIndex = 0; for (int i = 0; i < compatibleIndex; i++) { MethodBinding method = compatible[i]; if (method.canBeSeenBy(invocationSite, this)) visible[visibleIndex++] = method; } if (visibleIndex == 1) return visible[0]; if (visibleIndex == 0) return new ProblemMethodBinding(compatible[0], TypeConstants.INIT, compatible[0].parameters, ProblemReasons.NotVisible); // all of visible are from the same declaringClass, even before 1.4 we can call this method instead of mostSpecificClassMethodBinding return mostSpecificMethodBinding(visible, visibleIndex, argumentTypes, invocationSite, receiverType); } catch (AbortCompilation e) { e.updateContext(invocationSite, referenceCompilationUnit().compilationResult); throw e; } finally { env.missingClassFileLocation = null; } }
From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java
License:Open Source License
public MethodBinding getStaticFactory(ReferenceBinding allocationType, ReferenceBinding originalEnclosingType, TypeBinding[] argumentTypes, final InvocationSite allocationSite) { TypeVariableBinding[] classTypeVariables = allocationType.typeVariables(); int classTypeVariablesArity = classTypeVariables.length; MethodBinding[] methods = allocationType.getMethods(TypeConstants.INIT, argumentTypes.length); MethodBinding[] staticFactories = new MethodBinding[methods.length]; int sfi = 0;//ww w. ja v a2s .c o m for (int i = 0, length = methods.length; i < length; i++) { MethodBinding method = methods[i]; int paramLength = method.parameters.length; boolean isVarArgs = method.isVarargs(); if (argumentTypes.length != paramLength) if (!isVarArgs || argumentTypes.length < paramLength - 1) continue; // incompatible TypeVariableBinding[] methodTypeVariables = method.typeVariables(); int methodTypeVariablesArity = methodTypeVariables.length; MethodBinding staticFactory = new MethodBinding(method.modifiers | ClassFileConstants.AccStatic, TypeConstants.SYNTHETIC_STATIC_FACTORY, null, null, null, method.declaringClass); staticFactory.typeVariables = new TypeVariableBinding[classTypeVariablesArity + methodTypeVariablesArity]; final SimpleLookupTable map = new SimpleLookupTable(classTypeVariablesArity + methodTypeVariablesArity); // Rename each type variable T of the type to T' final LookupEnvironment environment = environment(); for (int j = 0; j < classTypeVariablesArity; j++) { map.put(classTypeVariables[j], staticFactory.typeVariables[j] = new TypeVariableBinding( CharOperation.concat(classTypeVariables[j].sourceName, "'".toCharArray()), //$NON-NLS-1$ staticFactory, j, environment)); } // Rename each type variable U of method U to U''. for (int j = classTypeVariablesArity, max = classTypeVariablesArity + methodTypeVariablesArity; j < max; j++) { map.put(methodTypeVariables[j - classTypeVariablesArity], (staticFactory.typeVariables[j] = new TypeVariableBinding( CharOperation.concat(methodTypeVariables[j - classTypeVariablesArity].sourceName, "''".toCharArray()), //$NON-NLS-1$ staticFactory, j, environment))); } ReferenceBinding enclosingType = originalEnclosingType; while (enclosingType != null) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=345968 if (enclosingType.kind() == Binding.PARAMETERIZED_TYPE) { final ParameterizedTypeBinding parameterizedType = (ParameterizedTypeBinding) enclosingType; final ReferenceBinding genericType = parameterizedType.genericType(); TypeVariableBinding[] enclosingClassTypeVariables = genericType.typeVariables(); int enclosingClassTypeVariablesArity = enclosingClassTypeVariables.length; for (int j = 0; j < enclosingClassTypeVariablesArity; j++) { map.put(enclosingClassTypeVariables[j], parameterizedType.arguments[j]); } } enclosingType = enclosingType.enclosingType(); } final Scope scope = this; Substitution substitution = new Substitution() { public LookupEnvironment environment() { return scope.environment(); } public boolean isRawSubstitution() { return false; } public TypeBinding substitute(TypeVariableBinding typeVariable) { TypeBinding retVal = (TypeBinding) map.get(typeVariable); return retVal != null ? retVal : typeVariable; } }; // initialize new variable bounds for (int j = 0, max = classTypeVariablesArity + methodTypeVariablesArity; j < max; j++) { TypeVariableBinding originalVariable = j < classTypeVariablesArity ? classTypeVariables[j] : methodTypeVariables[j - classTypeVariablesArity]; TypeBinding substitutedType = (TypeBinding) map.get(originalVariable); if (substitutedType instanceof TypeVariableBinding) { TypeVariableBinding substitutedVariable = (TypeVariableBinding) substitutedType; TypeBinding substitutedSuperclass = Scope.substitute(substitution, originalVariable.superclass); ReferenceBinding[] substitutedInterfaces = Scope.substitute(substitution, originalVariable.superInterfaces); if (originalVariable.firstBound != null) { substitutedVariable.firstBound = originalVariable.firstBound == originalVariable.superclass ? substitutedSuperclass // could be array type or interface : substitutedInterfaces[0]; } switch (substitutedSuperclass.kind()) { case Binding.ARRAY_TYPE: substitutedVariable.superclass = environment.getResolvedType(TypeConstants.JAVA_LANG_OBJECT, null); substitutedVariable.superInterfaces = substitutedInterfaces; break; default: if (substitutedSuperclass.isInterface()) { substitutedVariable.superclass = environment .getResolvedType(TypeConstants.JAVA_LANG_OBJECT, null); int interfaceCount = substitutedInterfaces.length; System.arraycopy(substitutedInterfaces, 0, substitutedInterfaces = new ReferenceBinding[interfaceCount + 1], 1, interfaceCount); substitutedInterfaces[0] = (ReferenceBinding) substitutedSuperclass; substitutedVariable.superInterfaces = substitutedInterfaces; } else { substitutedVariable.superclass = (ReferenceBinding) substitutedSuperclass; // typeVar was extending other typeVar which got substituted with interface substitutedVariable.superInterfaces = substitutedInterfaces; } } } } TypeVariableBinding[] returnTypeParameters = new TypeVariableBinding[classTypeVariablesArity]; for (int j = 0; j < classTypeVariablesArity; j++) { returnTypeParameters[j] = (TypeVariableBinding) map.get(classTypeVariables[j]); } staticFactory.returnType = environment.createParameterizedType(allocationType, returnTypeParameters, allocationType.enclosingType()); staticFactory.parameters = Scope.substitute(substitution, method.parameters); staticFactory.thrownExceptions = Scope.substitute(substitution, method.thrownExceptions); if (staticFactory.thrownExceptions == null) { staticFactory.thrownExceptions = Binding.NO_EXCEPTIONS; } staticFactories[sfi++] = new ParameterizedMethodBinding( (ParameterizedTypeBinding) environment.convertToParameterizedType(staticFactory.declaringClass), staticFactory); } if (sfi == 0) return null; if (sfi != methods.length) { System.arraycopy(staticFactories, 0, staticFactories = new MethodBinding[sfi], 0, sfi); } MethodBinding[] compatible = new MethodBinding[sfi]; int compatibleIndex = 0; for (int i = 0; i < sfi; i++) { MethodBinding compatibleMethod = computeCompatibleMethod(staticFactories[i], argumentTypes, allocationSite); if (compatibleMethod != null) { if (compatibleMethod.isValidBinding()) compatible[compatibleIndex++] = compatibleMethod; } } if (compatibleIndex == 0) { return null; } MethodBinding[] visible = new MethodBinding[compatibleIndex]; int visibleIndex = 0; for (int i = 0; i < compatibleIndex; i++) { MethodBinding method = compatible[i]; if (method.canBeSeenBy(allocationSite, this)) visible[visibleIndex++] = method; } if (visibleIndex == 0) { return null; } return visibleIndex == 1 ? visible[0] : mostSpecificMethodBinding(visible, visibleIndex, argumentTypes, allocationSite, allocationType); }