org.eclipse.jdt.core.dom.MethodInvocation#resolveMethodBinding ( )源码实例Demo

下面列出了org.eclipse.jdt.core.dom.MethodInvocation#resolveMethodBinding ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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;
}
 
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);
			}
		}
	}
}
 
@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);
}
 
源代码4 项目: JDeodorant   文件: ControlVariable.java
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);
}
 
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;
}
 
源代码6 项目: txtUML   文件: Utils.java
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;
}
 
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);
			}
		}
	}
}
 
源代码8 项目: txtUML   文件: Utils.java
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;
	}
}
 
@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);
}
 
源代码10 项目: JDeodorant   文件: ASTNodeMatcher.java
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;
}
 
@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;
}
 
@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;
}
 
@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);
		}
	}
}
 
源代码14 项目: JDeodorant   文件: ASTNodeMatcher.java
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;
}
 
源代码15 项目: JDeodorant   文件: PDGNode.java
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);
	}
}
 
源代码16 项目: JDeodorant   文件: AbstractLoopUtilities.java
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;
}
 
源代码17 项目: DesigniteJava   文件: MethodInvVisitor.java
@Override
public boolean visit(MethodInvocation method) {
	calledMethods.add(method);
	method.resolveMethodBinding();
	return super.visit(method);
}
 
源代码18 项目: txtUML   文件: SharedUtils.java
public static boolean isActionCall(MethodInvocation methodInvocation) {
	IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
	ITypeBinding declaringClass = methodBinding.getDeclaringClass();
	return typeIsAssignableFrom(declaringClass, hu.elte.txtuml.api.model.Action.class);
}
 
@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;
}
 
/**
 * @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;
	}
}