Java Code Examples for org.eclipse.jdt.core.dom.InfixExpression#getOperator()
The following examples show how to use
org.eclipse.jdt.core.dom.InfixExpression#getOperator() .
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: InvertBooleanUtility.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private static void breakInfixOperationAtOperation(ASTRewrite rewrite, Expression expression, Operator operator, int operatorOffset, boolean removeParentheses, Expression[] res) { if (expression.getStartPosition() + expression.getLength() <= operatorOffset) { // add to the left res[0] = combineOperands(rewrite, res[0], expression, removeParentheses, operator); return; } if (operatorOffset <= expression.getStartPosition()) { // add to the right res[1] = combineOperands(rewrite, res[1], expression, removeParentheses, operator); return; } if (!(expression instanceof InfixExpression)) { throw new IllegalArgumentException("Cannot break up non-infix expression"); //$NON-NLS-1$ } InfixExpression infixExpression = (InfixExpression) expression; if (infixExpression.getOperator() != operator) { throw new IllegalArgumentException("Incompatible operator"); //$NON-NLS-1$ } breakInfixOperationAtOperation(rewrite, infixExpression.getLeftOperand(), operator, operatorOffset, removeParentheses, res); breakInfixOperationAtOperation(rewrite, infixExpression.getRightOperand(), operator, operatorOffset, removeParentheses, res); List<Expression> extended = infixExpression.extendedOperands(); for (int i = 0; i < extended.size(); i++) { breakInfixOperationAtOperation(rewrite, extended.get(i), operator, operatorOffset, removeParentheses, res); } }
Example 2
Source File: UseEqualsResolution.java From spotbugs with GNU Lesser General Public License v2.1 | 6 votes |
@Override protected void repairBug(ASTRewrite rewrite, CompilationUnit workingUnit, BugInstance bug) throws BugResolutionException { Assert.isNotNull(rewrite); Assert.isNotNull(workingUnit); Assert.isNotNull(bug); InfixExpression[] stringEqualityChecks = findStringEqualityChecks(getASTNode(workingUnit, bug.getPrimarySourceLineAnnotation())); for (InfixExpression stringEqualityCheck : stringEqualityChecks) { Operator operator = stringEqualityCheck.getOperator(); Expression replaceExpression; if (EQUALS.equals(operator)) { replaceExpression = createEqualsExpression(rewrite, stringEqualityCheck); } else if (NOT_EQUALS.equals(operator)) { replaceExpression = createNotEqualsExpression(rewrite, stringEqualityCheck); } else { throw new BugResolutionException("Illegal operator '" + operator + "' found."); } rewrite.replace(stringEqualityCheck, replaceExpression, null); } }
Example 3
Source File: ASTFlattenerUtils.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
private Iterable<StringLiteral> collectCompatibleNodes(final InfixExpression node) { final ArrayList<StringLiteral> strings = CollectionLiterals.<StringLiteral>newArrayList(); InfixExpression.Operator _operator = node.getOperator(); boolean _notEquals = (!Objects.equal(_operator, InfixExpression.Operator.PLUS)); if (_notEquals) { return strings; } final Expression left = node.getLeftOperand(); if ((left instanceof StringLiteral)) { strings.add(((StringLiteral)left)); } else { if ((left instanceof InfixExpression)) { Iterables.<StringLiteral>addAll(strings, this.collectCompatibleNodes(((InfixExpression)left))); } } final Expression right = node.getRightOperand(); if ((right instanceof StringLiteral)) { strings.add(((StringLiteral)right)); } else { if ((right instanceof InfixExpression)) { Iterables.<StringLiteral>addAll(strings, this.collectCompatibleNodes(((InfixExpression)right))); } } Iterables.<StringLiteral>addAll(strings, Iterables.<StringLiteral>filter(node.extendedOperands(), StringLiteral.class)); return strings; }
Example 4
Source File: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static void breakInfixOperationAtOperation(ASTRewrite rewrite, Expression expression, Operator operator, int operatorOffset, boolean removeParentheses, Expression[] res) { if (expression.getStartPosition() + expression.getLength() <= operatorOffset) { // add to the left res[0]= combineOperands(rewrite, res[0], expression, removeParentheses, operator); return; } if (operatorOffset <= expression.getStartPosition()) { // add to the right res[1]= combineOperands(rewrite, res[1], expression, removeParentheses, operator); return; } if (!(expression instanceof InfixExpression)) { throw new IllegalArgumentException("Cannot break up non-infix expression"); //$NON-NLS-1$ } InfixExpression infixExpression= (InfixExpression) expression; if (infixExpression.getOperator() != operator) { throw new IllegalArgumentException("Incompatible operator"); //$NON-NLS-1$ } breakInfixOperationAtOperation(rewrite, infixExpression.getLeftOperand(), operator, operatorOffset, removeParentheses, res); breakInfixOperationAtOperation(rewrite, infixExpression.getRightOperand(), operator, operatorOffset, removeParentheses, res); List<Expression> extended= infixExpression.extendedOperands(); for (int i= 0; i < extended.size(); i++) { breakInfixOperationAtOperation(rewrite, extended.get(i), operator, operatorOffset, removeParentheses, res); } }
Example 5
Source File: ExpressionsFix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private boolean needsParentesis(ASTNode node) { if (!(node.getParent() instanceof InfixExpression)) return false; if (node instanceof InstanceofExpression) return true; if (node instanceof InfixExpression) { InfixExpression expression = (InfixExpression) node; InfixExpression.Operator operator = expression.getOperator(); InfixExpression parentExpression = (InfixExpression) node.getParent(); InfixExpression.Operator parentOperator = parentExpression.getOperator(); if (parentOperator == operator) return false; return true; } return false; }
Example 6
Source File: InfixExpressionWriter.java From juniversal with MIT License | 5 votes |
@SuppressWarnings("unchecked") @Override public void write(InfixExpression infixExpression) { InfixExpression.Operator operator = infixExpression.getOperator(); if (operator == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED) { write("rightShiftUnsigned("); writeNode(infixExpression.getLeftOperand()); // Skip spaces before the >>> but if there's a newline (or comments) there, copy them skipSpacesAndTabs(); copySpaceAndComments(); matchAndWrite(">>>", ","); copySpaceAndComments(); writeNode(infixExpression.getRightOperand()); write(")"); } else { writeNode(infixExpression.getLeftOperand()); copySpaceAndComments(); String operatorToken = this.equivalentOperators.get(infixExpression.getOperator()); matchAndWrite(operatorToken); copySpaceAndComments(); writeNode(infixExpression.getRightOperand()); if (infixExpression.hasExtendedOperands()) { for (Expression extendedOperand : (List<Expression>) infixExpression.extendedOperands()) { copySpaceAndComments(); matchAndWrite(operatorToken); copySpaceAndComments(); writeNode(extendedOperand); } } } }
Example 7
Source File: InfixExpressionWriter.java From juniversal with MIT License | 5 votes |
@Override public void write(InfixExpression infixExpression) { InfixExpression.Operator operator = infixExpression.getOperator(); if (operator == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED) { writeRightShiftUnsigned(infixExpression); } else { writeNode(infixExpression.getLeftOperand()); copySpaceAndComments(); String operatorToken = this.equivalentOperators.get(operator); matchAndWrite(operatorToken); copySpaceAndComments(); writeNode(infixExpression.getRightOperand()); if (infixExpression.hasExtendedOperands()) { forEach(infixExpression.extendedOperands(), (Expression extendedOperand) -> { copySpaceAndComments(); matchAndWrite(operatorToken); copySpaceAndComments(); writeNode(extendedOperand); }); } } }
Example 8
Source File: IfStatementExpressionAnalyzer.java From JDeodorant with MIT License | 5 votes |
public List<InfixExpression> getInfixExpressionsWithEqualsOperator() { List<InfixExpression> expressionList = new ArrayList<InfixExpression>(); DefaultMutableTreeNode leaf = root.getFirstLeaf(); while(leaf != null) { Expression expression = (Expression)leaf.getUserObject(); if(expression instanceof InfixExpression) { InfixExpression infixExpression = (InfixExpression)expression; InfixExpression.Operator operator = infixExpression.getOperator(); if(operator.equals(InfixExpression.Operator.EQUALS)) expressionList.add(infixExpression); } leaf = leaf.getNextLeaf(); } return expressionList; }
Example 9
Source File: IfStatementExpressionAnalyzer.java From JDeodorant with MIT License | 5 votes |
private void processExpression(DefaultMutableTreeNode parent, Expression expression) { if(expression instanceof InfixExpression) { InfixExpression infixExpression = (InfixExpression)expression; InfixExpression.Operator operator = infixExpression.getOperator(); if(operator.equals(InfixExpression.Operator.CONDITIONAL_AND) || operator.equals(InfixExpression.Operator.CONDITIONAL_OR)) { parent.setUserObject(operator); DefaultMutableTreeNode leftOperandNode = new DefaultMutableTreeNode(); DefaultMutableTreeNode rightOperandNode = new DefaultMutableTreeNode(); parent.add(leftOperandNode); parent.add(rightOperandNode); processExpression(leftOperandNode, infixExpression.getLeftOperand()); processExpression(rightOperandNode, infixExpression.getRightOperand()); if(infixExpression.hasExtendedOperands()) { DefaultMutableTreeNode grandParent = (DefaultMutableTreeNode)parent.getParent(); int parentIndex = -1; if(grandParent != null) parentIndex = grandParent.getIndex(parent); DefaultMutableTreeNode newParent = processExtendedOperands(parent, infixExpression.extendedOperands()); if(grandParent != null) grandParent.insert(newParent, parentIndex); else root = newParent; } } else { parent.setUserObject(infixExpression); } } else { parent.setUserObject(expression); } }
Example 10
Source File: LocalCorrectionsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override public boolean visit(InfixExpression e) { InfixExpression.Operator op= e.getOperator(); if (isBitOperation(op)) { return true; } else if (op == InfixExpression.Operator.EQUALS || op == InfixExpression.Operator.NOT_EQUALS) { fCompareExpression= e; return false; } return false; }
Example 11
Source File: QuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static boolean getConvertStringConcatenationProposals(IInvocationContext context, Collection<ICommandAccess> resultingCollections) { ASTNode node= context.getCoveringNode(); BodyDeclaration parentDecl= ASTResolving.findParentBodyDeclaration(node); if (!(parentDecl instanceof MethodDeclaration || parentDecl instanceof Initializer)) return false; AST ast= node.getAST(); ITypeBinding stringBinding= ast.resolveWellKnownType("java.lang.String"); //$NON-NLS-1$ if (node instanceof Expression && !(node instanceof InfixExpression)) { node= node.getParent(); } if (node instanceof VariableDeclarationFragment) { node= ((VariableDeclarationFragment) node).getInitializer(); } else if (node instanceof Assignment) { node= ((Assignment) node).getRightHandSide(); } InfixExpression oldInfixExpression= null; while (node instanceof InfixExpression) { InfixExpression curr= (InfixExpression) node; if (curr.resolveTypeBinding() == stringBinding && curr.getOperator() == InfixExpression.Operator.PLUS) { oldInfixExpression= curr; // is a infix expression we can use } else { break; } node= node.getParent(); } if (oldInfixExpression == null) return false; if (resultingCollections == null) { return true; } LinkedCorrectionProposal stringBufferProposal= getConvertToStringBufferProposal(context, ast, oldInfixExpression); resultingCollections.add(stringBufferProposal); ASTRewriteCorrectionProposal messageFormatProposal= getConvertToMessageFormatProposal(context, ast, oldInfixExpression); if (messageFormatProposal != null) resultingCollections.add(messageFormatProposal); return true; }
Example 12
Source File: AdvancedQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static boolean getExchangeOperandsProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) { // check that user invokes quick assist on infix expression if (!(node instanceof InfixExpression)) { return false; } InfixExpression infixExpression= (InfixExpression)node; Operator operator= infixExpression.getOperator(); if (operator != InfixExpression.Operator.CONDITIONAL_AND && operator != InfixExpression.Operator.AND && operator != InfixExpression.Operator.CONDITIONAL_OR && operator != InfixExpression.Operator.OR && operator != InfixExpression.Operator.EQUALS && operator != InfixExpression.Operator.NOT_EQUALS && operator != InfixExpression.Operator.LESS && operator != InfixExpression.Operator.LESS_EQUALS && operator != InfixExpression.Operator.GREATER && operator != InfixExpression.Operator.GREATER_EQUALS && operator != InfixExpression.Operator.PLUS && operator != InfixExpression.Operator.TIMES && operator != InfixExpression.Operator.XOR) { return false; } int offset= isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength()); if (offset == -1) { return false; } // we could produce quick assist if (resultingCollections == null) { return true; } AST ast= infixExpression.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); // prepare left and right expressions Expression leftExpression= null; Expression rightExpression= null; InfixExpression currentExpression= infixExpression; leftExpression= combineOperands(rewrite, leftExpression, infixExpression.getLeftOperand(), false, operator); if (infixExpression.getRightOperand().getStartPosition() <= context.getSelectionOffset()) { leftExpression= combineOperands(rewrite, leftExpression, infixExpression.getRightOperand(), false, operator); } else { rightExpression= combineOperands(rewrite, rightExpression, infixExpression.getRightOperand(), false, operator); } for (Iterator<Expression> iter= currentExpression.extendedOperands().iterator(); iter.hasNext();) { Expression extendedOperand= iter.next(); if (extendedOperand.getStartPosition() <= context.getSelectionOffset()) { leftExpression= combineOperands(rewrite, leftExpression, extendedOperand, false, operator); } else { rightExpression= combineOperands(rewrite, rightExpression, extendedOperand, false, operator); } } if (NecessaryParenthesesChecker.needsParentheses(leftExpression, infixExpression, InfixExpression.RIGHT_OPERAND_PROPERTY)) { leftExpression= getParenthesizedExpression(ast, leftExpression); } if (NecessaryParenthesesChecker.needsParentheses(rightExpression, infixExpression, InfixExpression.LEFT_OPERAND_PROPERTY)) { rightExpression= getParenthesizedExpression(ast, rightExpression); } if (operator == InfixExpression.Operator.LESS) { operator= InfixExpression.Operator.GREATER; } else if (operator == InfixExpression.Operator.LESS_EQUALS) { operator= InfixExpression.Operator.GREATER_EQUALS; } else if (operator == InfixExpression.Operator.GREATER) { operator= InfixExpression.Operator.LESS; } else if (operator == InfixExpression.Operator.GREATER_EQUALS) { operator= InfixExpression.Operator.LESS_EQUALS; } // create new infix expression InfixExpression newInfix= ast.newInfixExpression(); newInfix.setOperator(operator); newInfix.setLeftOperand(rightExpression); newInfix.setRightOperand(leftExpression); rewrite.replace(infixExpression, newInfix, null); // add correction proposal String label= CorrectionMessages.AdvancedQuickAssistProcessor_exchangeOperands_description; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_OPERANDS, image); resultingCollections.add(proposal); return true; }
Example 13
Source File: NecessaryParenthesesChecker.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static boolean needsParenthesesInInfixExpression(Expression expression, InfixExpression parentInfix, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType) { InfixExpression.Operator parentInfixOperator= parentInfix.getOperator(); ITypeBinding rightOperandType; ITypeBinding parentInfixExprType; if (leftOperandType == null) { // parentInfix has bindings leftOperandType= parentInfix.getLeftOperand().resolveTypeBinding(); rightOperandType= parentInfix.getRightOperand().resolveTypeBinding(); parentInfixExprType= parentInfix.resolveTypeBinding(); } else { rightOperandType= expression.resolveTypeBinding(); parentInfixExprType= getInfixExpressionType(parentInfixOperator, leftOperandType, rightOperandType); } boolean isAllOperandsHaveSameType= isAllOperandsHaveSameType(parentInfix, leftOperandType, rightOperandType); if (locationInParent == InfixExpression.LEFT_OPERAND_PROPERTY) { //we have (expr op expr) op expr //infix expressions are evaluated from left to right -> parentheses not needed return false; } else if (isAssociative(parentInfixOperator, parentInfixExprType, isAllOperandsHaveSameType)) { //we have parent op (expr op expr) and op is associative //left op (right) == (right) op left == right op left if (expression instanceof InfixExpression) { InfixExpression infixExpression= (InfixExpression)expression; Operator operator= infixExpression.getOperator(); if (isStringType(parentInfixExprType)) { if (parentInfixOperator == InfixExpression.Operator.PLUS && operator == InfixExpression.Operator.PLUS && isStringType(infixExpression.resolveTypeBinding())) { // 1 + ("" + 2) == 1 + "" + 2 // 1 + (2 + "") != 1 + 2 + "" // "" + (2 + "") == "" + 2 + "" return !isStringType(infixExpression.getLeftOperand().resolveTypeBinding()) && !isStringType(leftOperandType); } //"" + (1 + 2), "" + (1 - 2) etc return true; } if (parentInfixOperator != InfixExpression.Operator.TIMES) return false; if (operator == InfixExpression.Operator.TIMES) // x * (y * z) == x * y * z return false; if (operator == InfixExpression.Operator.REMAINDER || operator == InfixExpression.Operator.DIVIDE) // x * (y % z) != x * y % z , x * (y / z) == x * y / z rounding involved return true; return false; } return false; } else { return true; } }
Example 14
Source File: AssociativeInfixExpressionFragment.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static boolean isParentInfixWithSameOperator(InfixExpression node) { return node.getParent() instanceof InfixExpression && ((InfixExpression) node.getParent()).getOperator() == node.getOperator(); }
Example 15
Source File: AbstractLoopUtilities.java From JDeodorant with MIT License | 4 votes |
private static Integer assignmentUpdateValue(Assignment assignment) { Integer updateValue = null; Expression leftHandSide = assignment.getLeftHandSide(); Assignment.Operator operator = assignment.getOperator(); Expression rightHandSide = assignment.getRightHandSide(); if (operator == Assignment.Operator.PLUS_ASSIGN) { updateValue = AbstractLoopUtilities.getIntegerValue(rightHandSide); } else if (operator == Assignment.Operator.MINUS_ASSIGN) { Integer rightHandSideIntegerValue = AbstractLoopUtilities.getIntegerValue(rightHandSide); if (rightHandSideIntegerValue != null) { updateValue = (-1) * rightHandSideIntegerValue; } } else if (leftHandSide instanceof SimpleName && operator == Assignment.Operator.ASSIGN && rightHandSide instanceof InfixExpression) { SimpleName leftHandSideSimpleName = (SimpleName) leftHandSide; IBinding leftHandSideBinding = leftHandSideSimpleName.resolveBinding(); InfixExpression infixExpression = (InfixExpression) rightHandSide; InfixExpression.Operator infixOperator = infixExpression.getOperator(); Expression rightOperand = infixExpression.getRightOperand(); Expression leftOperand = infixExpression.getLeftOperand(); if (infixOperator.toString().equals("+") || infixOperator.toString().equals("-")) { if (leftOperand instanceof SimpleName) { SimpleName leftOperandSimpleName = (SimpleName) leftOperand; IBinding leftOperandBinding = leftOperandSimpleName.resolveBinding(); if (leftOperandBinding.isEqualTo(leftHandSideBinding)) { Integer rightOperandIntegerValue = AbstractLoopUtilities.getIntegerValue(rightOperand); if (infixOperator.toString().equals("+") && rightOperandIntegerValue != null) { updateValue = rightOperandIntegerValue; } else if (infixOperator.toString().equals("-") && rightOperandIntegerValue != null) { updateValue = (-1) * rightOperandIntegerValue; } } } else if (rightOperand instanceof SimpleName) { SimpleName rightOperandSimpleName = (SimpleName) rightOperand; IBinding rightOperandBinding = rightOperandSimpleName.resolveBinding(); if (rightOperandBinding.isEqualTo(leftHandSideBinding)) { Integer leftOperandIntegerValue = AbstractLoopUtilities.getIntegerValue(leftOperand); if (infixOperator.toString().equals("+") && leftOperandIntegerValue != null) { updateValue = leftOperandIntegerValue; } } } } } return updateValue; }
Example 16
Source File: InfixExpressionWriter.java From juniversal with MIT License | 4 votes |
@SuppressWarnings("unchecked") @Override public void write(ASTNode node) { InfixExpression infixExpression = (InfixExpression) node; // TODO: Add spaces to left & right of binary operators if needed, per Swift's rules about needing space on // both sides or neither InfixExpression.Operator operator = infixExpression.getOperator(); if (operator == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED) { // TODO: Handle this write("rightShiftUnsigned("); swiftASTWriters.writeNode(infixExpression.getLeftOperand()); // Skip spaces before the >>> but if there's a newline (or comments) there, copy them skipSpacesAndTabs(); copySpaceAndComments(); matchAndWrite(">>>", ","); copySpaceAndComments(); swiftASTWriters.writeNode(infixExpression.getRightOperand()); write(")"); } else { swiftASTWriters.writeNode(infixExpression.getLeftOperand()); copySpaceAndComments(); String operatorToken = this.equivalentOperators.get(infixExpression.getOperator()); matchAndWrite(operatorToken); copySpaceAndComments(); swiftASTWriters.writeNode(infixExpression.getRightOperand()); if (infixExpression.hasExtendedOperands()) { for (Expression extendedOperand : (List<Expression>) infixExpression.extendedOperands()) { copySpaceAndComments(); matchAndWrite(operatorToken); copySpaceAndComments(); swiftASTWriters.writeNode(extendedOperand); } } } }
Example 17
Source File: UseEqualsResolution.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
static boolean isStringEqualityCheck(InfixExpression infix) { Operator op = infix.getOperator(); return (EQUALS.equals(op) || NOT_EQUALS.equals(op)) && isStringOperand(infix.getLeftOperand()) && isStringOperand(infix.getRightOperand()); }
Example 18
Source File: AssociativeInfixExpressionFragment.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private static boolean isParentInfixWithSameOperator(InfixExpression node) { return node.getParent() instanceof InfixExpression && ((InfixExpression) node.getParent()).getOperator() == node.getOperator(); }
Example 19
Source File: NumberOfComparisons.java From ck with Apache License 2.0 | 3 votes |
@Override public void visit(InfixExpression node) { if(node.getOperator() == InfixExpression.Operator.EQUALS) qty++; if(node.getOperator() == InfixExpression.Operator.NOT_EQUALS) qty++; }