Java Code Examples for org.eclipse.jdt.core.dom.MethodInvocation#resolveMethodBinding()

The following examples show how to use org.eclipse.jdt.core.dom.MethodInvocation#resolveMethodBinding() . 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: MovedMemberAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean visit(MethodInvocation node) {
	IBinding binding= node.resolveMethodBinding();
	if (isSourceAccess(binding)) {
		if (isMovedMember(binding)) {
			if (node.getExpression() != null)
				rewrite(node, fTarget);
		} else
			rewrite(node, fSource);

	} else if (isTargetAccess(binding)) {
		if (node.getExpression() != null) {
			fCuRewrite.getASTRewrite().remove(node.getExpression(), null);
			fCuRewrite.getImportRemover().registerRemovedNode(node.getExpression());
		}
	}
	return super.visit(node);
}
 
Example 2
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean visit(MethodInvocation node) {
	Expression expression= node.getExpression();
	if (expression == null) {
		IMethodBinding binding= node.resolveMethodBinding();
		if (binding != null) {
			if (isAccessToOuter(binding.getDeclaringClass())) {
				fMethodAccesses.add(node);
			}
		}
	} else {
		expression.accept(this);
	}
	List<Expression> arguments= node.arguments();
	for (int i= 0; i < arguments.size(); i++) {
		arguments.get(i).accept(this);
	}
	return false;
}
 
Example 3
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void modifyAccessToMethodsFromEnclosingInstance(CompilationUnitRewrite targetRewrite, MethodInvocation[] methodInvocations, AbstractTypeDeclaration declaration) {
	IMethodBinding binding= null;
	MethodInvocation invocation= null;
	for (int index= 0; index < methodInvocations.length; index++) {
		invocation= methodInvocations[index];
		binding= invocation.resolveMethodBinding();
		if (binding != null) {
			final Expression target= invocation.getExpression();
			if (target == null) {
				final Expression expression= createAccessExpressionToEnclosingInstanceFieldText(invocation, binding, declaration);
				targetRewrite.getASTRewrite().set(invocation, MethodInvocation.EXPRESSION_PROPERTY, expression, null);
			} else {
				if (!(invocation.getExpression() instanceof ThisExpression) || !(((ThisExpression) invocation.getExpression()).getQualifier() != null))
					continue;
				targetRewrite.getASTRewrite().replace(target, createAccessExpressionToEnclosingInstanceFieldText(invocation, binding, declaration), null);
				targetRewrite.getImportRemover().registerRemovedNode(target);
			}
		}
	}
}
 
Example 4
Source File: AbstractLoopUtilities.java    From JDeodorant with MIT License 5 votes vote down vote up
public static boolean isDataStructureSizeInvocation(Expression expression)
{
	if (expression instanceof MethodInvocation)
	{
		MethodInvocation methodInvocation = (MethodInvocation) expression;
		Expression callingExpression      = methodInvocation.getExpression();
		IMethodBinding methodBinding      = methodInvocation.resolveMethodBinding();
		if (methodBinding != null && callingExpression != null)
		{
			AbstractLoopBindingInformation bindingInformation = AbstractLoopBindingInformation.getInstance();
			return bindingInformation.dataStructureSizeMethodContains(methodBinding.getKey());
		}
	}
	return false;
}
 
Example 5
Source File: PDGNode.java    From JDeodorant with MIT License 5 votes vote down vote up
protected void processArgumentsOfInternalMethodInvocation(MethodInvocationObject methodInvocationObject, AbstractVariable variable) {
	SystemObject systemObject = ASTReader.getSystemObject();
	MethodInvocation methodInvocation = methodInvocationObject.getMethodInvocation();
	IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
	ClassObject classObject = systemObject.getClassObject(methodInvocationObject.getOriginClassName());
	MethodObject methodObject = null;
	if(classObject != null) {
		methodObject = classObject.getMethod(methodInvocationObject);
	}
	if(classObject == null || methodObject != null) {
		//classObject == null => external method call
		//methodObject != null => the internal method might not exist, in the case of built-in enumeration methods, such as values() and valueOf()
		methodCallAnalyzer.processArgumentsOfInternalMethodInvocation(classObject, methodObject, methodInvocation.arguments(), methodBinding, variable);
	}
}
 
Example 6
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean getterMethodForField(MethodInvocation methodInvocation, SimpleName fieldName) {
	IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
	ITypeBinding declaringClassTypeBinding = methodBinding.getDeclaringClass();
	ClassObject declaringClass = ASTReader.getSystemObject().getClassObject(declaringClassTypeBinding.getQualifiedName());
	if(declaringClass != null) {
		ListIterator<MethodObject> methodIterator = declaringClass.getMethodIterator();
		while(methodIterator.hasNext()) {
			MethodObject method = methodIterator.next();
			MethodDeclaration methodDeclaration = method.getMethodDeclaration();
			if(methodDeclaration.resolveBinding().isEqualTo(methodInvocation.resolveMethodBinding())) {
				SimpleName getField = MethodDeclarationUtility.isGetter(methodDeclaration);
				if(getField != null) {
					if(getField.resolveBinding().getKind() == IBinding.VARIABLE &&
							fieldName.resolveBinding().getKind() == IBinding.VARIABLE) {
						IVariableBinding getFieldBinding = (IVariableBinding)getField.resolveBinding();
						IVariableBinding fieldNameBinding = (IVariableBinding)fieldName.resolveBinding();
						if(getFieldBinding.isEqualTo(fieldNameBinding) ||
								(getField.getIdentifier().equals(fieldName.getIdentifier()) &&
								getFieldBinding.getType().isEqualTo(fieldNameBinding.getType()) && getFieldBinding.getType().getQualifiedName().equals(fieldNameBinding.getType().getQualifiedName()))) {
							return true;
						}
					}
				}
			}
		}
	}
	return false;
}
 
Example 7
Source File: SuperTypeConstraintsCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final void endVisit(final MethodInvocation node) {
	final IMethodBinding binding= node.resolveMethodBinding();
	if (binding != null) {
		endVisit(node, binding);
		endVisit(node.arguments(), binding);
		final Expression expression= node.getExpression();
		if (expression != null) {
			final ConstraintVariable2 descendant= (ConstraintVariable2) expression.getProperty(PROPERTY_CONSTRAINT_VARIABLE);
			if (descendant != null)
				endVisit(binding, descendant);
		}
	}
}
 
Example 8
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final boolean visit(final MethodInvocation node) {
	Assert.isNotNull(node);
	final Expression expression= node.getExpression();
	final IMethodBinding binding= node.resolveMethodBinding();
	if (binding == null || !Modifier.isStatic(binding.getModifiers()) && Bindings.equals(binding, fBinding) && (expression == null || expression instanceof ThisExpression)) {
		fStatus.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.MoveInstanceMethodProcessor_potentially_recursive, JavaStatusContext.create(fMethod.getCompilationUnit(), node)));
		fResult.add(node);
		return false;
	}
	return true;
}
 
Example 9
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final boolean visit(final MethodInvocation node) {
	Assert.isNotNull(node);
	if (fAnonymousClass > 0) {
		final IMethodBinding binding= node.resolveMethodBinding();
		if (binding != null) {
			if (node.getExpression() == null && !Modifier.isStatic(binding.getModifiers()))
				fResult.add(node.getName());
		}
	}
	return true;
}
 
Example 10
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 5 votes vote down vote up
private SimpleName setterMethodForField(MethodInvocation methodInvocation, SimpleName fieldName) {
	IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
	ITypeBinding declaringClassTypeBinding = methodBinding.getDeclaringClass();
	ClassObject declaringClass = ASTReader.getSystemObject().getClassObject(declaringClassTypeBinding.getQualifiedName());
	if(declaringClass != null) {
		ListIterator<MethodObject> methodIterator = declaringClass.getMethodIterator();
		while(methodIterator.hasNext()) {
			MethodObject method = methodIterator.next();
			MethodDeclaration methodDeclaration = method.getMethodDeclaration();
			if(methodDeclaration.resolveBinding().isEqualTo(methodInvocation.resolveMethodBinding())) {
				SimpleName setField = MethodDeclarationUtility.isSetter(methodDeclaration);
				if(setField != null) {
					if(setField.resolveBinding().getKind() == IBinding.VARIABLE &&
							fieldName.resolveBinding().getKind() == IBinding.VARIABLE) {
						IVariableBinding setFieldBinding = (IVariableBinding)setField.resolveBinding();
						IVariableBinding fieldNameBinding = (IVariableBinding)fieldName.resolveBinding();
						if(setFieldBinding.isEqualTo(fieldNameBinding) ||
								(setField.getIdentifier().equals(fieldName.getIdentifier()) &&
								setFieldBinding.getType().isEqualTo(fieldNameBinding.getType()) && setFieldBinding.getType().getQualifiedName().equals(fieldNameBinding.getType().getQualifiedName()))) {
							return setField;
						}
					}
				}
			}
		}
	}
	return null;
}
 
Example 11
Source File: ReferenceAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(MethodInvocation node) {
	IMethodBinding binding= node.resolveMethodBinding();
	if (binding != null) {
		binding= binding.getMethodDeclaration();
		if (isMovedMember(binding))
			rewrite(node, fTarget);
	}
	return super.visit(node);
}
 
Example 12
Source File: Utils.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
public static Type getReturnTypeFromInvocation(MethodInvocation methodInvocation) {
	IMethodBinding binding = methodInvocation.resolveMethodBinding();
	try {
		CompilationUnit cu = getCompilationUnit(binding);
		MethodDeclaration decl = (MethodDeclaration) cu.findDeclaringNode(binding.getKey());
		Type returnType = decl.getReturnType2();
		return returnType;
	} catch (NullPointerException ex) {
		return null;
	}
}
 
Example 13
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void addExplicitTypeArgumentsIfNecessary(ASTRewrite rewrite, ASTRewriteCorrectionProposal proposal, Expression invocation) {
	if (Invocations.isResolvedTypeInferredFromExpectedType(invocation)) {
		ITypeBinding[] typeArguments= Invocations.getInferredTypeArguments(invocation);
		if (typeArguments == null)
			return;
		
		ImportRewrite importRewrite= proposal.getImportRewrite();
		if (importRewrite == null) {
			importRewrite= proposal.createImportRewrite((CompilationUnit) invocation.getRoot());
		}
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(invocation, importRewrite);
		
		AST ast= invocation.getAST();
		ListRewrite typeArgsRewrite= Invocations.getInferredTypeArgumentsRewrite(rewrite, invocation);
		
		for (int i= 0; i < typeArguments.length; i++) {
			Type typeArgumentNode= importRewrite.addImport(typeArguments[i], ast, importRewriteContext);
			typeArgsRewrite.insertLast(typeArgumentNode, null);
		}
		
		if (invocation instanceof MethodInvocation) {
			MethodInvocation methodInvocation= (MethodInvocation) invocation;
			Expression expression= methodInvocation.getExpression();
			if (expression == null) {
				IMethodBinding methodBinding= methodInvocation.resolveMethodBinding();
				if (methodBinding != null && Modifier.isStatic(methodBinding.getModifiers())) {
					expression= ast.newName(importRewrite.addImport(methodBinding.getDeclaringClass().getTypeDeclaration(), importRewriteContext));
				} else {
					expression= ast.newThisExpression();
				}
				rewrite.set(invocation, MethodInvocation.EXPRESSION_PROPERTY, expression, null);
			}
		}
	}
}
 
Example 14
Source File: Utils.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isParInvocation(Statement statement) {
	MethodInvocation parInvocation = getMethodInvocationFromStatement(statement);
	if (parInvocation != null) {
		IMethodBinding methodBinding = parInvocation.resolveMethodBinding();
		if (methodBinding == null) {
			return false;

		}
		return methodBinding.getName().equals("par")
				&& methodBinding.getDeclaringClass().getQualifiedName().equals(Sequence.class.getCanonicalName());
	}
	return false;
}
 
Example 15
Source File: ConvertIterableLoopOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IStatus checkExpressionCondition() {
	Expression expression= getForStatement().getExpression();
	if (!(expression instanceof MethodInvocation))
		return SEMANTIC_CHANGE_WARNING_STATUS;

	MethodInvocation invoc= (MethodInvocation)expression;
	IMethodBinding methodBinding= invoc.resolveMethodBinding();
	if (methodBinding == null)
		return ERROR_STATUS;

	ITypeBinding declaringClass= methodBinding.getDeclaringClass();
	if (declaringClass == null)
		return ERROR_STATUS;

	String qualifiedName= declaringClass.getQualifiedName();
	String methodName= invoc.getName().getIdentifier();
	if (qualifiedName.startsWith("java.util.Enumeration")) { //$NON-NLS-1$
		if (!methodName.equals("hasMoreElements")) //$NON-NLS-1$
			return SEMANTIC_CHANGE_WARNING_STATUS;
	} else if (qualifiedName.startsWith("java.util.Iterator")) { //$NON-NLS-1$
		if (!methodName.equals("hasNext")) //$NON-NLS-1$
			return SEMANTIC_CHANGE_WARNING_STATUS;
		return checkIteratorCondition();
	} else {
		return SEMANTIC_CHANGE_WARNING_STATUS;
	}

	return StatusInfo.OK_STATUS;
}
 
Example 16
Source File: ControlVariable.java    From JDeodorant with MIT License 5 votes vote down vote up
private static List<Expression> getAllFirstLevelUpdaters(Statement statement)
{
	List<Expression> updaters               = new ArrayList<Expression>();
	ExpressionExtractor expressionExtractor = new ExpressionExtractor();
	List<Statement> innerStatements         = new ArrayList<Statement>();
	innerStatements.add(statement);
	innerStatements = AbstractLoopUtilities.unBlock(innerStatements);
	// get all first level PrefixExpressions, PostfixExpressions, Assignments, and next() MethodInvocations from each inner statement
	for (Statement currentStatement : innerStatements)
	{
		// only updaters in an ExpressionStatment or VariableDeclaration are first level, unless a ConditionalExpression (handled in return statement)
		if (currentStatement instanceof ExpressionStatement || currentStatement instanceof VariableDeclarationStatement)
		{
			updaters.addAll(expressionExtractor.getPrefixExpressions(currentStatement));
			updaters.addAll(expressionExtractor.getPostfixExpressions(currentStatement));
			updaters.addAll(expressionExtractor.getAssignments(currentStatement));
			
			List<Expression> methodInvocations = expressionExtractor.getMethodInvocations(currentStatement);
			for (Expression currentExpression : methodInvocations)
			{
				if (currentExpression instanceof MethodInvocation)
				{
					MethodInvocation currentMethodInvocation      = (MethodInvocation) currentExpression;
					IMethodBinding currentMethodInvocationBinding = currentMethodInvocation.resolveMethodBinding();
					AbstractLoopBindingInformation bindingInformation = AbstractLoopBindingInformation.getInstance();
					if (bindingInformation.updateMethodValuesContains(currentMethodInvocationBinding.getMethodDeclaration().getKey()))
					{
						updaters.add(currentMethodInvocation);
					}
				}
			}
		}
	}
	return removeExpressionsInAConditionalExpression(updaters, statement);
}
 
Example 17
Source File: MethodInvVisitor.java    From DesigniteJava with Apache License 2.0 4 votes vote down vote up
@Override
public boolean visit(MethodInvocation method) {
	calledMethods.add(method);
	method.resolveMethodBinding();
	return super.visit(method);
}
 
Example 18
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public final boolean visit(final MethodInvocation node) {
	Assert.isNotNull(node);
	final Expression expression= node.getExpression();
	final IMethodBinding method= node.resolveMethodBinding();
	if (method != null) {
		final ASTRewrite rewrite= fRewrite;
		if (expression == null) {
			final AST ast= node.getAST();
			if (!JdtFlags.isStatic(method))
				rewrite.set(node, MethodInvocation.EXPRESSION_PROPERTY, ast.newSimpleName(fTargetName), null);
			else if (!fStaticImports.contains(method)) {
				ITypeBinding declaring= method.getDeclaringClass();
				if (declaring != null) {
					IType type= (IType) declaring.getJavaElement();
					if (type != null) {
						rewrite.set(node, MethodInvocation.EXPRESSION_PROPERTY, ast.newName(type.getTypeQualifiedName('.')), null);
					}
				}
			}
			return true;
		} else {
			if (expression instanceof FieldAccess) {
				final FieldAccess access= (FieldAccess) expression;
				if (Bindings.equals(fTarget, access.resolveFieldBinding())) {
					rewrite.remove(expression, null);
					visit(node.arguments());
					return false;
				}
			} else if (expression instanceof Name) {
				final Name name= (Name) expression;
				if (Bindings.equals(fTarget, name.resolveBinding())) {
					rewrite.remove(expression, null);
					visit(node.arguments());
					return false;
				}
			}
		}
	}
	return true;
}
 
Example 19
Source File: CallInliner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * @param status the status
 * @return <code>true</code> if explicit cast is needed otherwise <code>false</code>
 */
private boolean needsExplicitCast(RefactoringStatus status) {
	// if the return type of the method is the same as the type of the
	// returned expression then we don't need an explicit cast.
	if (fSourceProvider.returnTypeMatchesReturnExpressions())
			return false;

	List<Expression> returnExprs= fSourceProvider.getReturnExpressions();
	// it is inferred that only methods consisting of a single
	// return statement can be inlined as parameters in other
	// method invocations
	if (returnExprs.size() != 1)
		return false;

	if (fTargetNode.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) {
		MethodInvocation methodInvocation= (MethodInvocation)fTargetNode.getParent();
		if(methodInvocation.getExpression() == fTargetNode)
			return false;
		IMethodBinding method= methodInvocation.resolveMethodBinding();
		if (method == null) {
			status.addError(RefactoringCoreMessages.CallInliner_cast_analysis_error,
				JavaStatusContext.create(fCUnit, methodInvocation));
			return false;
		}
		ITypeBinding[] parameters= method.getParameterTypes();
		int argumentIndex= methodInvocation.arguments().indexOf(fInvocation);

		ITypeBinding parameterType= returnExprs.get(0).resolveTypeBinding();
		if (method.isVarargs() && argumentIndex >= parameters.length - 1) {
			argumentIndex= parameters.length - 1;
			parameterType= parameterType.createArrayType(1);
		}
		parameters[argumentIndex]= parameterType;

		ITypeBinding type= ASTNodes.getReceiverTypeBinding(methodInvocation);
		TypeBindingVisitor visitor= new AmbiguousMethodAnalyzer(
			fTypeEnvironment, method, fTypeEnvironment.create(parameters));
		if(!visitor.visit(type)) {
			return true;
		} else if (type.isInterface()) {
			return !Bindings.visitInterfaces(type, visitor);
		} else if (Modifier.isAbstract(type.getModifiers())) {
			return !Bindings.visitHierarchy(type, visitor);
		} else {
			// it is not needed to visit interfaces if receiver is a concrete class
			return !Bindings.visitSuperclasses(type, visitor);
		}
	} else {
		ITypeBinding explicitCast= ASTNodes.getExplicitCast(returnExprs.get(0), (Expression)fTargetNode);
		return explicitCast != null;
	}
}
 
Example 20
Source File: SharedUtils.java    From txtUML with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean isActionCall(MethodInvocation methodInvocation) {
	IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
	ITypeBinding declaringClass = methodBinding.getDeclaringClass();
	return typeIsAssignableFrom(declaringClass, hu.elte.txtuml.api.model.Action.class);
}