Java Code Examples for org.eclipse.jdt.core.dom.ParenthesizedExpression#setExpression()
The following examples show how to use
org.eclipse.jdt.core.dom.ParenthesizedExpression#setExpression() .
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: CreateAndOddnessCheckResolution.java From spotbugs with GNU Lesser General Public License v2.1 | 6 votes |
/** * Creates the new <CODE>InfixExpression</CODE> <CODE>(x & 1) == 1</CODE>. */ @Override protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) { Assert.isNotNull(rewrite); Assert.isNotNull(numberExpression); final AST ast = rewrite.getAST(); InfixExpression andOddnessCheck = ast.newInfixExpression(); ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression(); InfixExpression andExpression = ast.newInfixExpression(); andExpression.setLeftOperand((Expression) rewrite.createMoveTarget(numberExpression)); andExpression.setOperator(AND); andExpression.setRightOperand(ast.newNumberLiteral("1")); parenthesizedExpression.setExpression(andExpression); andOddnessCheck.setLeftOperand(parenthesizedExpression); andOddnessCheck.setOperator(EQUALS); andOddnessCheck.setRightOperand(ast.newNumberLiteral("1")); return andOddnessCheck; }
Example 2
Source File: ExpressionsFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * {@inheritDoc} */ @Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException { TextEditGroup group= createTextEditGroup(FixMessages.ExpressionsFix_addParanoiacParentheses_description, cuRewrite); ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); for (int i= 0; i < fExpressions.length; i++) { // add parenthesis around expression Expression expression= fExpressions[i]; ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression(); parenthesizedExpression.setExpression((Expression) rewrite.createCopyTarget(expression)); rewrite.replace(expression, parenthesizedExpression, group); } }
Example 3
Source File: UnusedCodeFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static void replaceCast(CastExpression castExpression, Expression replacement, ASTRewrite rewrite, TextEditGroup group) { boolean castEnclosedInNecessaryParentheses= castExpression.getParent() instanceof ParenthesizedExpression && NecessaryParenthesesChecker.needsParentheses(castExpression, castExpression.getParent().getParent(), castExpression.getParent().getLocationInParent()); ASTNode toReplace= castEnclosedInNecessaryParentheses ? castExpression.getParent() : castExpression; ASTNode move; if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) { if (replacement.getParent() instanceof ParenthesizedExpression) { move= rewrite.createMoveTarget(replacement.getParent()); } else if (castEnclosedInNecessaryParentheses) { toReplace= castExpression; move= rewrite.createMoveTarget(replacement); } else { ParenthesizedExpression parentheses= replacement.getAST().newParenthesizedExpression(); parentheses.setExpression((Expression) rewrite.createMoveTarget(replacement)); move= parentheses; } } else { move= rewrite.createMoveTarget(replacement); } rewrite.replace(toReplace, move, group); }
Example 4
Source File: BaseTranslator.java From junion with BSD 3-Clause "New" or "Revised" License | 5 votes |
public ParenthesizedExpression wrap(Expression e) { ParenthesizedExpression p = ast.newParenthesizedExpression(); p.setExpression(e); Object o = e.getProperty(TYPEBIND_PROP); if(o != null) p.setProperty(TYPEBIND_PROP, o); return p; }
Example 5
Source File: UnresolvedElementsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static boolean useExistingParentCastProposal(ICompilationUnit cu, CastExpression expression, Expression accessExpression, SimpleName accessSelector, ITypeBinding[] paramTypes, Collection<ICommandAccess> proposals) { ITypeBinding castType= expression.getType().resolveBinding(); if (castType == null) { return false; } if (paramTypes != null) { if (Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes) == null) { return false; } } else if (Bindings.findFieldInHierarchy(castType, accessSelector.getIdentifier()) == null) { return false; } ITypeBinding bindingToCast= accessExpression.resolveTypeBinding(); if (bindingToCast != null && !bindingToCast.isCastCompatible(castType)) { return false; } IMethodBinding res= Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes); if (res != null) { AST ast= expression.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); CastExpression newCast= ast.newCastExpression(); newCast.setType((Type) ASTNode.copySubtree(ast, expression.getType())); newCast.setExpression((Expression) rewrite.createCopyTarget(accessExpression)); ParenthesizedExpression parents= ast.newParenthesizedExpression(); parents.setExpression(newCast); ASTNode node= rewrite.createCopyTarget(expression.getExpression()); rewrite.replace(expression, node, null); rewrite.replace(accessExpression, parents, null); String label= CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_PARENTHESES_AROUND_CAST, image); proposals.add(proposal); return true; } return false; }
Example 6
Source File: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static boolean getPullNegationUpProposals(IInvocationContext context, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) { if (coveredNodes.size() != 1) { return false; } // ASTNode fullyCoveredNode= coveredNodes.get(0); Expression expression= getBooleanExpression(fullyCoveredNode); if (expression == null || (!(expression instanceof InfixExpression) && !(expression instanceof ConditionalExpression))) { return false; } // we could produce quick assist if (resultingCollections == null) { return true; } // AST ast= expression.getAST(); final ASTRewrite rewrite= ASTRewrite.create(ast); // prepared inverted expression Expression inversedExpression= getInversedExpression(rewrite, expression); // prepare ParenthesizedExpression ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression(); parenthesizedExpression.setExpression(inversedExpression); // prepare NOT prefix expression PrefixExpression prefixExpression= ast.newPrefixExpression(); prefixExpression.setOperator(PrefixExpression.Operator.NOT); prefixExpression.setOperand(parenthesizedExpression); // replace old expression rewrite.replace(expression, prefixExpression, null); // add correction proposal String label= CorrectionMessages.AdvancedQuickAssistProcessor_pullNegationUp; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PULL_NEGATION_UP, image); resultingCollections.add(proposal); return true; }
Example 7
Source File: UseEqualsResolution.java From spotbugs with GNU Lesser General Public License v2.1 | 5 votes |
private Expression createLeftOperand(ASTRewrite rewrite, Expression leftOperand) { Expression leftExp = (Expression) rewrite.createMoveTarget(leftOperand); if (leftOperand instanceof Name || leftOperand instanceof ParenthesizedExpression) { return leftExp; } final AST ast = rewrite.getAST(); ParenthesizedExpression parExp = ast.newParenthesizedExpression(); parExp.setExpression(leftExp); return parExp; }
Example 8
Source File: InlineTempRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private Expression getInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException { Expression copy= getModifiedInitializerSource(rewrite, reference); if (NecessaryParenthesesChecker.needsParentheses(copy, reference.getParent(), reference.getLocationInParent())) { ParenthesizedExpression parentExpr= rewrite.getAST().newParenthesizedExpression(); parentExpr.setExpression(copy); return parentExpr; } return copy; }
Example 9
Source File: StringConcatenationGenerator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void addElement(Object element, SumExpressionBuilder builder) { if (element instanceof String) { builder.addString((String)element); } if (element instanceof Expression) { Expression expr= (Expression)element; if (expr instanceof ConditionalExpression) { ParenthesizedExpression expr2= fAst.newParenthesizedExpression(); expr2.setExpression(expr); expr= expr2; } builder.addExpression(expr); } }
Example 10
Source File: StringConcatenationGenerator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override protected void addMemberCheckNull(Object member, boolean addSeparator) { ConditionalExpression cExpression= fAst.newConditionalExpression(); // member != null ? InfixExpression infExpression= fAst.newInfixExpression(); infExpression.setLeftOperand(createMemberAccessExpression(member, true, true)); infExpression.setRightOperand(fAst.newNullLiteral()); infExpression.setOperator(Operator.NOT_EQUALS); cExpression.setExpression(infExpression); SumExpressionBuilder builder= new SumExpressionBuilder(null); String[] arrayString= getContext().getTemplateParser().getBody(); for (int i= 0; i < arrayString.length; i++) { addElement(processElement(arrayString[i], member), builder); } if (addSeparator) addElement(getContext().getTemplateParser().getSeparator(), builder); cExpression.setThenExpression(builder.getExpression()); StringLiteral literal= fAst.newStringLiteral(); literal.setLiteralValue(getContext().isSkipNulls() ? "" : "null"); //$NON-NLS-1$ //$NON-NLS-2$ cExpression.setElseExpression(literal); ParenthesizedExpression pExpression= fAst.newParenthesizedExpression(); pExpression.setExpression(cExpression); toStringExpressionBuilder.addExpression(pExpression); }
Example 11
Source File: GetterSetterUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Checks if the assignment needs a downcast and inserts it if necessary * * @param expression the right hand-side * @param expressionType the type of the right hand-side. Can be null * @param ast the AST * @param variableType the Type of the variable the expression will be assigned to * @param is50OrHigher if <code>true</code> java 5.0 code will be assumed * @return the casted expression if necessary */ private static Expression createNarrowCastIfNessecary(Expression expression, ITypeBinding expressionType, AST ast, ITypeBinding variableType, boolean is50OrHigher) { PrimitiveType castTo= null; if (variableType.isEqualTo(expressionType)) return expression; //no cast for same type if (is50OrHigher) { if (ast.resolveWellKnownType("java.lang.Character").isEqualTo(variableType)) //$NON-NLS-1$ castTo= ast.newPrimitiveType(PrimitiveType.CHAR); if (ast.resolveWellKnownType("java.lang.Byte").isEqualTo(variableType)) //$NON-NLS-1$ castTo= ast.newPrimitiveType(PrimitiveType.BYTE); if (ast.resolveWellKnownType("java.lang.Short").isEqualTo(variableType)) //$NON-NLS-1$ castTo= ast.newPrimitiveType(PrimitiveType.SHORT); } if (ast.resolveWellKnownType("char").isEqualTo(variableType)) //$NON-NLS-1$ castTo= ast.newPrimitiveType(PrimitiveType.CHAR); if (ast.resolveWellKnownType("byte").isEqualTo(variableType)) //$NON-NLS-1$ castTo= ast.newPrimitiveType(PrimitiveType.BYTE); if (ast.resolveWellKnownType("short").isEqualTo(variableType)) //$NON-NLS-1$ castTo= ast.newPrimitiveType(PrimitiveType.SHORT); if (castTo != null) { CastExpression cast= ast.newCastExpression(); if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) { ParenthesizedExpression parenthesized= ast.newParenthesizedExpression(); parenthesized.setExpression(expression); cast.setExpression(parenthesized); } else cast.setExpression(expression); cast.setType(castTo); return cast; } return expression; }
Example 12
Source File: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static boolean getAddParenthesesForExpressionProposals(IInvocationContext context, ASTNode coveringNode, Collection<ICommandAccess> resultingCollections) { ASTNode node; if (context.getSelectionLength() == 0) { node= coveringNode; while (node != null && !(node instanceof CastExpression) && !(node instanceof InfixExpression) && !(node instanceof InstanceofExpression) && !(node instanceof ConditionalExpression)) { node= node.getParent(); } } else { node= context.getCoveredNode(); } String label= null; if (node instanceof CastExpression) { label= CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description; } else if (node instanceof InstanceofExpression) { label= CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_instanceof_description; } else if (node instanceof InfixExpression) { InfixExpression infixExpression= (InfixExpression)node; label= Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_description, infixExpression.getOperator().toString()); } else if (node instanceof ConditionalExpression) { label= CorrectionMessages.AdvancedQuickAssistProcessor_putConditionalExpressionInParentheses; } else { return false; } if (node.getParent() instanceof ParenthesizedExpression) return false; if (resultingCollections == null) return true; AST ast= node.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression(); parenthesizedExpression.setExpression((Expression)rewrite.createCopyTarget(node)); rewrite.replace(node, parenthesizedExpression, null); Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_PARENTHESES_FOR_EXPRESSION, image); resultingCollections.add(proposal); return true; }
Example 13
Source File: LocalCorrectionsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static void addRemoveIncludingConditionProposal(IInvocationContext context, ASTNode toRemove, ASTNode replacement, Collection<ICommandAccess> proposals) { Image image= JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE); String label= CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description; AST ast= toRemove.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_UNREACHABLE_CODE_INCLUDING_CONDITION, image); if (replacement == null || replacement instanceof EmptyStatement || replacement instanceof Block && ((Block)replacement).statements().size() == 0) { if (ASTNodes.isControlStatementBody(toRemove.getLocationInParent())) { rewrite.replace(toRemove, toRemove.getAST().newBlock(), null); } else { rewrite.remove(toRemove, null); } } else if (toRemove instanceof Expression && replacement instanceof Expression) { Expression moved= (Expression) rewrite.createMoveTarget(replacement); Expression toRemoveExpression= (Expression) toRemove; Expression replacementExpression= (Expression) replacement; ITypeBinding explicitCast= ASTNodes.getExplicitCast(replacementExpression, toRemoveExpression); if (explicitCast != null) { CastExpression cast= ast.newCastExpression(); if (NecessaryParenthesesChecker.needsParentheses(replacementExpression, cast, CastExpression.EXPRESSION_PROPERTY)) { ParenthesizedExpression parenthesized= ast.newParenthesizedExpression(); parenthesized.setExpression(moved); moved= parenthesized; } cast.setExpression(moved); ImportRewrite imports= proposal.createImportRewrite(context.getASTRoot()); ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(toRemove, imports); cast.setType(imports.addImport(explicitCast, ast, importRewriteContext)); moved= cast; } rewrite.replace(toRemove, moved, null); } else { ASTNode parent= toRemove.getParent(); ASTNode moveTarget; if ((parent instanceof Block || parent instanceof SwitchStatement) && replacement instanceof Block) { ListRewrite listRewrite= rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY); List<Statement> list= ((Block)replacement).statements(); int lastIndex= list.size() - 1; moveTarget= listRewrite.createMoveTarget(list.get(0), list.get(lastIndex)); } else { moveTarget= rewrite.createMoveTarget(replacement); } rewrite.replace(toRemove, moveTarget, null); } proposals.add(proposal); }
Example 14
Source File: GetterSetterUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Converts an assignment, postfix expression or prefix expression into an assignable equivalent expression using the getter. * * @param node the assignment/prefix/postfix node * @param astRewrite the astRewrite to use * @param getterExpression the expression to insert for read accesses or <code>null</code> if such an expression does not exist * @param variableType the type of the variable that the result will be assigned to * @param is50OrHigher <code>true</code> if a 5.0 or higher environment can be used * @return an expression that can be assigned to the type variableType with node being replaced by a equivalent expression using the getter */ public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType, boolean is50OrHigher) { InfixExpression.Operator op= null; AST ast= astRewrite.getAST(); if (isNotInBlock(node)) return null; if (node.getNodeType() == ASTNode.ASSIGNMENT) { Assignment assignment= ((Assignment) node); Expression rightHandSide= assignment.getRightHandSide(); Expression copiedRightOp= (Expression) astRewrite.createCopyTarget(rightHandSide); if (assignment.getOperator() == Operator.ASSIGN) { ITypeBinding rightHandSideType= rightHandSide.resolveTypeBinding(); copiedRightOp= createNarrowCastIfNessecary(copiedRightOp, rightHandSideType, ast, variableType, is50OrHigher); return copiedRightOp; } if (getterExpression != null) { InfixExpression infix= ast.newInfixExpression(); infix.setLeftOperand(getterExpression); infix.setOperator(ASTNodes.convertToInfixOperator(assignment.getOperator())); ITypeBinding infixType= infix.resolveTypeBinding(); if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, infix, variableType)) { ParenthesizedExpression p= ast.newParenthesizedExpression(); p.setExpression(copiedRightOp); copiedRightOp= p; } infix.setRightOperand(copiedRightOp); return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher); } } else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) { PostfixExpression po= (PostfixExpression) node; if (po.getOperator() == PostfixExpression.Operator.INCREMENT) op= InfixExpression.Operator.PLUS; if (po.getOperator() == PostfixExpression.Operator.DECREMENT) op= InfixExpression.Operator.MINUS; } else if (node.getNodeType() == ASTNode.PREFIX_EXPRESSION) { PrefixExpression pe= (PrefixExpression) node; if (pe.getOperator() == PrefixExpression.Operator.INCREMENT) op= InfixExpression.Operator.PLUS; if (pe.getOperator() == PrefixExpression.Operator.DECREMENT) op= InfixExpression.Operator.MINUS; } if (op != null && getterExpression != null) { return createInfixInvocationFromPostPrefixExpression(op, getterExpression, ast, variableType, is50OrHigher); } return null; }
Example 15
Source File: GenerateHashCodeEqualsOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private Expression parenthesize(Expression expression) { ParenthesizedExpression pe= fAst.newParenthesizedExpression(); pe.setExpression(expression); return pe; }
Example 16
Source File: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static ParenthesizedExpression getParenthesizedExpression(AST ast, Expression expression) { ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression(); parenthesizedExpression.setExpression(expression); return parenthesizedExpression; }
Example 17
Source File: SourceProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private Expression createParenthesizedExpression(Expression newExpression, AST ast) { ParenthesizedExpression parenthesized= ast.newParenthesizedExpression(); parenthesized.setExpression(newExpression); return parenthesized; }
Example 18
Source File: LocalCorrectionsSubProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private static void addRemoveIncludingConditionProposal(IInvocationContext context, ASTNode toRemove, ASTNode replacement, Collection<ChangeCorrectionProposal> proposals) { String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description; AST ast = toRemove.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_UNREACHABLE_CODE_INCLUDING_CONDITION); if (replacement == null || replacement instanceof EmptyStatement || replacement instanceof Block && ((Block) replacement).statements().size() == 0) { if (ASTNodes.isControlStatementBody(toRemove.getLocationInParent())) { rewrite.replace(toRemove, toRemove.getAST().newBlock(), null); } else { rewrite.remove(toRemove, null); } } else if (toRemove instanceof Expression && replacement instanceof Expression) { Expression moved = (Expression) rewrite.createMoveTarget(replacement); Expression toRemoveExpression = (Expression) toRemove; Expression replacementExpression = (Expression) replacement; ITypeBinding explicitCast = ASTNodes.getExplicitCast(replacementExpression, toRemoveExpression); if (explicitCast != null) { CastExpression cast = ast.newCastExpression(); if (NecessaryParenthesesChecker.needsParentheses(replacementExpression, cast, CastExpression.EXPRESSION_PROPERTY)) { ParenthesizedExpression parenthesized = ast.newParenthesizedExpression(); parenthesized.setExpression(moved); moved = parenthesized; } cast.setExpression(moved); ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot()); ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(toRemove, imports); cast.setType(imports.addImport(explicitCast, ast, importRewriteContext, TypeLocation.CAST)); moved = cast; } rewrite.replace(toRemove, moved, null); } else { ASTNode parent = toRemove.getParent(); ASTNode moveTarget; if ((parent instanceof Block || parent instanceof SwitchStatement) && replacement instanceof Block) { ListRewrite listRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY); List<Statement> list = ((Block) replacement).statements(); int lastIndex = list.size() - 1; moveTarget = listRewrite.createMoveTarget(list.get(0), list.get(lastIndex)); } else { moveTarget = rewrite.createMoveTarget(replacement); } rewrite.replace(toRemove, moveTarget, null); } proposals.add(proposal); }
Example 19
Source File: InvertBooleanUtility.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private static ParenthesizedExpression getParenthesizedExpression(AST ast, Expression expression) { ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression(); parenthesizedExpression.setExpression(expression); return parenthesizedExpression; }
Example 20
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; }