Java Code Examples for org.eclipse.jdt.internal.compiler.lookup.TypeBinding#isCompatibleWith()

The following examples show how to use org.eclipse.jdt.internal.compiler.lookup.TypeBinding#isCompatibleWith() . 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: PatchExtensionMethod.java    From EasyMPermission with MIT License 6 votes vote down vote up
private static List<MethodBinding> getApplicableExtensionMethodsDefinedInProvider(EclipseNode typeNode, ReferenceBinding extensionMethodProviderBinding,
		TypeBinding receiverType) {
	
	List<MethodBinding> extensionMethods = new ArrayList<MethodBinding>();
	CompilationUnitScope cuScope = ((CompilationUnitDeclaration) typeNode.top().get()).scope;
	for (MethodBinding method : extensionMethodProviderBinding.methods()) {
		if (!method.isStatic()) continue;
		if (!method.isPublic()) continue;
		if (method.parameters == null || method.parameters.length == 0) continue;
		TypeBinding firstArgType = method.parameters[0];
		if (receiverType.isProvablyDistinct(firstArgType) && !receiverType.isCompatibleWith(firstArgType.erasure())) continue;
		TypeBinding[] argumentTypes = Arrays.copyOfRange(method.parameters, 1, method.parameters.length);
		if ((receiverType instanceof ReferenceBinding) && ((ReferenceBinding) receiverType).getExactMethod(method.selector, argumentTypes, cuScope) != null) continue;
		extensionMethods.add(method);
	}
	return extensionMethods;
}
 
Example 2
Source File: TypesImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @return true if a value of type t1 can be assigned to a variable of type t2, i.e., t2 = t1.
 */
@Override
public boolean isAssignable(TypeMirror t1, TypeMirror t2) {
    if (!(t1 instanceof TypeMirrorImpl) || !(t2 instanceof TypeMirrorImpl)) {
        return false;
    }
    Binding b1 = ((TypeMirrorImpl)t1).binding();
    Binding b2 = ((TypeMirrorImpl)t2).binding();
    if (!(b1 instanceof TypeBinding) || !(b2 instanceof TypeBinding)) {
        // package, method, import, etc.
        throw new IllegalArgumentException();
    }
    if (((TypeBinding)b1).isCompatibleWith((TypeBinding)b2)) {
        return true;
    }

    TypeBinding convertedType = _env.getLookupEnvironment().computeBoxingType((TypeBinding)b1);
    return null != convertedType && convertedType.isCompatibleWith((TypeBinding)b2);
}
 
Example 3
Source File: CastExpression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Complain if assigned expression is cast, but not actually used as such, e.g. Object o = (List) object;
 */
public static void checkNeedForAssignedCast(BlockScope scope, TypeBinding expectedType, CastExpression rhs) {
	CompilerOptions compilerOptions = scope.compilerOptions();
	if (compilerOptions.getSeverity(CompilerOptions.UnnecessaryTypeCheck) == ProblemSeverities.Ignore) return;

	TypeBinding castedExpressionType = rhs.expression.resolvedType;
	//	int i = (byte) n; // cast still had side effect
	// double d = (float) n; // cast to float is unnecessary
	if (castedExpressionType == null || rhs.resolvedType.isBaseType()) return;
	//if (castedExpressionType.id == T_null) return; // tolerate null expression cast
	if (castedExpressionType.isCompatibleWith(expectedType, scope)) {
		if (compilerOptions.isAnnotationBasedNullAnalysisEnabled && compilerOptions.sourceLevel >= ClassFileConstants.JDK1_8) {
			// are null annotations compatible, too?
			if (NullAnnotationMatching.analyse(expectedType, castedExpressionType, -1).isAnyMismatch())
				return; // already reported unchecked cast (nullness), say no more.
		}
		scope.problemReporter().unnecessaryCast(rhs);
	}
}
 
Example 4
Source File: Expression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public TypeBinding resolveTypeExpecting(BlockScope scope, TypeBinding expectedType) {
	setExpectedType(expectedType); // needed in case of generic method invocation
	TypeBinding expressionType = this.resolveType(scope);
	if (expressionType == null) return null;
	if (TypeBinding.equalsEquals(expressionType, expectedType)) return expressionType;

	if (!expressionType.isCompatibleWith(expectedType)) {
		if (scope.isBoxingCompatibleWith(expressionType, expectedType)) {
			computeConversion(scope, expectedType, expressionType);
		} else {
			scope.problemReporter().typeMismatchError(expressionType, expectedType, this, null);
			return null;
		}
	}
	return expressionType;
}
 
Example 5
Source File: ReferenceExpression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public boolean sIsMoreSpecific(TypeBinding s, TypeBinding t, Scope scope) {
	
	if (super.sIsMoreSpecific(s, t, scope))
		return true;
	
	if (this.exactMethodBinding == null || t.findSuperTypeOriginatingFrom(s) != null)
		return false;
	
	s = s.capture(this.enclosingScope, this.sourceEnd);
	MethodBinding sSam = s.getSingleAbstractMethod(this.enclosingScope, true);
	if (sSam == null || !sSam.isValidBinding())
		return false;
	TypeBinding r1 = sSam.returnType;
	
	MethodBinding tSam = t.getSingleAbstractMethod(this.enclosingScope, true);
	if (tSam == null || !tSam.isValidBinding())
		return false;
	TypeBinding r2 = tSam.returnType;
	
	if (r2.id == TypeIds.T_void)
		return true;
	
	if (r1.id == TypeIds.T_void)
		return false;
	
	// r1 <: r2
	if (r1.isCompatibleWith(r2, scope))
		return true;
	
	return r1.isBaseType() != r2.isBaseType() && r1.isBaseType() == this.exactMethodBinding.returnType.isBaseType();
}
 
Example 6
Source File: LambdaExpression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public boolean sIsMoreSpecific(TypeBinding s, TypeBinding t, Scope skope) {
	
	// 15.12.2.5 
	
	if (super.sIsMoreSpecific(s, t, skope))
		return true;
	
	if (argumentsTypeElided() || t.findSuperTypeOriginatingFrom(s) != null)
		return false;
	
	s = s.capture(this.enclosingScope, this.sourceEnd);
	MethodBinding sSam = s.getSingleAbstractMethod(this.enclosingScope, true);
	if (sSam == null || !sSam.isValidBinding())
		return false;
	TypeBinding r1 = sSam.returnType;
	MethodBinding tSam = t.getSingleAbstractMethod(this.enclosingScope, true);
	if (tSam == null || !tSam.isValidBinding())
		return false;
	TypeBinding r2 = tSam.returnType;
	
	if (r2.id == TypeIds.T_void)
		return true;
	
	if (r1.id == TypeIds.T_void)
		return false;
	
	// r1 <: r2
	if (r1.isCompatibleWith(r2, skope))
		return true;
	
	Expression [] returnExpressions = this.resultExpressions;
	int returnExpressionsLength = returnExpressions == null ? 0 : returnExpressions.length;
	
	int i;
	// r1 is a primitive type, r2 is a reference type, and each result expression is a standalone expression (15.2) of a primitive type
	if (r1.isBaseType() && !r2.isBaseType()) {
		for (i = 0; i < returnExpressionsLength; i++) {
			if (returnExpressions[i].isPolyExpression() || !returnExpressions[i].resolvedType.isBaseType())
				break;
		}
		if (i == returnExpressionsLength)
			return true;
	}
	if (!r1.isBaseType() && r2.isBaseType()) {
		for (i = 0; i < returnExpressionsLength; i++) {
			if (returnExpressions[i].resolvedType.isBaseType())
				break;
		}
		if (i == returnExpressionsLength)
			return true;
	}
	if (r1.isFunctionalInterface(this.enclosingScope) && r2.isFunctionalInterface(this.enclosingScope)) {
		for (i = 0; i < returnExpressionsLength; i++) {
			Expression resultExpression = returnExpressions[i];
			if (!resultExpression.sIsMoreSpecific(r1, r2, skope))
				break;
		}
		if (i == returnExpressionsLength)
			return true;
	}
	return false;
}
 
Example 7
Source File: Expression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public boolean sIsMoreSpecific(TypeBinding s, TypeBinding t, Scope scope) {
	return s.isCompatibleWith(t, scope);
}
 
Example 8
Source File: UnionTypeReference.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public TypeBinding resolveType(BlockScope scope, boolean checkBounds, int location) {
	// return the lub (least upper bound of all type binding) 
	int length = this.typeReferences.length;
	TypeBinding[] allExceptionTypes = new TypeBinding[length];
	boolean hasError = false;
	for (int i = 0; i < length; i++) {
		TypeBinding exceptionType = this.typeReferences[i].resolveType(scope, checkBounds, location);
		if (exceptionType == null) {
			return null;
		}
		switch(exceptionType.kind()) {
			case Binding.PARAMETERIZED_TYPE :
				if (exceptionType.isBoundParameterizedType()) {
					hasError = true;
					scope.problemReporter().invalidParameterizedExceptionType(exceptionType, this.typeReferences[i]);
					// fall thru to create the variable - avoids additional errors because the variable is missing
				}
				break;
			case Binding.TYPE_PARAMETER :
				scope.problemReporter().invalidTypeVariableAsException(exceptionType, this.typeReferences[i]);
				hasError = true;
				// fall thru to create the variable - avoids additional errors because the variable is missing
				break;
		}
		if (exceptionType.findSuperTypeOriginatingFrom(TypeIds.T_JavaLangThrowable, true) == null
				&& exceptionType.isValidBinding()) {
			scope.problemReporter().cannotThrowType(this.typeReferences[i], exceptionType);
			hasError = true;
		}
		allExceptionTypes[i] = exceptionType;
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=340486, ensure types are of union type.
		for (int j = 0; j < i; j++) {
			if (allExceptionTypes[j].isCompatibleWith(exceptionType)) {
				scope.problemReporter().wrongSequenceOfExceptionTypes(
						this.typeReferences[j],
						allExceptionTypes[j],
						exceptionType);
				hasError = true;
			} else if (exceptionType.isCompatibleWith(allExceptionTypes[j])) {
				scope.problemReporter().wrongSequenceOfExceptionTypes(
						this.typeReferences[i],
						exceptionType,
						allExceptionTypes[j]);
				hasError = true;
			}
		}
	}
	if (hasError) {
		return null;
	}
	// compute lub
	return (this.resolvedType = scope.lowerUpperBound(allExceptionTypes));
}
 
Example 9
Source File: CaseStatement.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns the constant intValue or ordinal for enum constants. If constant is NotAConstant, then answers Float.MIN_VALUE
 * @see org.eclipse.jdt.internal.compiler.ast.Statement#resolveCase(org.eclipse.jdt.internal.compiler.lookup.BlockScope, org.eclipse.jdt.internal.compiler.lookup.TypeBinding, org.eclipse.jdt.internal.compiler.ast.SwitchStatement)
 */
public Constant resolveCase(BlockScope scope, TypeBinding switchExpressionType, SwitchStatement switchStatement) {
	// switchExpressionType maybe null in error case
	scope.enclosingCase = this; // record entering in a switch case block

	if (this.constantExpression == null) {
		// remember the default case into the associated switch statement
		if (switchStatement.defaultCase != null)
			scope.problemReporter().duplicateDefaultCase(this);

		// on error the last default will be the selected one ...
		switchStatement.defaultCase = this;
		return Constant.NotAConstant;
	}
	// add into the collection of cases of the associated switch statement
	switchStatement.cases[switchStatement.caseCount++] = this;
	// tag constant name with enum type for privileged access to its members
	if (switchExpressionType != null && switchExpressionType.isEnum() && (this.constantExpression instanceof SingleNameReference)) {
		((SingleNameReference) this.constantExpression).setActualReceiverType((ReferenceBinding)switchExpressionType);
	}
	TypeBinding caseType = this.constantExpression.resolveType(scope);
	if (caseType == null || switchExpressionType == null) return Constant.NotAConstant;
	if (this.constantExpression.isConstantValueOfTypeAssignableToType(caseType, switchExpressionType)
			|| caseType.isCompatibleWith(switchExpressionType)) {
		if (caseType.isEnum()) {
			if (((this.constantExpression.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT) != 0) {
				scope.problemReporter().enumConstantsCannotBeSurroundedByParenthesis(this.constantExpression);
			}

			if (this.constantExpression instanceof NameReference
					&& (this.constantExpression.bits & ASTNode.RestrictiveFlagMASK) == Binding.FIELD) {
				NameReference reference = (NameReference) this.constantExpression;
				FieldBinding field = reference.fieldBinding();
				if ((field.modifiers & ClassFileConstants.AccEnum) == 0) {
					 scope.problemReporter().enumSwitchCannotTargetField(reference, field);
				} else 	if (reference instanceof QualifiedNameReference) {
					 scope.problemReporter().cannotUseQualifiedEnumConstantInCaseLabel(reference, field);
				}
				return IntConstant.fromValue(field.original().id + 1); // (ordinal value + 1) zero should not be returned see bug 141810
			}
		} else {
			return this.constantExpression.constant;
		}
	} else if (isBoxingCompatible(caseType, switchExpressionType, this.constantExpression, scope)) {
		// constantExpression.computeConversion(scope, caseType, switchExpressionType); - do not report boxing/unboxing conversion
		return this.constantExpression.constant;
	}
	scope.problemReporter().typeMismatchError(caseType, switchExpressionType, this.constantExpression, switchStatement.expression);
	return Constant.NotAConstant;
}