Java Code Examples for org.eclipse.jdt.core.dom.MethodInvocation#setName()
The following examples show how to use
org.eclipse.jdt.core.dom.MethodInvocation#setName() .
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: UseEqualsResolution.java From spotbugs with GNU Lesser General Public License v2.1 | 6 votes |
protected Expression createEqualsExpression(ASTRewrite rewrite, InfixExpression stringEqualityCheck) { Assert.isNotNull(rewrite); Assert.isNotNull(stringEqualityCheck); final AST ast = rewrite.getAST(); MethodInvocation equalsInvocation = ast.newMethodInvocation(); Expression leftOperand = createLeftOperand(rewrite, stringEqualityCheck.getLeftOperand()); Expression rightOperand = createRightOperand(rewrite, stringEqualityCheck.getRightOperand()); equalsInvocation.setName(ast.newSimpleName(EQUALS_METHOD_NAME)); equalsInvocation.setExpression(leftOperand); ListRewrite argumentsRewrite = rewrite.getListRewrite(equalsInvocation, MethodInvocation.ARGUMENTS_PROPERTY); argumentsRewrite.insertLast(rightOperand, null); return equalsInvocation; }
Example 2
Source File: GenerateHashCodeEqualsOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private Statement createAddQualifiedHashCode(IVariableBinding binding) { MethodInvocation invoc= fAst.newMethodInvocation(); invoc.setExpression(getThisAccessForHashCode(binding.getName())); invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE)); InfixExpression expr= fAst.newInfixExpression(); expr.setOperator(Operator.EQUALS); expr.setLeftOperand(getThisAccessForHashCode(binding.getName())); expr.setRightOperand(fAst.newNullLiteral()); ConditionalExpression cexpr= fAst.newConditionalExpression(); cexpr.setThenExpression(fAst.newNumberLiteral("0")); //$NON-NLS-1$ cexpr.setElseExpression(invoc); cexpr.setExpression(parenthesize(expr)); return prepareAssignment(parenthesize(cexpr)); }
Example 3
Source File: GenerateForLoopAssistProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Generates the initializer for an iterator based <code>for</code> loop, which declares and * initializes the variable to loop over. * * @param rewrite the instance of {@link ASTRewrite} * @param loopVariableName the proposed name of the loop variable * @return a {@link VariableDeclarationExpression} to use as initializer */ private VariableDeclarationExpression getIteratorBasedForInitializer(ASTRewrite rewrite, SimpleName loopVariableName) { AST ast= rewrite.getAST(); IMethodBinding iteratorMethodBinding= Bindings.findMethodInHierarchy(fExpressionType, "iterator", new ITypeBinding[] {}); //$NON-NLS-1$ // initializing fragment VariableDeclarationFragment varDeclarationFragment= ast.newVariableDeclarationFragment(); varDeclarationFragment.setName(loopVariableName); MethodInvocation iteratorExpression= ast.newMethodInvocation(); iteratorExpression.setName(ast.newSimpleName(iteratorMethodBinding.getName())); iteratorExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression)); varDeclarationFragment.setInitializer(iteratorExpression); // declaration VariableDeclarationExpression varDeclarationExpression= ast.newVariableDeclarationExpression(varDeclarationFragment); varDeclarationExpression.setType(getImportRewrite().addImport(iteratorMethodBinding.getReturnType(), ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite()))); return varDeclarationExpression; }
Example 4
Source File: GenerateHashCodeEqualsOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private Statement createOuterComparison() { MethodInvocation outer1= fAst.newMethodInvocation(); outer1.setName(fAst.newSimpleName(METHODNAME_OUTER_TYPE)); MethodInvocation outer2= fAst.newMethodInvocation(); outer2.setName(fAst.newSimpleName(METHODNAME_OUTER_TYPE)); outer2.setExpression(fAst.newSimpleName(VARIABLE_NAME_EQUALS_CASTED)); MethodInvocation outerEql= fAst.newMethodInvocation(); outerEql.setName(fAst.newSimpleName(METHODNAME_EQUALS)); outerEql.setExpression(outer1); outerEql.arguments().add(outer2); PrefixExpression not= fAst.newPrefixExpression(); not.setOperand(outerEql); not.setOperator(PrefixExpression.Operator.NOT); IfStatement notEqNull= fAst.newIfStatement(); notEqNull.setExpression(not); notEqNull.setThenStatement(getThenStatement(getReturnFalse())); return notEqNull; }
Example 5
Source File: StaticMethodInvocationFragment.java From SparkBuilderGenerator with MIT License | 5 votes |
public MethodInvocation createStaticMethodInvocation(AST ast, String className, String methodName) { SimpleName typeName = ast.newSimpleName(className); MethodInvocation methodInvocation = ast.newMethodInvocation(); methodInvocation.setName(ast.newSimpleName(methodName)); methodInvocation.setExpression(typeName); return methodInvocation; }
Example 6
Source File: AccessAnalyzer.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private MethodInvocation createInvocation(AST ast, Expression operand, String operator) { Expression receiver = getReceiver(operand); MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(fSetter)); if (receiver != null) { invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver)); } InfixExpression argument = ast.newInfixExpression(); invocation.arguments().add(argument); if ("++".equals(operator)) { //$NON-NLS-1$ argument.setOperator(InfixExpression.Operator.PLUS); } else if ("--".equals(operator)) { //$NON-NLS-1$ argument.setOperator(InfixExpression.Operator.MINUS); } else { Assert.isTrue(false, "Should not happen"); //$NON-NLS-1$ } MethodInvocation getter = ast.newMethodInvocation(); getter.setName(ast.newSimpleName(fGetter)); if (receiver != null) { getter.setExpression((Expression) fRewriter.createCopyTarget(receiver)); } argument.setLeftOperand(getter); argument.setRightOperand(ast.newNumberLiteral("1")); //$NON-NLS-1$ fReferencingGetter = true; fReferencingSetter = true; return invocation; }
Example 7
Source File: BaseTranslator.java From junion with BSD 3-Clause "New" or "Revised" License | 5 votes |
public MethodInvocation methodTmpStackVar(ASTNode StackExpression, Expression AddressIdentifier, Entry TypeBind) { MethodInvocation m = ast.newMethodInvocation(); m.setExpression(name(MEM0)); m.setName(name("tmpstack")); List args = m.arguments(); args.add((Expression)copySubtreeIfHasParent(StackExpression)); args.add((Expression)copySubtreeIfHasParent(AddressIdentifier)); args.add(returnInt(0)); m.setProperty(TYPEBIND_PROP, TypeBind); m.setProperty(TYPEBIND_METHOD_TMP, new MethodTmp(TypebindMethodTmp.StackVar, TypeBind)); return m; }
Example 8
Source File: AbstractToStringGenerator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Creates an invocation of a method that takes zero or one argument * * @param expression the receiver expression * @param methodName the method name * @param argument the argument, can be <code>null</code> if the method does not take any arguments * @return MethodInvocation in following form: <code>expression.methodName(argument)</code> */ protected MethodInvocation createMethodInvocation(Expression expression, String methodName, Expression argument) { MethodInvocation invocation= fAst.newMethodInvocation(); invocation.setExpression(expression); invocation.setName(fAst.newSimpleName(methodName)); if (argument != null) invocation.arguments().add(argument); return invocation; }
Example 9
Source File: GenerateForLoopAssistProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Helper to generate an iterator based <code>for</code> loop to iterate over an * {@link Iterable}. * * @param ast the {@link AST} instance to rewrite the loop to * @return the complete {@link ASTRewrite} object */ private ASTRewrite generateIteratorBasedForRewrite(AST ast) { ASTRewrite rewrite= ASTRewrite.create(ast); ForStatement loopStatement= ast.newForStatement(); ITypeBinding loopOverType= extractElementType(ast); SimpleName loopVariableName= resolveLinkedVariableNameWithProposals(rewrite, "iterator", null, true); //$NON-NLS-1$ loopStatement.initializers().add(getIteratorBasedForInitializer(rewrite, loopVariableName)); MethodInvocation loopExpression= ast.newMethodInvocation(); loopExpression.setName(ast.newSimpleName("hasNext")); //$NON-NLS-1$ SimpleName expressionName= ast.newSimpleName(loopVariableName.getIdentifier()); addLinkedPosition(rewrite.track(expressionName), LinkedPositionGroup.NO_STOP, expressionName.getIdentifier()); loopExpression.setExpression(expressionName); loopStatement.setExpression(loopExpression); Block forLoopBody= ast.newBlock(); Assignment assignResolvedVariable= getIteratorBasedForBodyAssignment(rewrite, loopOverType, loopVariableName); forLoopBody.statements().add(ast.newExpressionStatement(assignResolvedVariable)); forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite)); loopStatement.setBody(forLoopBody); rewrite.replace(fCurrentNode, loopStatement, null); return rewrite; }
Example 10
Source File: ParameterObjectFactory.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public Expression createFieldReadAccess(ParameterInfo pi, String paramName, AST ast, IJavaProject project, boolean useSuper, Expression qualifier) { Expression completeQualifier= generateQualifier(paramName, ast, useSuper, qualifier); if (fCreateGetter) { MethodInvocation mi= ast.newMethodInvocation(); mi.setName(ast.newSimpleName(getGetterName(pi, ast, project))); mi.setExpression(completeQualifier); return mi; } return createFieldAccess(pi, ast, completeQualifier); }
Example 11
Source File: GenerateForLoopAssistProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Creates an {@link Assignment} as first expression appearing in an index based * <code>for</code> loop's body. This Assignment declares a local variable and initializes it * using the {@link List}'s current element identified by the loop index. * * @param rewrite the current {@link ASTRewrite} instance * @param loopVariableName the name of the index variable in String representation * @return a completed {@link Assignment} containing the mentioned declaration and * initialization */ private Expression getIndexBasedForBodyAssignment(ASTRewrite rewrite, SimpleName loopVariableName) { AST ast= rewrite.getAST(); ITypeBinding loopOverType= extractElementType(ast); Assignment assignResolvedVariable= ast.newAssignment(); // left hand side SimpleName resolvedVariableName= resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false); VariableDeclarationFragment resolvedVariableDeclarationFragment= ast.newVariableDeclarationFragment(); resolvedVariableDeclarationFragment.setName(resolvedVariableName); VariableDeclarationExpression resolvedVariableDeclaration= ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment); resolvedVariableDeclaration.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite()))); assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration); // right hand side MethodInvocation invokeGetExpression= ast.newMethodInvocation(); invokeGetExpression.setName(ast.newSimpleName("get")); //$NON-NLS-1$ SimpleName indexVariableName= ast.newSimpleName(loopVariableName.getIdentifier()); addLinkedPosition(rewrite.track(indexVariableName), LinkedPositionGroup.NO_STOP, indexVariableName.getIdentifier()); invokeGetExpression.arguments().add(indexVariableName); invokeGetExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression)); assignResolvedVariable.setRightHandSide(invokeGetExpression); assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN); return assignResolvedVariable; }
Example 12
Source File: MoveInstanceMethodProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override protected ASTNode createBody(BodyDeclaration bd) throws JavaModelException { MethodDeclaration methodDeclaration= (MethodDeclaration) bd; final MethodInvocation invocation= getAst().newMethodInvocation(); invocation.setName(getAst().newSimpleName(getNewElementName())); invocation.setExpression(createSimpleTargetAccessExpression(methodDeclaration)); createArgumentList(methodDeclaration, invocation.arguments(), new VisibilityAdjustingArgumentFactory(getAst(), fRewrites, fAdjustments)); final Block block= getAst().newBlock(); block.statements().add(createMethodInvocation(methodDeclaration, invocation)); if (!fSourceRewrite.getCu().equals(fTargetType.getCompilationUnit())) fSourceRewrite.getImportRemover().registerRemovedNode(methodDeclaration.getBody()); return block; }
Example 13
Source File: PullUpRefactoringProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@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 14
Source File: BaseTranslator.java From junion with BSD 3-Clause "New" or "Revised" License | 5 votes |
public MethodInvocation methodTmpRef(ASTNode StackExpression, Expression AddressIdentifier, Entry TypeBind, StructCache.FieldEntry f, String varData, String var) { MethodInvocation m = ast.newMethodInvocation(); m.setExpression(name(MEM0)); m.setName(name("tmpref")); List args = m.arguments(); args.add((Expression)copySubtreeIfHasParent(StackExpression)); args.add((Expression)copySubtreeIfHasParent(AddressIdentifier)); args.add(returnInt(0)); m.setProperty(TYPEBIND_PROP, TypeBind); m.setProperty(TYPEBIND_METHOD_TMP, new MethodTmp(TypebindMethodTmp.Ref, TypeBind, f, varData, var)); return m; }
Example 15
Source File: AbstractToStringGenerator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * * @param templateElement the template element, see constants in {@link ToStringTemplateParser} * @param member the member * @return <code>String</code> or <code>Expression</code> switching */ protected Object processElement(String templateElement, Object member) { Object result= templateElement; if (templateElement == ToStringTemplateParser.OBJECT_NAME_VARIABLE) { result= fContext.getTypeBinding().getName(); } if (templateElement == ToStringTemplateParser.OBJECT_GET_NAME_VARIABLE) { //this.getClass().getName() MethodInvocation getClassInvocation= fAst.newMethodInvocation(); if (fContext.isKeywordThis()) getClassInvocation.setExpression(fAst.newThisExpression()); getClassInvocation.setName(fAst.newSimpleName("getClass")); //$NON-NLS-1$ MethodInvocation getNameInvocation= fAst.newMethodInvocation(); getNameInvocation.setExpression(getClassInvocation); getNameInvocation.setName(fAst.newSimpleName("getName")); //$NON-NLS-1$ result= getNameInvocation; } if (templateElement == ToStringTemplateParser.OBJECT_SUPER_TOSTRING_VARIABLE) { //super.toString() SuperMethodInvocation superToStringInvocation= fAst.newSuperMethodInvocation(); superToStringInvocation.setName(fAst.newSimpleName(METHODNAME_TO_STRING)); result= superToStringInvocation; } if (templateElement == ToStringTemplateParser.OBJECT_HASHCODE_VARIABLE) { //this.hashCode() MethodInvocation hashCodeInvocation= fAst.newMethodInvocation(); if (fContext.isKeywordThis()) hashCodeInvocation.setExpression(fAst.newThisExpression()); hashCodeInvocation.setName(fAst.newSimpleName("hashCode")); //$NON-NLS-1$ result= hashCodeInvocation; } if (templateElement == ToStringTemplateParser.OBJECT_SYSTEM_HASHCODE_VARIABLE) { //system.identityHashCode(this) result= createMethodInvocation(addImport("java.lang.System"), "identityHashCode", fAst.newThisExpression()); //$NON-NLS-1$ //$NON-NLS-2$ } if (templateElement == ToStringTemplateParser.MEMBER_NAME_VARIABLE || templateElement == ToStringTemplateParser.MEMBER_NAME_PARENTHESIS_VARIABLE) { result= getMemberName(member, templateElement); } if (templateElement == ToStringTemplateParser.MEMBER_VALUE_VARIABLE) { result= createMemberAccessExpression(member, false, fContext.isSkipNulls()); } if (result instanceof StringLiteral) return ((StringLiteral)result).getLiteralValue(); else return result; }
Example 16
Source File: IntroduceIndirectionRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private RefactoringStatus updateMethodInvocation(MethodInvocation originalInvocation, IMember enclosing, CompilationUnitRewrite unitRewriter) throws JavaModelException { RefactoringStatus status= new RefactoringStatus(); // If the method invocation utilizes type arguments, skip this // call as the new target method may have additional parameters if (originalInvocation.typeArguments().size() > 0) return createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_type_arguments); MethodInvocation newInvocation= unitRewriter.getAST().newMethodInvocation(); List<Expression> newInvocationArgs= newInvocation.arguments(); List<Expression> originalInvocationArgs= originalInvocation.arguments(); // static call => always use a qualifier String qualifier= unitRewriter.getImportRewrite().addImport(fIntermediaryTypeBinding); newInvocation.setExpression(ASTNodeFactory.newName(unitRewriter.getAST(), qualifier)); newInvocation.setName(unitRewriter.getAST().newSimpleName(getIntermediaryMethodName())); final Expression expression= originalInvocation.getExpression(); if (!isStaticTarget()) { // Add the expression as the first parameter if (expression == null) { // There is no expression for this call. Use a (possibly qualified) "this" expression. ThisExpression expr= unitRewriter.getAST().newThisExpression(); RefactoringStatus qualifierStatus= qualifyThisExpression(expr, originalInvocation, enclosing, unitRewriter); status.merge(qualifierStatus); if (qualifierStatus.hasEntries()) // warning means don't include this invocation return status; newInvocationArgs.add(expr); } else { Expression expressionAsParam= (Expression) unitRewriter.getASTRewrite().createMoveTarget(expression); newInvocationArgs.add(expressionAsParam); } } else { if (expression != null) { // Check if expression is the type name. If not, there may // be side effects (e.g. inside methods) -> don't update if (! (expression instanceof Name) || ASTNodes.getTypeBinding((Name) expression) == null) return createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_static_expression_access); } } for (int i= 0; i < originalInvocationArgs.size(); i++) { Expression originalInvocationArg= originalInvocationArgs.get(i); Expression movedArg= (Expression) unitRewriter.getASTRewrite().createMoveTarget(originalInvocationArg); newInvocationArgs.add(movedArg); } unitRewriter.getASTRewrite().replace(originalInvocation, newInvocation, unitRewriter.createGroupDescription(RefactoringCoreMessages.IntroduceIndirectionRefactoring_group_description_replace_call)); return status; }
Example 17
Source File: ExtractMethodRefactoring.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private ASTNode[] createCallNodes(SnippetFinder.Match duplicate, int modifiers) { List<ASTNode> result = new ArrayList<>(2); IVariableBinding[] locals = fAnalyzer.getCallerLocals(); for (int i = 0; i < locals.length; i++) { result.add(createDeclaration(locals[i], null)); } MethodInvocation invocation = fAST.newMethodInvocation(); invocation.setName(fAST.newSimpleName(fMethodName)); ASTNode typeNode = ASTResolving.findParentType(fAnalyzer.getEnclosingBodyDeclaration()); RefactoringStatus status = new RefactoringStatus(); while (fDestination != typeNode) { fAnalyzer.checkInput(status, fMethodName, typeNode); if (!status.isOK()) { SimpleName destinationTypeName = fAST.newSimpleName(ASTNodes.getEnclosingType(fDestination).getName()); if ((modifiers & Modifier.STATIC) == 0) { ThisExpression thisExpression = fAST.newThisExpression(); thisExpression.setQualifier(destinationTypeName); invocation.setExpression(thisExpression); } else { invocation.setExpression(destinationTypeName); } break; } typeNode = typeNode.getParent(); } List<Expression> arguments = invocation.arguments(); for (int i = 0; i < fParameterInfos.size(); i++) { ParameterInfo parameter = fParameterInfos.get(i); arguments.add(ASTNodeFactory.newName(fAST, getMappedName(duplicate, parameter))); } if (fLinkedProposalModel != null) { LinkedProposalPositionGroupCore nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true); nameGroup.addPosition(fRewriter.track(invocation.getName()), true); } ASTNode call; int returnKind = fAnalyzer.getReturnKind(); switch (returnKind) { case ExtractMethodAnalyzer.ACCESS_TO_LOCAL: IVariableBinding binding = fAnalyzer.getReturnLocal(); if (binding != null) { VariableDeclarationStatement decl = createDeclaration(getMappedBinding(duplicate, binding), invocation); call = decl; } else { Assignment assignment = fAST.newAssignment(); assignment.setLeftHandSide(ASTNodeFactory.newName(fAST, getMappedBinding(duplicate, fAnalyzer.getReturnValue()).getName())); assignment.setRightHandSide(invocation); call = assignment; } break; case ExtractMethodAnalyzer.RETURN_STATEMENT_VALUE: ReturnStatement rs = fAST.newReturnStatement(); rs.setExpression(invocation); call = rs; break; default: call = invocation; } if (call instanceof Expression && !fAnalyzer.isExpressionSelected()) { call = fAST.newExpressionStatement((Expression) call); } result.add(call); // We have a void return statement. The code looks like // extracted(); // return; if (returnKind == ExtractMethodAnalyzer.RETURN_STATEMENT_VOID && !fAnalyzer.isLastStatementSelected()) { result.add(fAST.newReturnStatement()); } return result.toArray(new ASTNode[result.size()]); }
Example 18
Source File: AccessAnalyzer.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
@Override public boolean visit(Assignment node) { Expression leftHandSide = node.getLeftHandSide(); if (!considerBinding(resolveBinding(leftHandSide), leftHandSide)) { return true; } checkParent(node); Expression rightHandSide = node.getRightHandSide(); if (!fIsFieldFinal) { // Write access. AST ast = node.getAST(); MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(fSetter)); fReferencingSetter = true; Expression receiver = getReceiver(leftHandSide); if (receiver != null) { invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver)); } List<Expression> arguments = invocation.arguments(); if (node.getOperator() == Assignment.Operator.ASSIGN) { arguments.add((Expression) fRewriter.createCopyTarget(rightHandSide)); } else { // This is the compound assignment case: field+= 10; InfixExpression exp = ast.newInfixExpression(); exp.setOperator(ASTNodes.convertToInfixOperator(node.getOperator())); MethodInvocation getter = ast.newMethodInvocation(); getter.setName(ast.newSimpleName(fGetter)); fReferencingGetter = true; if (receiver != null) { getter.setExpression((Expression) fRewriter.createCopyTarget(receiver)); } exp.setLeftOperand(getter); Expression rhs = (Expression) fRewriter.createCopyTarget(rightHandSide); if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, exp, leftHandSide.resolveTypeBinding())) { ParenthesizedExpression p = ast.newParenthesizedExpression(); p.setExpression(rhs); rhs = p; } exp.setRightOperand(rhs); arguments.add(exp); } fRewriter.replace(node, invocation, createGroupDescription(WRITE_ACCESS)); } rightHandSide.accept(this); return false; }
Example 19
Source File: ExtractMethodRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private ASTNode[] createCallNodes(SnippetFinder.Match duplicate, int modifiers) { List<ASTNode> result= new ArrayList<ASTNode>(2); IVariableBinding[] locals= fAnalyzer.getCallerLocals(); for (int i= 0; i < locals.length; i++) { result.add(createDeclaration(locals[i], null)); } MethodInvocation invocation= fAST.newMethodInvocation(); invocation.setName(fAST.newSimpleName(fMethodName)); ASTNode typeNode= ASTResolving.findParentType(fAnalyzer.getEnclosingBodyDeclaration()); RefactoringStatus status= new RefactoringStatus(); while (fDestination != typeNode) { fAnalyzer.checkInput(status, fMethodName, typeNode); if (!status.isOK()) { SimpleName destinationTypeName= fAST.newSimpleName(ASTNodes.getEnclosingType(fDestination).getName()); if ((modifiers & Modifier.STATIC) == 0) { ThisExpression thisExpression= fAST.newThisExpression(); thisExpression.setQualifier(destinationTypeName); invocation.setExpression(thisExpression); } else { invocation.setExpression(destinationTypeName); } break; } typeNode= typeNode.getParent(); } List<Expression> arguments= invocation.arguments(); for (int i= 0; i < fParameterInfos.size(); i++) { ParameterInfo parameter= fParameterInfos.get(i); arguments.add(ASTNodeFactory.newName(fAST, getMappedName(duplicate, parameter))); } if (fLinkedProposalModel != null) { LinkedProposalPositionGroup nameGroup= fLinkedProposalModel.getPositionGroup(KEY_NAME, true); nameGroup.addPosition(fRewriter.track(invocation.getName()), false); } ASTNode call; int returnKind= fAnalyzer.getReturnKind(); switch (returnKind) { case ExtractMethodAnalyzer.ACCESS_TO_LOCAL: IVariableBinding binding= fAnalyzer.getReturnLocal(); if (binding != null) { VariableDeclarationStatement decl= createDeclaration(getMappedBinding(duplicate, binding), invocation); call= decl; } else { Assignment assignment= fAST.newAssignment(); assignment.setLeftHandSide(ASTNodeFactory.newName(fAST, getMappedBinding(duplicate, fAnalyzer.getReturnValue()).getName())); assignment.setRightHandSide(invocation); call= assignment; } break; case ExtractMethodAnalyzer.RETURN_STATEMENT_VALUE: ReturnStatement rs= fAST.newReturnStatement(); rs.setExpression(invocation); call= rs; break; default: call= invocation; } if (call instanceof Expression && !fAnalyzer.isExpressionSelected()) { call= fAST.newExpressionStatement((Expression)call); } result.add(call); // We have a void return statement. The code looks like // extracted(); // return; if (returnKind == ExtractMethodAnalyzer.RETURN_STATEMENT_VOID && !fAnalyzer.isLastStatementSelected()) { result.add(fAST.newReturnStatement()); } return result.toArray(new ASTNode[result.size()]); }
Example 20
Source File: CustomBuilderGenerator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private MethodInvocation createAppendMethodForMember(Object member) { ITypeBinding memberType= getMemberType(member); String memberTypeName= memberType.getQualifiedName(); Expression memberAccessExpression= null; AppendMethodInformation ami= appendMethodSpecificTypes.get(memberTypeName); if (ami == null && memberType.isPrimitive()) { memberTypeName= wrapperTypes[primitiveTypes.indexOf(memberTypeName)]; memberType= fAst.resolveWellKnownType(memberTypeName); ami= appendMethodSpecificTypes.get(memberTypeName); if (!getContext().is50orHigher()) { ClassInstanceCreation classInstance= fAst.newClassInstanceCreation(); classInstance.setType(fAst.newSimpleType(addImport(memberTypeName))); classInstance.arguments().add(createMemberAccessExpression(member, true, true)); memberAccessExpression= classInstance; } } while (ami == null) { memberType= memberType.getSuperclass(); if (memberType != null) memberTypeName= memberType.getQualifiedName(); else memberTypeName= "java.lang.Object"; //$NON-NLS-1$ ami= appendMethodSpecificTypes.get(memberTypeName); } if (memberAccessExpression == null) { memberAccessExpression= createMemberAccessExpression(member, false, getContext().isSkipNulls()); } MethodInvocation appendInvocation= fAst.newMethodInvocation(); appendInvocation.setName(fAst.newSimpleName(getContext().getCustomBuilderAppendMethod())); if (ami.methodType == 1 || ami.methodType == 2) { appendInvocation.arguments().add(memberAccessExpression); } if (ami.methodType == 2 || ami.methodType == 3) { StringLiteral literal= fAst.newStringLiteral(); literal.setLiteralValue(getMemberName(member, ToStringTemplateParser.MEMBER_NAME_PARENTHESIS_VARIABLE)); appendInvocation.arguments().add(literal); } if (ami.methodType == 3) { appendInvocation.arguments().add(memberAccessExpression); } canChainLastAppendCall= ami.returnsBuilder; return appendInvocation; }