org.eclipse.jdt.core.dom.MethodInvocation Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.MethodInvocation. 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: CodeStyleFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 7 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean visit(MethodInvocation node) {
	if (!fFindUnqualifiedMethodAccesses && !fFindUnqualifiedStaticMethodAccesses)
		return true;

	if (node.getExpression() != null)
		return true;

	IBinding binding= node.getName().resolveBinding();
	if (!(binding instanceof IMethodBinding))
		return true;

	handleMethod(node.getName(), (IMethodBinding)binding);
	return true;
}
 
Example #2
Source File: DelegateMethodCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 7 votes vote down vote up
/**
 * Creates the corresponding statement for the method invocation, based on
 * the return type.
 *
 * @param declaration the method declaration where the invocation statement
 *            is inserted
 * @param invocation the method invocation being encapsulated by the
 *            resulting statement
 * @return the corresponding statement
 */
protected Statement createMethodInvocation(final MethodDeclaration declaration, final MethodInvocation invocation) {
	Assert.isNotNull(declaration);
	Assert.isNotNull(invocation);
	Statement statement= null;
	final Type type= declaration.getReturnType2();
	if (type == null) {
		statement= createExpressionStatement(invocation);
	} else {
		if (type instanceof PrimitiveType) {
			final PrimitiveType primitive= (PrimitiveType) type;
			if (primitive.getPrimitiveTypeCode().equals(PrimitiveType.VOID)) {
				statement= createExpressionStatement(invocation);
			} else {
				statement= createReturnStatement(invocation);
			}
		} else {
			statement= createReturnStatement(invocation);
		}
	}
	return statement;
}
 
Example #3
Source File: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 7 votes vote down vote up
private Statement createAddArrayHashCode(IVariableBinding binding) {
	MethodInvocation invoc= fAst.newMethodInvocation();
	if (JavaModelUtil.is50OrHigher(fRewrite.getCu().getJavaProject())) {
		invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE));
		invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS));
		invoc.arguments().add(getThisAccessForHashCode(binding.getName()));
	} else {
		invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE));
		final IJavaElement element= fType.getJavaElement();
		if (element != null && !"".equals(element.getElementName())) //$NON-NLS-1$
			invoc.setExpression(fAst.newSimpleName(element.getElementName()));
		invoc.arguments().add(getThisAccessForHashCode(binding.getName()));
		ITypeBinding type= binding.getType().getElementType();
		if (!Bindings.isVoidType(type)) {
			if (!type.isPrimitive() || binding.getType().getDimensions() >= 2)
				type= fAst.resolveWellKnownType(JAVA_LANG_OBJECT);
			if (!fCustomHashCodeTypes.contains(type))
				fCustomHashCodeTypes.add(type);
		}
	}
	return prepareAssignment(invoc);
}
 
Example #4
Source File: TryPurifyByException.java    From SimFix with GNU General Public License v2.0 7 votes vote down vote up
private Set<Integer> findAllMethodCall(){
	Set<Integer> methodStmt = new HashSet<>();
	if(_backupBody != null){
		Block body = _backupBody;
		for(int i = 0; i < body.statements().size(); i++){
			ASTNode stmt = (ASTNode) body.statements().get(i);
			if(stmt instanceof ExpressionStatement){
				stmt = ((ExpressionStatement) stmt).getExpression();
				if(stmt instanceof MethodInvocation){
					methodStmt.add(i);
				} else if(stmt instanceof Assignment){
					Assignment assign = (Assignment) stmt;
					if(assign.getRightHandSide() instanceof MethodInvocation){
						methodStmt.add(i);
					}
				}
			}
		}
		
	}
	return methodStmt;
}
 
Example #5
Source File: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 7 votes vote down vote up
public static Expression getExpression(ASTNode invocation) {
	switch (invocation.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
			return ((MethodInvocation)invocation).getExpression();
		case ASTNode.SUPER_METHOD_INVOCATION:
			return null;
			
		case ASTNode.CONSTRUCTOR_INVOCATION:
			return null;
		case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
			return ((SuperConstructorInvocation)invocation).getExpression();
			
		case ASTNode.CLASS_INSTANCE_CREATION:
			return ((ClassInstanceCreation)invocation).getExpression();
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return null;
			
		default:
			throw new IllegalArgumentException(invocation.toString());
	}
}
 
Example #6
Source File: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 7 votes vote down vote up
private Statement createArrayComparison(String name) {
	MethodInvocation invoc= fAst.newMethodInvocation();
	invoc.setName(fAst.newSimpleName(METHODNAME_EQUALS));
	invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS));
	invoc.arguments().add(getThisAccessForEquals(name));
	invoc.arguments().add(getOtherAccess(name));

	PrefixExpression pe= fAst.newPrefixExpression();
	pe.setOperator(PrefixExpression.Operator.NOT);
	pe.setOperand(invoc);

	IfStatement ifSt= fAst.newIfStatement();
	ifSt.setExpression(pe);
	ifSt.setThenStatement(getThenStatement(getReturnFalse()));

	return ifSt;
}
 
Example #7
Source File: GenerateForLoopAssistProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Helper to generate an index based <code>for</code> loop to iterate over a {@link List}
 * implementation.
 * 
 * @param ast the current {@link AST} instance to generate the {@link ASTRewrite} for
 * @return an applicable {@link ASTRewrite} instance
 */
private ASTRewrite generateIndexBasedForRewrite(AST ast) {
	ASTRewrite rewrite= ASTRewrite.create(ast);

	ForStatement loopStatement= ast.newForStatement();
	SimpleName loopVariableName= resolveLinkedVariableNameWithProposals(rewrite, "int", null, true); //$NON-NLS-1$
	loopStatement.initializers().add(getForInitializer(ast, loopVariableName));

	MethodInvocation listSizeExpression= ast.newMethodInvocation();
	listSizeExpression.setName(ast.newSimpleName("size")); //$NON-NLS-1$
	Expression listExpression= (Expression) rewrite.createCopyTarget(fCurrentExpression);
	listSizeExpression.setExpression(listExpression);

	loopStatement.setExpression(getLinkedInfixExpression(rewrite, loopVariableName.getIdentifier(), listSizeExpression, InfixExpression.Operator.LESS));
	loopStatement.updaters().add(getLinkedIncrementExpression(rewrite, loopVariableName.getIdentifier()));

	Block forLoopBody= ast.newBlock();
	forLoopBody.statements().add(ast.newExpressionStatement(getIndexBasedForBodyAssignment(rewrite, loopVariableName)));
	forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
	loopStatement.setBody(forLoopBody);
	rewrite.replace(fCurrentNode, loopStatement, null);

	return rewrite;
}
 
Example #8
Source File: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isResolvedTypeInferredFromExpectedType(Expression invocation) {
	if (invocation == null)
		return false;
	
	switch (invocation.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
			return ((MethodInvocation) invocation).isResolvedTypeInferredFromExpectedType();
		case ASTNode.SUPER_METHOD_INVOCATION:
			return ((SuperMethodInvocation) invocation).isResolvedTypeInferredFromExpectedType();
		case ASTNode.CLASS_INSTANCE_CREATION:
			return ((ClassInstanceCreation) invocation).isResolvedTypeInferredFromExpectedType();
			
		default:
			return false;
	}
}
 
Example #9
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 #10
Source File: SignatureHelpHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private IMethod getMethod(ASTNode node) throws JavaModelException {
	IBinding binding;
	if (node instanceof MethodInvocation) {
		binding = ((MethodInvocation) node).resolveMethodBinding();
	} else if (node instanceof MethodRef) {
		binding = ((MethodRef) node).resolveBinding();
	} else if (node instanceof ClassInstanceCreation) {
		binding = ((ClassInstanceCreation) node).resolveConstructorBinding();
	} else {
		binding = null;
	}
	if (binding != null) {
		IJavaElement javaElement = binding.getJavaElement();
		if (javaElement instanceof IMethod) {
			IMethod method = (IMethod) javaElement;
			return method;
		}
	}
	return null;
}
 
Example #11
Source File: InlineMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public RefactoringStatus setCurrentMode(Mode mode) throws JavaModelException {
	if (fCurrentMode == mode)
		return new RefactoringStatus();
	Assert.isTrue(getInitialMode() == Mode.INLINE_SINGLE);
	fCurrentMode= mode;
	if (mode == Mode.INLINE_SINGLE) {
		if (fInitialNode instanceof MethodInvocation)
			fTargetProvider= TargetProvider.create((ICompilationUnit) fInitialTypeRoot, (MethodInvocation)fInitialNode);
		else if (fInitialNode instanceof SuperMethodInvocation)
			fTargetProvider= TargetProvider.create((ICompilationUnit) fInitialTypeRoot, (SuperMethodInvocation)fInitialNode);
		else if (fInitialNode instanceof ConstructorInvocation)
			fTargetProvider= TargetProvider.create((ICompilationUnit) fInitialTypeRoot, (ConstructorInvocation)fInitialNode);
		else
			throw new IllegalStateException(String.valueOf(fInitialNode));
	} else {
		fTargetProvider= TargetProvider.create(fSourceProvider.getDeclaration());
	}
	return fTargetProvider.checkActivation();
}
 
Example #12
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 #13
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 6 votes vote down vote up
protected boolean isTypeHolder(Object o) {
	if(o.getClass().equals(MethodInvocation.class) || o.getClass().equals(SuperMethodInvocation.class)			
			|| o.getClass().equals(NumberLiteral.class) || o.getClass().equals(StringLiteral.class)
			|| o.getClass().equals(CharacterLiteral.class) || o.getClass().equals(BooleanLiteral.class)
			|| o.getClass().equals(TypeLiteral.class) || o.getClass().equals(NullLiteral.class)
			|| o.getClass().equals(ArrayCreation.class)
			|| o.getClass().equals(ClassInstanceCreation.class)
			|| o.getClass().equals(ArrayAccess.class) || o.getClass().equals(FieldAccess.class)
			|| o.getClass().equals(SuperFieldAccess.class) || o.getClass().equals(ParenthesizedExpression.class)
			|| o.getClass().equals(SimpleName.class) || o.getClass().equals(QualifiedName.class)
			|| o.getClass().equals(CastExpression.class) || o.getClass().equals(InfixExpression.class)
			|| o.getClass().equals(PrefixExpression.class) || o.getClass().equals(InstanceofExpression.class)
			|| o.getClass().equals(ThisExpression.class) || o.getClass().equals(ConditionalExpression.class))
		return true;
	return false;
}
 
Example #14
Source File: ExpressionVariable.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static IBinding resolveBinding(Expression expression){
	if (expression instanceof Name)
		return ((Name)expression).resolveBinding();
	if (expression instanceof ParenthesizedExpression)
		return resolveBinding(((ParenthesizedExpression)expression).getExpression());
	else if (expression instanceof Assignment)
		return resolveBinding(((Assignment)expression).getLeftHandSide());//TODO ???
	else if (expression instanceof MethodInvocation)
		return ((MethodInvocation)expression).resolveMethodBinding();
	else if (expression instanceof SuperMethodInvocation)
		return ((SuperMethodInvocation)expression).resolveMethodBinding();
	else if (expression instanceof FieldAccess)
		return ((FieldAccess)expression).resolveFieldBinding();
	else if (expression instanceof SuperFieldAccess)
		return ((SuperFieldAccess)expression).resolveFieldBinding();
	else if (expression instanceof ConditionalExpression)
		return resolveBinding(((ConditionalExpression)expression).getThenExpression());
	return null;
}
 
Example #15
Source File: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static ListRewrite getInferredTypeArgumentsRewrite(ASTRewrite rewrite, Expression invocation) {
	switch (invocation.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
			return rewrite.getListRewrite(invocation, MethodInvocation.TYPE_ARGUMENTS_PROPERTY);
		case ASTNode.SUPER_METHOD_INVOCATION:
			return rewrite.getListRewrite(invocation, SuperMethodInvocation.TYPE_ARGUMENTS_PROPERTY);
		case ASTNode.CLASS_INSTANCE_CREATION:
			Type type= ((ClassInstanceCreation) invocation).getType();
			return rewrite.getListRewrite(type, ParameterizedType.TYPE_ARGUMENTS_PROPERTY);
			
		default:
			throw new IllegalArgumentException(invocation.toString());
	}
}
 
Example #16
Source File: BindingSignatureVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(MethodInvocation expr) {
	if (expr.getExpression() != null) {
		handleExpression(expr.getExpression());
	}
	handleExpression(expr.getName());
	handleParameters(expr.arguments());
	return false;
}
 
Example #17
Source File: Slicer.java    From SnowGraph with Apache License 2.0 5 votes vote down vote up
private void genereateAllAPIs() {
	allAPIs = new HashSet<>();
	List<MethodInvocation> invocations = methodAST.getMethodInvocations();
	for (int j = 0; j < invocations.size(); j++) {
		APIMethodData apiMethodData = new APIMethodData();

		IMethodBinding mBinding = invocations.get(j).resolveMethodBinding();
		if (mBinding != null) {
			apiMethodData.initByIMethodBinding(mBinding);
			allAPIs.add(apiMethodData);
		}
	}
}
 
Example #18
Source File: InvertBooleanUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static Expression getBooleanExpression(ASTNode node) {
	if (!(node instanceof Expression)) {
		return null;
	}

	// check if the node is a location where it can be negated
	StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
	if (locationInParent == QualifiedName.NAME_PROPERTY) {
		node = node.getParent();
		locationInParent = node.getLocationInParent();
	}
	while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
		node = node.getParent();
		locationInParent = node.getLocationInParent();
	}
	Expression expression = (Expression) node;
	if (!isBoolean(expression)) {
		return null;
	}
	if (expression.getParent() instanceof InfixExpression) {
		return expression;
	}
	if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY
			|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY
			|| locationInParent == MethodInvocation.ARGUMENTS_PROPERTY || locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY
			|| locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY
			|| locationInParent == ConditionalExpression.EXPRESSION_PROPERTY || locationInParent == PrefixExpression.OPERAND_PROPERTY) {
		return expression;
	}
	return null;
}
 
Example #19
Source File: InlineConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(MethodInvocation invocation) {
	if (invocation.getExpression() == null)
		qualifyUnqualifiedMemberNameIfNecessary(invocation.getName());
	else
		invocation.getExpression().accept(this);

	for (Iterator<Expression> it= invocation.arguments().iterator(); it.hasNext();)
		it.next().accept(this);

	return false;
}
 
Example #20
Source File: AstVisitor.java    From jdt2famix with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * handles object.method(parameter)
 */
@SuppressWarnings("unchecked")
@Override
public boolean visit(MethodInvocation node) {
	if (importer.topOfContainerStack() instanceof Method) {
		Invocation invocation = importer.createInvocationFromMethodBinding(node.resolveMethodBinding(),
				node.toString().trim());
		importer.createLightweightSourceAnchor(invocation, node.getName());
		importer.createAccessFromExpression(node.getExpression());
		invocation.setReceiver(importer.ensureStructuralEntityFromExpression(node.getExpression()));
		node.arguments().stream().forEach(arg -> importer.createAccessFromExpression((Expression) arg));
	}
	return true;
}
 
Example #21
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks
 * <ul>
 * <li>whether the given infix expression is the argument of a StringBuilder#append() or
 * StringBuffer#append() invocation, and</li>
 * <li>the append method is called on a simple variable, and</li>
 * <li>the invocation occurs in a statement (not as nested expression)</li>
 * </ul>
 *
 * @param infixExpression the infix expression
 * @return the name of the variable we were appending to, or <code>null</code> if not matching
 */
private static SimpleName getEnclosingAppendBuffer(InfixExpression infixExpression) {
	if (infixExpression.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) {
		MethodInvocation methodInvocation= (MethodInvocation)infixExpression.getParent();

		// ..not in an expression.. (e.g. not sb.append("high" + 5).append(6);)
		if (methodInvocation.getParent() instanceof Statement) {

			// ..of a function called append:
			if ("append".equals(methodInvocation.getName().getIdentifier())) { //$NON-NLS-1$
				Expression expression= methodInvocation.getExpression();

				// ..and the append is being called on a Simple object:
				if (expression instanceof SimpleName) {
					IBinding binding= ((SimpleName)expression).resolveBinding();
					if (binding instanceof IVariableBinding) {
						String typeName= ((IVariableBinding)binding).getType().getQualifiedName();

						// And the object's type is a StringBuilder or StringBuffer:
						if ("java.lang.StringBuilder".equals(typeName) || "java.lang.StringBuffer".equals(typeName)) { //$NON-NLS-1$ //$NON-NLS-2$
							return (SimpleName)expression;
						}
					}
				}
			}
		}
	}
	return null;
}
 
Example #22
Source File: OperationInvocation.java    From RefactoringMiner with MIT License 5 votes vote down vote up
public OperationInvocation(CompilationUnit cu, String filePath, MethodInvocation invocation) {
	this.locationInfo = new LocationInfo(cu, filePath, invocation, CodeElementType.METHOD_INVOCATION);
	this.methodName = invocation.getName().getIdentifier();
	this.typeArguments = invocation.arguments().size();
	this.arguments = new ArrayList<String>();
	List<Expression> args = invocation.arguments();
	for(Expression argument : args) {
		this.arguments.add(argument.toString());
	}
	if(invocation.getExpression() != null) {
		this.expression = invocation.getExpression().toString();
		processExpression(invocation.getExpression(), this.subExpressions);
	}
}
 
Example #23
Source File: PullUpRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final boolean visit(final SuperMethodInvocation node) {
	if (!fAnonymousClassDeclaration && !fTypeDeclarationStatement) {
		final IBinding superBinding= node.getName().resolveBinding();
		if (superBinding instanceof IMethodBinding) {
			final IMethodBinding extended= (IMethodBinding) superBinding;
			if (fEnclosingMethod != null && fEnclosingMethod.overrides(extended))
				return true;
			final ITypeBinding declaringBinding= extended.getDeclaringClass();
			if (declaringBinding != null) {
				final IType type= (IType) declaringBinding.getJavaElement();
				if (!fSuperReferenceType.equals(type))
					return true;
			}
		}
		final AST ast= node.getAST();
		final ThisExpression expression= ast.newThisExpression();
		final MethodInvocation invocation= ast.newMethodInvocation();
		final SimpleName simple= ast.newSimpleName(node.getName().getIdentifier());
		invocation.setName(simple);
		invocation.setExpression(expression);
		final List<Expression> arguments= node.arguments();
		if (arguments != null && arguments.size() > 0) {
			final ListRewrite rewriter= fRewrite.getListRewrite(invocation, MethodInvocation.ARGUMENTS_PROPERTY);
			ListRewrite superRewriter= fRewrite.getListRewrite(node, SuperMethodInvocation.ARGUMENTS_PROPERTY);
			ASTNode copyTarget= superRewriter.createCopyTarget(arguments.get(0), arguments.get(arguments.size() - 1));
			rewriter.insertLast(copyTarget, null);
		}
		fRewrite.replace(node, invocation, null);
		if (!fSourceRewriter.getCu().equals(fTargetRewriter.getCu()))
			fSourceRewriter.getImportRemover().registerRemovedNode(node);
		return true;
	}
	return false;
}
 
Example #24
Source File: TypeCheckCodeFragmentAnalyzer.java    From JDeodorant with MIT License 5 votes vote down vote up
private Expression extractOperand(Expression operand) {
	if(operand instanceof SimpleName) {
		SimpleName operandSimpleName = (SimpleName)operand;
		return operandSimpleName;
	}
	else if(operand instanceof QualifiedName) {
		QualifiedName operandQualifiedName = (QualifiedName)operand;
		return operandQualifiedName.getName();
	}
	else if(operand instanceof FieldAccess) {
		FieldAccess operandFieldAccess = (FieldAccess)operand;
		return operandFieldAccess.getName();
	}
	else if(operand instanceof MethodInvocation) {
		MethodInvocation methodInvocation = (MethodInvocation)operand;
		for(MethodDeclaration method : methods) {
			SimpleName fieldInstruction = MethodDeclarationUtility.isGetter(method);
			if(fieldInstruction != null && method.resolveBinding().isEqualTo(methodInvocation.resolveMethodBinding())) {
				return fieldInstruction;
			}
			MethodInvocation delegateMethodInvocation = MethodDeclarationUtility.isDelegate(method);
			if(delegateMethodInvocation != null && method.resolveBinding().isEqualTo(methodInvocation.resolveMethodBinding())) {
				return delegateMethodInvocation;
			}
		}
		return methodInvocation;
	}
	return null;
}
 
Example #25
Source File: NewBuilderAndWithMethodCallCreationFragment.java    From SparkBuilderGenerator with MIT License 5 votes vote down vote up
public Block createReturnBlock(AST ast, TypeDeclaration builderType, String withName, String parameterName) {
    Block builderMethodBlock = ast.newBlock();
    ReturnStatement returnStatement = ast.newReturnStatement();
    ClassInstanceCreation newClassInstanceCreation = ast.newClassInstanceCreation();
    newClassInstanceCreation.setType(ast.newSimpleType(ast.newName(builderType.getName().toString())));

    MethodInvocation withMethodInvocation = ast.newMethodInvocation();
    withMethodInvocation.setExpression(newClassInstanceCreation);
    withMethodInvocation.setName(ast.newSimpleName(withName));
    withMethodInvocation.arguments().add(ast.newSimpleName(parameterName));

    returnStatement.setExpression(withMethodInvocation);
    builderMethodBlock.statements().add(returnStatement);
    return builderMethodBlock;
}
 
Example #26
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 #27
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static ASTNode getInlineableMethodNode(ASTNode node, IJavaElement unit) {
	if (node == null) {
		return null;
	}
	switch (node.getNodeType()) {
		case ASTNode.SIMPLE_NAME:
			StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
			if (locationInParent == MethodDeclaration.NAME_PROPERTY) {
				return node.getParent();
			} else if (locationInParent == MethodInvocation.NAME_PROPERTY || locationInParent == SuperMethodInvocation.NAME_PROPERTY) {
				return unit instanceof ICompilationUnit ? node.getParent() : null; // don't start on invocations in binary
			}
			return null;
		case ASTNode.EXPRESSION_STATEMENT:
			node = ((ExpressionStatement) node).getExpression();
	}
	switch (node.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
			return node;
		case ASTNode.METHOD_INVOCATION:
		case ASTNode.SUPER_METHOD_INVOCATION:
		case ASTNode.CONSTRUCTOR_INVOCATION:
			return unit instanceof ICompilationUnit ? node : null; // don't start on invocations in binary
	}
	return null;
}
 
Example #28
Source File: CreateMutableCloneResolution.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param workingUnit
 * @param original
 * @return
 */
private MethodInvocation invokeClone(CompilationUnit workingUnit, SimpleName original) {
    MethodInvocation cloneInvoke = workingUnit.getAST().newMethodInvocation();
    Expression cloneField = (SimpleName) ASTNode.copySubtree(cloneInvoke.getAST(), original);
    SimpleName cloneName = workingUnit.getAST().newSimpleName("clone");
    cloneInvoke.setExpression(cloneField);
    cloneInvoke.setName(cloneName);
    return cloneInvoke;
}
 
Example #29
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 #30
Source File: ImportReferencesCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(MethodInvocation node) {
	evalQualifyingExpression(node.getExpression(), node.getName());
	doVisitChildren(node.typeArguments());
	doVisitChildren(node.arguments());
	return false;
}