Java Code Examples for org.eclipse.jdt.core.dom.ITypeBinding#isAssignmentCompatible()

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#isAssignmentCompatible() . 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: MissingReturnTypeInLambdaCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected Expression computeProposals(AST ast, ITypeBinding returnBinding, int returnOffset, CompilationUnit root, Expression result) {
	ScopeAnalyzer analyzer= new ScopeAnalyzer(root);
	IBinding[] bindings= analyzer.getDeclarationsInScope(returnOffset, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY);

	org.eclipse.jdt.core.dom.NodeFinder finder= new org.eclipse.jdt.core.dom.NodeFinder(root, returnOffset, 0);
	ASTNode varDeclFrag= ASTResolving.findAncestor(finder.getCoveringNode(), ASTNode.VARIABLE_DECLARATION_FRAGMENT);
	IVariableBinding varDeclFragBinding= null;
	if (varDeclFrag != null) {
		varDeclFragBinding= ((VariableDeclarationFragment) varDeclFrag).resolveBinding();
	}
	for (int i= 0; i < bindings.length; i++) {
		IVariableBinding curr= (IVariableBinding) bindings[i];
		ITypeBinding type= curr.getType();
		// Bindings are compared to make sure that a lambda does not return a variable which is yet to be initialised.
		if (type != null && type.isAssignmentCompatible(returnBinding) && testModifier(curr) && !Bindings.equals(curr, varDeclFragBinding)) {
			if (result == null) {
				result= ast.newSimpleName(curr.getName());
			}
			addLinkedPositionProposal(RETURN_EXPRESSION_KEY, curr.getName());
		}
	}
	return result;
}
 
Example 2
Source File: ExceptionAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static ITypeBinding[] perform(BodyDeclaration enclosingNode, Selection selection) {
	ExceptionAnalyzer analyzer= new ExceptionAnalyzer(selection);
	enclosingNode.accept(analyzer);
	List<ITypeBinding> exceptions= analyzer.getCurrentExceptions();
	if (enclosingNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
		List<Type> thrownExceptions= ((MethodDeclaration) enclosingNode).thrownExceptionTypes();
		for (Iterator<Type> thrown= thrownExceptions.iterator(); thrown.hasNext();) {
			ITypeBinding thrownException= thrown.next().resolveBinding();
			if (thrownException != null) {
				for (Iterator<ITypeBinding> excep= exceptions.iterator(); excep.hasNext();) {
					ITypeBinding exception= excep.next();
					if (exception.isAssignmentCompatible(thrownException))
						excep.remove();
				}
			}
		}
	}
	Collections.sort(exceptions, new ExceptionComparator());
	return exceptions.toArray(new ITypeBinding[exceptions.size()]);
}
 
Example 3
Source File: MissingReturnTypeInLambdaCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected Expression computeProposals(AST ast, ITypeBinding returnBinding, int returnOffset, CompilationUnit root, Expression result) {
	ScopeAnalyzer analyzer= new ScopeAnalyzer(root);
	IBinding[] bindings= analyzer.getDeclarationsInScope(returnOffset, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY);

	org.eclipse.jdt.core.dom.NodeFinder finder= new org.eclipse.jdt.core.dom.NodeFinder(root, returnOffset, 0);
	ASTNode varDeclFrag= ASTResolving.findAncestor(finder.getCoveringNode(), ASTNode.VARIABLE_DECLARATION_FRAGMENT);
	IVariableBinding varDeclFragBinding= null;
	if (varDeclFrag != null)
		varDeclFragBinding= ((VariableDeclarationFragment) varDeclFrag).resolveBinding();
	for (int i= 0; i < bindings.length; i++) {
		IVariableBinding curr= (IVariableBinding) bindings[i];
		ITypeBinding type= curr.getType();
		// Bindings are compared to make sure that a lambda does not return a variable which is yet to be initialised.
		if (type != null && type.isAssignmentCompatible(returnBinding) && testModifier(curr) && !Bindings.equals(curr, varDeclFragBinding)) {
			if (result == null) {
				result= ast.newSimpleName(curr.getName());
			}
			addLinkedPositionProposal(RETURN_EXPRESSION_KEY, curr.getName(), null);
		}
	}
	return result;
}
 
Example 4
Source File: ExceptionAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static void updateExceptionsList(List<ITypeBinding> exceptions, ITypeBinding thrownException) {
	for (Iterator<ITypeBinding> excep = exceptions.iterator(); excep.hasNext();) {
		ITypeBinding exception = excep.next();
		if (exception.isAssignmentCompatible(thrownException)) {
			excep.remove();
		}
	}
}
 
Example 5
Source File: MissingReturnTypeCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
protected Expression computeProposals(AST ast, ITypeBinding returnBinding, int returnOffset, CompilationUnit root, Expression result) {
	ScopeAnalyzer analyzer= new ScopeAnalyzer(root);
	IBinding[] bindings= analyzer.getDeclarationsInScope(returnOffset, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY);
	for (int i= 0; i < bindings.length; i++) {
		IVariableBinding curr= (IVariableBinding) bindings[i];
		ITypeBinding type= curr.getType();
		if (type != null && type.isAssignmentCompatible(returnBinding) && testModifier(curr)) {
			if (result == null) {
				result= ast.newSimpleName(curr.getName());
			}
			addLinkedPositionProposal(RETURN_EXPRESSION_KEY, curr.getName());
		}
	}
	return result;
}
 
Example 6
Source File: SharedUtils.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Check if the method can be called with the given arguments. Used when we don't have
 * a direct link to the method that is invoked (for example: Action.create(...)).
 */
public static boolean isApplicableToCall(Iterable<ITypeBinding> args, IMethodBinding meth) {
	int[] i = { 0 };
	ITypeBinding[] params = meth.getParameterTypes();
	for (ITypeBinding arg : args) {
		if (params.length <= i[0] || !arg.isAssignmentCompatible(params[i[0]++])) {
			return false;
		}
	}
	return true;
}
 
Example 7
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static String getBaseNameFromLocationInParent(Expression assignedExpression, List<Expression> arguments, IMethodBinding binding) {
	if (binding == null)
		return null;

	ITypeBinding[] parameterTypes= binding.getParameterTypes();
	if (parameterTypes.length != arguments.size()) // beware of guessed method bindings
		return null;

	int index= arguments.indexOf(assignedExpression);
	if (index == -1)
		return null;

	ITypeBinding expressionBinding= assignedExpression.resolveTypeBinding();
	if (expressionBinding != null && !expressionBinding.isAssignmentCompatible(parameterTypes[index]))
		return null;

	try {
		IJavaElement javaElement= binding.getJavaElement();
		if (javaElement instanceof IMethod) {
			IMethod method= (IMethod)javaElement;
			if (method.getOpenable().getBuffer() != null) { // avoid dummy names and lookup from Javadoc
				String[] parameterNames= method.getParameterNames();
				if (index < parameterNames.length) {
					return NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, parameterNames[index], method.getJavaProject());
				}
			}
		}
	} catch (JavaModelException e) {
		// ignore
	}
	return null;
}
 
Example 8
Source File: MissingReturnTypeCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected Expression computeProposals(AST ast, ITypeBinding returnBinding, int returnOffset, CompilationUnit root, Expression result) {
	ScopeAnalyzer analyzer= new ScopeAnalyzer(root);
	IBinding[] bindings= analyzer.getDeclarationsInScope(returnOffset, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY);
	for (int i= 0; i < bindings.length; i++) {
		IVariableBinding curr= (IVariableBinding) bindings[i];
		ITypeBinding type= curr.getType();
		if (type != null && type.isAssignmentCompatible(returnBinding) && testModifier(curr)) {
			if (result == null) {
				result= ast.newSimpleName(curr.getName());
			}
			addLinkedPositionProposal(RETURN_EXPRESSION_KEY, curr.getName(), null);
		}
	}
	return result;
}
 
Example 9
Source File: QuickAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean getAssignParamToFieldProposals(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) {
	node = ASTNodes.getNormalizedNode(node);
	ASTNode parent = node.getParent();
	if (!(parent instanceof SingleVariableDeclaration) || !(parent.getParent() instanceof MethodDeclaration)) {
		return false;
	}
	SingleVariableDeclaration paramDecl = (SingleVariableDeclaration) parent;
	IVariableBinding binding = paramDecl.resolveBinding();

	MethodDeclaration methodDecl = (MethodDeclaration) parent.getParent();
	if (binding == null || methodDecl.getBody() == null) {
		return false;
	}
	ITypeBinding typeBinding = binding.getType();
	if (typeBinding == null) {
		return false;
	}

	if (resultingCollections == null) {
		return true;
	}

	ITypeBinding parentType = Bindings.getBindingOfParentType(node);
	if (parentType != null) {
		if (parentType.isInterface()) {
			return false;
		}
		// assign to existing fields
		CompilationUnit root = context.getASTRoot();
		IVariableBinding[] declaredFields = parentType.getDeclaredFields();
		boolean isStaticContext = ASTResolving.isInStaticContext(node);
		for (int i = 0; i < declaredFields.length; i++) {
			IVariableBinding curr = declaredFields[i];
			if (isStaticContext == Modifier.isStatic(curr.getModifiers()) && typeBinding.isAssignmentCompatible(curr.getType())) {
				ASTNode fieldDeclFrag = root.findDeclaringNode(curr);
				if (fieldDeclFrag instanceof VariableDeclarationFragment) {
					VariableDeclarationFragment fragment = (VariableDeclarationFragment) fieldDeclFrag;
					if (fragment.getInitializer() == null) {
						resultingCollections.add(new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, fragment, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_EXISTING_FIELD));
					}
				}
			}
		}
	}

	AssignToVariableAssistProposal fieldProposal = new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, null, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_NEW_FIELD);
	resultingCollections.add(fieldProposal);
	return true;
}
 
Example 10
Source File: AddArgumentCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private boolean canAssign(ITypeBinding curr, ITypeBinding best) {
	return curr.isAssignmentCompatible(best);
}
 
Example 11
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean canAssign(ITypeBinding returnType, ITypeBinding guessedType) {
	return returnType.isAssignmentCompatible(guessedType);
}
 
Example 12
Source File: CodeStyleFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean visit(final MethodInvocation node) {
	if (!fRemoveMethodQualifiers)
		return true;

	Expression expression= node.getExpression();
	if (!(expression instanceof ThisExpression))
		return true;

	final SimpleName name= node.getName();
	if (name.resolveBinding() == null)
		return true;

	if (hasConflict(expression.getStartPosition(), name, ScopeAnalyzer.METHODS | ScopeAnalyzer.CHECK_VISIBILITY))
		return true;

	Name qualifier= ((ThisExpression)expression).getQualifier();
	if (qualifier != null) {
		ITypeBinding declaringClass= ((IMethodBinding)name.resolveBinding()).getDeclaringClass();
		if (declaringClass == null)
			return true;

		ITypeBinding caller= getDeclaringType(node);
		if (caller == null)
			return true;

		ITypeBinding callee= (ITypeBinding)qualifier.resolveBinding();
		if (callee == null)
			return true;

		if (callee.isAssignmentCompatible(declaringClass) && caller.isAssignmentCompatible(declaringClass))
			return true;
	}

	fOperations.add(new CompilationUnitRewriteOperation() {
		@Override
		public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {
			ASTRewrite rewrite= cuRewrite.getASTRewrite();
			TextEditGroup group= createTextEditGroup(FixMessages.CodeStyleFix_removeThis_groupDescription, cuRewrite);
			rewrite.remove(node.getExpression(), group);
		}
	});
	return super.visit(node);
}
 
Example 13
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean getAssignParamToFieldProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
	node= ASTNodes.getNormalizedNode(node);
	ASTNode parent= node.getParent();
	if (!(parent instanceof SingleVariableDeclaration) || !(parent.getParent() instanceof MethodDeclaration)) {
		return false;
	}
	SingleVariableDeclaration paramDecl= (SingleVariableDeclaration) parent;
	IVariableBinding binding= paramDecl.resolveBinding();

	MethodDeclaration methodDecl= (MethodDeclaration) parent.getParent();
	if (binding == null || methodDecl.getBody() == null) {
		return false;
	}
	ITypeBinding typeBinding= binding.getType();
	if (typeBinding == null) {
		return false;
	}

	if (resultingCollections == null) {
		return true;
	}

	ITypeBinding parentType= Bindings.getBindingOfParentType(node);
	if (parentType != null) {
		if (parentType.isInterface()) {
			return false;
		}
		// assign to existing fields
		CompilationUnit root= context.getASTRoot();
		IVariableBinding[] declaredFields= parentType.getDeclaredFields();
		boolean isStaticContext= ASTResolving.isInStaticContext(node);
		for (int i= 0; i < declaredFields.length; i++) {
			IVariableBinding curr= declaredFields[i];
			if (isStaticContext == Modifier.isStatic(curr.getModifiers()) && typeBinding.isAssignmentCompatible(curr.getType())) {
				ASTNode fieldDeclFrag= root.findDeclaringNode(curr);
				if (fieldDeclFrag instanceof VariableDeclarationFragment) {
					VariableDeclarationFragment fragment= (VariableDeclarationFragment) fieldDeclFrag;
					if (fragment.getInitializer() == null) {
						resultingCollections.add(new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, fragment, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_EXISTING_FIELD));
					}
				}
			}
		}
	}

	AssignToVariableAssistProposal fieldProposal= new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, null, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_NEW_FIELD);
	fieldProposal.setCommandId(ASSIGN_PARAM_TO_FIELD_ID);
	resultingCollections.add(fieldProposal);
	return true;
}
 
Example 14
Source File: UnresolvedElementsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean canAssign(ITypeBinding returnType, ITypeBinding guessedType) {
	return returnType.isAssignmentCompatible(guessedType);
}
 
Example 15
Source File: AddArgumentCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean canAssign(ITypeBinding curr, ITypeBinding best) {
	return curr.isAssignmentCompatible(best);
}