org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding Java Examples

The following examples show how to use org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CodeSnippetScope.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public MethodBinding findMethodForArray(ArrayBinding receiverType, char[] selector, TypeBinding[] argumentTypes, InvocationSite invocationSite) {
	ReferenceBinding object = getJavaLangObject();
	MethodBinding methodBinding = object.getExactMethod(selector, argumentTypes, null);
	if (methodBinding != null) {
		// handle the method clone() specially... cannot be protected or throw exceptions
		if (argumentTypes == Binding.NO_PARAMETERS && CharOperation.equals(selector, TypeConstants.CLONE))
			return new MethodBinding((methodBinding.modifiers & ~ClassFileConstants.AccProtected) | ClassFileConstants.AccPublic, TypeConstants.CLONE, methodBinding.returnType, argumentTypes, null, object);
		if (canBeSeenByForCodeSnippet(methodBinding, receiverType, invocationSite, this))
			return methodBinding;
	}

	// answers closest approximation, may not check argumentTypes or visibility
	methodBinding = findMethod(object, selector, argumentTypes, invocationSite, false);
	if (methodBinding == null)
		return new ProblemMethodBinding(selector, argumentTypes, ProblemReasons.NotFound);
	if (methodBinding.isValidBinding()) {
	    MethodBinding compatibleMethod = computeCompatibleMethod(methodBinding, argumentTypes, invocationSite, Scope.FULL_INFERENCE);
	    if (compatibleMethod == null)
			return new ProblemMethodBinding(methodBinding, selector, argumentTypes, ProblemReasons.NotFound);
	    methodBinding = compatibleMethod;
		if (!canBeSeenByForCodeSnippet(methodBinding, receiverType, invocationSite, this))
			return new ProblemMethodBinding(methodBinding, selector, methodBinding.parameters, ProblemReasons.NotVisible);
	}
	return methodBinding;
}
 
Example #2
Source File: LambdaExpression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public TypeBinding[] getMarkerInterfaces() {
	if (this.expectedType instanceof IntersectionCastTypeBinding) {
		Set markerBindings = new LinkedHashSet();
		TypeBinding[] intersectionTypes = ((IntersectionCastTypeBinding)this.expectedType).intersectingTypes;
		for (int i = 0,max = intersectionTypes.length; i < max; i++) {
			TypeBinding typeBinding = intersectionTypes[i];
			MethodBinding methodBinding = typeBinding.getSingleAbstractMethod(this.scope, true);
			// Why doesn't getSingleAbstractMethod do as the javadoc says, and return null
			// when it is not a SAM type
			if (!(methodBinding instanceof ProblemMethodBinding && ((ProblemMethodBinding)methodBinding).problemId()==ProblemReasons.NoSuchSingleAbstractMethod)) {
				continue;
			}
			if (typeBinding.id == TypeIds.T_JavaIoSerializable) {
				// Serializable is captured as a bitflag
				continue;
			}
			markerBindings.add(typeBinding);
		}
		if (markerBindings.size() > 0) {
			return (TypeBinding[])markerBindings.toArray(new TypeBinding[markerBindings.size()]);
		}
	}
	return null;
}
 
Example #3
Source File: EcjParser.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private ResolvedNode resolve(@Nullable Binding binding) {
    if (binding == null || binding instanceof ProblemBinding) {
        return null;
    }

    if (binding instanceof TypeBinding) {
        TypeBinding tb = (TypeBinding) binding;
        return new EcjResolvedClass(tb);
    } else if (binding instanceof MethodBinding) {
        MethodBinding mb = (MethodBinding) binding;
        if (mb instanceof ProblemMethodBinding) {
            return null;
        }
        //noinspection VariableNotUsedInsideIf
        if (mb.declaringClass != null) {
            return new EcjResolvedMethod(mb);
        }
    } else if (binding instanceof LocalVariableBinding) {
        LocalVariableBinding lvb = (LocalVariableBinding) binding;
        //noinspection VariableNotUsedInsideIf
        if (lvb.type != null) {
            return new EcjResolvedVariable(lvb);
        }
    } else if (binding instanceof FieldBinding) {
        FieldBinding fb = (FieldBinding) binding;
        if (fb instanceof ProblemFieldBinding) {
            return null;
        }
        if (fb.type != null && fb.declaringClass != null) {
            return new EcjResolvedField(fb);
        }
    }

    return null;
}
 
Example #4
Source File: MemberDeclarationVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public boolean visit(LambdaExpression lambdaExpression, BlockScope scope) {
	Integer level = (Integer) this.nodeSet.matchingNodes.removeKey(lambdaExpression);
	try {
		if (lambdaExpression.resolvedType != null && lambdaExpression.resolvedType.isValidBinding() &&
				!(lambdaExpression.descriptor instanceof ProblemMethodBinding))
			this.locator.reportMatching(lambdaExpression, this.enclosingElement, level != null ? level.intValue() : -1, this.nodeSet, this.typeInHierarchy);
		else 
			return true;
	} catch (CoreException e) {
		throw new WrappedCoreException(e);
	}
	return false; // Don't visit the children as they get traversed under control of reportMatching.
}
 
Example #5
Source File: CodeSnippetScope.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public MethodBinding findMethod(ReferenceBinding receiverType, char[] selector, TypeBinding[] argumentTypes, InvocationSite invocationSite, boolean inStaticContext) {
	MethodBinding methodBinding = super.findMethod(receiverType, selector, argumentTypes, invocationSite, inStaticContext);
	if (methodBinding != null && methodBinding.isValidBinding())
		if (!canBeSeenByForCodeSnippet(methodBinding, receiverType, invocationSite, this))
			return new ProblemMethodBinding(methodBinding, selector, argumentTypes, ProblemReasons.NotVisible);
	return methodBinding;
}
 
Example #6
Source File: CodeSnippetScope.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public MethodBinding getConstructor(ReferenceBinding receiverType, TypeBinding[] argumentTypes, InvocationSite invocationSite) {
	MethodBinding methodBinding = receiverType.getExactConstructor(argumentTypes);
	if (methodBinding != null) {
		if (canBeSeenByForCodeSnippet(methodBinding, receiverType, invocationSite, this)) {
			return methodBinding;
		}
	}
	MethodBinding[] methods = receiverType.getMethods(TypeConstants.INIT);
	if (methods == Binding.NO_METHODS) {
		return new ProblemMethodBinding(TypeConstants.INIT, argumentTypes, ProblemReasons.NotFound);
	}
	MethodBinding[] compatible = new MethodBinding[methods.length];
	int compatibleIndex = 0;
	for (int i = 0, length = methods.length; i < length; i++) {
	    MethodBinding compatibleMethod = computeCompatibleMethod(methods[i], argumentTypes, invocationSite, Scope.APPLICABILITY);
		if (compatibleMethod != null)
			compatible[compatibleIndex++] = compatibleMethod;
	}
	if (compatibleIndex == 0)
		return new ProblemMethodBinding(TypeConstants.INIT, argumentTypes, ProblemReasons.NotFound); // 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 (canBeSeenByForCodeSnippet(method, receiverType, invocationSite, this)) {
			visible[visibleIndex++] = method;
		}
	}
	if (visibleIndex == 1) {
		// 1.8: Give inference a chance to perform outstanding tasks (18.5.2):
		return inferInvocationType(invocationSite, visible[0], argumentTypes);
	}
	if (visibleIndex == 0) {
		return new ProblemMethodBinding(compatible[0], TypeConstants.INIT, compatible[0].parameters, ProblemReasons.NotVisible);
	}
	return mostSpecificClassMethodBinding(visible, visibleIndex, invocationSite);
}
 
Example #7
Source File: CodeSnippetScope.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public MethodBinding getImplicitMethod(ReferenceBinding receiverType, char[] selector, TypeBinding[] argumentTypes, InvocationSite invocationSite) {
	// retrieve an exact visible match (if possible)
	MethodBinding methodBinding = findExactMethod(receiverType, selector, argumentTypes, invocationSite);
	if (methodBinding == null)
		methodBinding = findMethod(receiverType, selector, argumentTypes, invocationSite, false);
	if (methodBinding != null) { // skip it if we did not find anything
		if (methodBinding.isValidBinding())
		    if (!canBeSeenByForCodeSnippet(methodBinding, receiverType, invocationSite, this))
				return new ProblemMethodBinding(methodBinding, selector, argumentTypes, ProblemReasons.NotVisible);
		return methodBinding;
	}
	return new ProblemMethodBinding(selector, argumentTypes, ProblemReasons.NotFound);
}
 
Example #8
Source File: LambdaExpression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public MethodBinding getMethodBinding() {
	if (this.actualMethodBinding == null) {
		if (this.binding != null) {
			this.actualMethodBinding = new MethodBinding(this.binding.modifiers, this.binding.selector, this.binding.returnType, 
					this.binding instanceof SyntheticMethodBinding ? this.descriptor.parameters : this.binding.parameters,  // retain any faults in parameter list.
							this.binding.thrownExceptions, this.binding.declaringClass);
			this.actualMethodBinding.tagBits = this.binding.tagBits;
		} else {
			this.actualMethodBinding = new ProblemMethodBinding(CharOperation.NO_CHAR, null, ProblemReasons.NoSuchSingleAbstractMethod);
		}
	}
	return this.actualMethodBinding;
}
 
Example #9
Source File: HandleFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Create handle by adding child to parent obtained by recursing into parent scopes.
 */
public IJavaElement createElement(Scope scope, int elementPosition, ICompilationUnit unit, HashSet existingElements, HashMap knownScopes) {
	IJavaElement newElement = (IJavaElement)knownScopes.get(scope);
	if (newElement != null) return newElement;

	switch(scope.kind) {
		case Scope.COMPILATION_UNIT_SCOPE :
			newElement = unit;
			break;
		case Scope.CLASS_SCOPE :
			IJavaElement parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
			switch (parentElement.getElementType()) {
				case IJavaElement.COMPILATION_UNIT :
					newElement = ((ICompilationUnit)parentElement).getType(new String(scope.enclosingSourceType().sourceName));
					break;
				case IJavaElement.TYPE :
					newElement = ((IType)parentElement).getType(new String(scope.enclosingSourceType().sourceName));
					break;
				case IJavaElement.FIELD :
				case IJavaElement.INITIALIZER :
				case IJavaElement.METHOD :
				    IMember member = (IMember)parentElement;
				    if (member.isBinary()) {
				        return null;
				    } else {
						newElement = member.getType(new String(scope.enclosingSourceType().sourceName), 1);
						// increment occurrence count if collision is detected
						if (newElement != null) {
							while (!existingElements.add(newElement)) ((SourceRefElement)newElement).occurrenceCount++;
						}
				    }
					break;
			}
			if (newElement != null) {
				knownScopes.put(scope, newElement);
			}
			break;
		case Scope.METHOD_SCOPE :
			if (scope.isLambdaScope()) {
				parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
				LambdaExpression expression = (LambdaExpression) scope.originalReferenceContext();
				if (expression.resolvedType != null && expression.resolvedType.isValidBinding() && 
						!(expression.descriptor instanceof ProblemMethodBinding)) { // chain in lambda element only if resolved properly.
					//newElement = new org.eclipse.jdt.internal.core.SourceLambdaExpression((JavaElement) parentElement, expression).getMethod();
					newElement = LambdaFactory.createLambdaExpression((JavaElement) parentElement, expression).getMethod();
					knownScopes.put(scope, newElement);
					return newElement;
				}
				return parentElement;
			}
			IType parentType = (IType) createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
			MethodScope methodScope = (MethodScope) scope;
			if (methodScope.isInsideInitializer()) {
				// inside field or initializer, must find proper one
				TypeDeclaration type = methodScope.referenceType();
				int occurenceCount = 1;
				int length = type.fields == null ? 0 : type.fields.length;
				for (int i = 0; i < length; i++) {
					FieldDeclaration field = type.fields[i];
					if (field.declarationSourceStart <= elementPosition && elementPosition <= field.declarationSourceEnd) {
						switch (field.getKind()) {
							case AbstractVariableDeclaration.FIELD :
							case AbstractVariableDeclaration.ENUM_CONSTANT :
								newElement = parentType.getField(new String(field.name));
								break;
							case AbstractVariableDeclaration.INITIALIZER :
								newElement = parentType.getInitializer(occurenceCount);
								break;
						}
						break;
					} else if (field.getKind() == AbstractVariableDeclaration.INITIALIZER) {
						occurenceCount++;
					}
				}
			} else {
				// method element
				AbstractMethodDeclaration method = methodScope.referenceMethod();
				newElement = parentType.getMethod(new String(method.selector), Util.typeParameterSignatures(method));
				if (newElement != null) {
					knownScopes.put(scope, newElement);
				}
			}
			break;
		case Scope.BLOCK_SCOPE :
			// standard block, no element per se
			newElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
			break;
	}
	return newElement;
}
 
Example #10
Source File: NullAnnotationMatching.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * After a method has substituted type parameters, check if this resulted in any contradictory null annotations.
 * Problems are either reported directly (if scope != null) or by returning a ProblemMethodBinding.
 */
public static MethodBinding checkForContraditions(
		final MethodBinding method, final InvocationSite invocationSite, final Scope scope) {
	
	class SearchContradictions extends TypeBindingVisitor {
		ReferenceBinding typeWithContradiction;
		@Override
		public boolean visit(ReferenceBinding referenceBinding) {
			if ((referenceBinding.tagBits & TagBits.AnnotationNullMASK) == TagBits.AnnotationNullMASK) {
				this.typeWithContradiction = referenceBinding;
				return false;
			}
			return true;
		}
		@Override
		public boolean visit(TypeVariableBinding typeVariable) {
			return visit((ReferenceBinding)typeVariable);
		}
		@Override
		public boolean visit(RawTypeBinding rawType) {
			return visit((ReferenceBinding)rawType);
		}
	}

	SearchContradictions searchContradiction = new SearchContradictions();
	TypeBindingVisitor.visit(searchContradiction, method.returnType);
	if (searchContradiction.typeWithContradiction != null) {
		if (scope == null)
			return new ProblemMethodBinding(method, method.selector, method.parameters, ProblemReasons.ContradictoryNullAnnotations);
		scope.problemReporter().contradictoryNullAnnotationsInferred(method, invocationSite);
		// note: if needed, we might want to update the method by removing the contradictory annotations??
		return method;
	}

	Expression[] arguments = null;
	if (invocationSite instanceof Invocation)
		arguments = ((Invocation)invocationSite).arguments();
	for (int i = 0; i < method.parameters.length; i++) {
		TypeBindingVisitor.visit(searchContradiction, method.parameters[i]);
		if (searchContradiction.typeWithContradiction != null) {
			if (scope == null)
				return new ProblemMethodBinding(method, method.selector, method.parameters, ProblemReasons.ContradictoryNullAnnotations);
			if (arguments != null && i < arguments.length)
				scope.problemReporter().contradictoryNullAnnotationsInferred(method, arguments[i]);
			else
				scope.problemReporter().contradictoryNullAnnotationsInferred(method, invocationSite);
			return method;
		}
	}
	return method;
}
 
Example #11
Source File: MessageSend.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Find the method binding; 
 * if this.innersNeedUpdate allow for two attempts where the first round may stop
 * after applicability checking (18.5.1) to include more information into the final
 * invocation type inference (18.5.2).
 */
protected void findMethodBinding(BlockScope scope, TypeBinding[] argumentTypes) {
	this.binding = this.receiver.isImplicitThis()
			? scope.getImplicitMethod(this.selector, argumentTypes, this)
			: scope.getMethod(this.actualReceiverType, this.selector, argumentTypes, this);
	resolvePolyExpressionArguments(this, this.binding, argumentTypes, scope);
	
	/* There are embedded assumptions in the JLS8 type inference scheme that a successful solution of the type equations results in an
	   applicable method. This appears to be a tenuous assumption, at least one not made by the JLS7 engine or the reference compiler and 
	   there are cases where this assumption would appear invalid: See https://bugs.eclipse.org/bugs/show_bug.cgi?id=426537, where we allow 
	   certain compatibility constrains around raw types to be violated. 
       
       Here, we filter out such inapplicable methods with raw type usage that may have sneaked past overload resolution and type inference, 
       playing the devils advocate, blaming the invocations with raw arguments that should not go blameless. At this time this is in the 
       nature of a point fix and is not a general solution which needs to come later (that also includes AE, QAE and ECC)
    */
	final CompilerOptions compilerOptions = scope.compilerOptions();
	if (compilerOptions.sourceLevel >= ClassFileConstants.JDK1_8 && this.binding instanceof ParameterizedGenericMethodBinding && this.binding.isValidBinding()) {
		if (!compilerOptions.postResolutionRawTypeCompatibilityCheck)
			return;
		ParameterizedGenericMethodBinding pgmb = (ParameterizedGenericMethodBinding) this.binding;
		InferenceContext18 ctx = getInferenceContext(pgmb);
		if (ctx == null || ctx.stepCompleted < InferenceContext18.BINDINGS_UPDATED)
			return;
		int length = pgmb.typeArguments == null ? 0 : pgmb.typeArguments.length;
		boolean sawRawType = false;
		for (int i = 0;  i < length; i++) {
			/* Must check compatibility against capture free method. Formal parameters cannot have captures, but our machinery is not up to snuff to
			   construct a PGMB without captures at the moment - for one thing ITCB does not support uncapture() yet, for another, INTERSECTION_CAST_TYPE
			   does not appear fully hooked up into isCompatibleWith and isEquivalent to everywhere. At the moment, bail out if we see capture.
			*/   
			if (pgmb.typeArguments[i].isCapture())
				return;
			if (pgmb.typeArguments[i].isRawType())
				sawRawType = true;
		}
		if (!sawRawType)
			return;
		length = this.arguments == null ? 0 : this.arguments.length;
		if (length == 0)
			return;
		TypeBinding [] finalArgumentTypes = new TypeBinding[length];
		for (int i = 0; i < length; i++) {
			TypeBinding finalArgumentType = this.arguments[i].resolvedType;
			if (finalArgumentType == null || !finalArgumentType.isValidBinding())  // already sided with the devil.
				return;
			finalArgumentTypes[i] = finalArgumentType; 
		}
		if (scope.parameterCompatibilityLevel(this.binding, finalArgumentTypes, false) == Scope.NOT_COMPATIBLE)
			this.binding = new ProblemMethodBinding(this.binding.original(), this.binding.selector, finalArgumentTypes, ProblemReasons.NotFound);
	}
}