List of usage examples for org.eclipse.jdt.core.dom InfixExpression getRightOperand
public Expression getRightOperand()
From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java
License:Open Source License
@Override public boolean visit(InfixExpression node) { node.getLeftOperand().accept(this); this.fBuffer.append(' '); // for cases like x= i - -1; or x= i++ + ++i; this.fBuffer.append(node.getOperator().toString()); this.fBuffer.append(' '); node.getRightOperand().accept(this); final List<Expression> extendedOperands = node.extendedOperands(); if (extendedOperands.size() != 0) { this.fBuffer.append(' '); for (Iterator<Expression> it = extendedOperands.iterator(); it.hasNext();) { this.fBuffer.append(node.getOperator().toString()).append(' '); Expression e = it.next(); e.accept(this); }// w ww . ja va2 s .c o m } return false; }
From source file:boa.datagen.util.Java7Visitor.java
License:Apache License
@Override public boolean visit(InfixExpression node) { boa.types.Ast.Expression.Builder b = boa.types.Ast.Expression.newBuilder(); // b.setPosition(pos.build()); if (node.getOperator() == InfixExpression.Operator.AND) b.setKind(boa.types.Ast.Expression.ExpressionKind.BIT_AND); else if (node.getOperator() == InfixExpression.Operator.CONDITIONAL_AND) b.setKind(boa.types.Ast.Expression.ExpressionKind.LOGICAL_AND); else if (node.getOperator() == InfixExpression.Operator.CONDITIONAL_OR) b.setKind(boa.types.Ast.Expression.ExpressionKind.LOGICAL_OR); else if (node.getOperator() == InfixExpression.Operator.DIVIDE) b.setKind(boa.types.Ast.Expression.ExpressionKind.OP_DIV); else if (node.getOperator() == InfixExpression.Operator.EQUALS) b.setKind(boa.types.Ast.Expression.ExpressionKind.EQ); else if (node.getOperator() == InfixExpression.Operator.GREATER) b.setKind(boa.types.Ast.Expression.ExpressionKind.GT); else if (node.getOperator() == InfixExpression.Operator.GREATER_EQUALS) b.setKind(boa.types.Ast.Expression.ExpressionKind.GTEQ); else if (node.getOperator() == InfixExpression.Operator.LEFT_SHIFT) b.setKind(boa.types.Ast.Expression.ExpressionKind.BIT_LSHIFT); else if (node.getOperator() == InfixExpression.Operator.LESS) b.setKind(boa.types.Ast.Expression.ExpressionKind.LT); else if (node.getOperator() == InfixExpression.Operator.LESS_EQUALS) b.setKind(boa.types.Ast.Expression.ExpressionKind.LTEQ); else if (node.getOperator() == InfixExpression.Operator.MINUS) b.setKind(boa.types.Ast.Expression.ExpressionKind.OP_SUB); else if (node.getOperator() == InfixExpression.Operator.NOT_EQUALS) b.setKind(boa.types.Ast.Expression.ExpressionKind.NEQ); else if (node.getOperator() == InfixExpression.Operator.OR) b.setKind(boa.types.Ast.Expression.ExpressionKind.BIT_OR); else if (node.getOperator() == InfixExpression.Operator.PLUS) b.setKind(boa.types.Ast.Expression.ExpressionKind.OP_ADD); else if (node.getOperator() == InfixExpression.Operator.REMAINDER) b.setKind(boa.types.Ast.Expression.ExpressionKind.OP_MOD); else if (node.getOperator() == InfixExpression.Operator.RIGHT_SHIFT_SIGNED) b.setKind(boa.types.Ast.Expression.ExpressionKind.BIT_RSHIFT); else if (node.getOperator() == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED) b.setKind(boa.types.Ast.Expression.ExpressionKind.BIT_UNSIGNEDRSHIFT); else if (node.getOperator() == InfixExpression.Operator.TIMES) b.setKind(boa.types.Ast.Expression.ExpressionKind.OP_MULT); else if (node.getOperator() == InfixExpression.Operator.XOR) b.setKind(boa.types.Ast.Expression.ExpressionKind.BIT_XOR); node.getLeftOperand().accept(this); b.addExpressions(expressions.pop()); node.getRightOperand().accept(this); b.addExpressions(expressions.pop()); for (Object e : node.extendedOperands()) { ((org.eclipse.jdt.core.dom.Expression) e).accept(this); b.addExpressions(expressions.pop()); }/*w ww.jav a 2 s .com*/ expressions.push(b.build()); return false; }
From source file:ca.mcgill.cs.swevo.ppa.inference.BinaryInferenceStrategy.java
License:Open Source License
@Override public List<PPAIndex> getSecondaryIndexes(ASTNode node) { List<PPAIndex> list = super.getSecondaryIndexes(node); InfixExpression infix = (InfixExpression) node; Expression left = infix.getLeftOperand(); Expression right = infix.getRightOperand(); if (indexer.isIndexable(left)) { list.add(indexer.getMainIndex(left)); }/*from w w w.j a va 2s. c om*/ if (indexer.isIndexable(right)) { list.add(indexer.getMainIndex(right)); } return list; }
From source file:ca.mcgill.cs.swevo.ppa.inference.BinaryInferenceStrategy.java
License:Open Source License
private void processOperators(ASTNode node, TypeFact newFact) { InfixExpression infix = (InfixExpression) node; InfixExpression.Operator operator = infix.getOperator(); // if (node.toString().equals("falseTb == ShortBinding")) { // System.out.println(); // }/*from ww w .ja va 2 s . c o m*/ Expression left = infix.getLeftOperand(); Expression right = infix.getRightOperand(); boolean isSecondaryIndex = newFact != null && isSecondary(node, newFact); boolean isPrimaryIndex = newFact != null && !isSecondaryIndex; boolean leftSafe = !PPABindingsUtil.isUnknownType(left.resolveTypeBinding()) && (indexer.isSafe(left) || (isSecondaryIndex && isLeftIndex(node, newFact))); boolean rightSafe = !PPABindingsUtil.isUnknownType(right.resolveTypeBinding()) && (indexer.isSafe(right) || (isSecondaryIndex && !isLeftIndex(node, newFact))); boolean leftBoolean = isBoolean(left, leftSafe); boolean rightBoolean = isBoolean(right, rightSafe); ITypeBinding newType = null; ITypeBinding factType = newFact != null ? newFact.getNewType() : null; boolean isNewType = true; // && and || if (operator == InfixExpression.Operator.CONDITIONAL_AND || operator == InfixExpression.Operator.CONDITIONAL_OR) { if (!leftSafe) { ITypeBinding newBinding = ppaEngine.getRegistry().getPrimitiveBinding("boolean", left); ITypeBinding oldBinding = left.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(left), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); } if (!rightSafe) { ITypeBinding newBinding = ppaEngine.getRegistry().getPrimitiveBinding("boolean", right); ITypeBinding oldBinding = right.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(right), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); } } // & and | else if (operator == InfixExpression.Operator.AND || operator == InfixExpression.Operator.CONDITIONAL_OR || operator == InfixExpression.Operator.XOR) { if (leftBoolean && !rightSafe) { ITypeBinding newBinding = ppaEngine.getRegistry().getPrimitiveBinding("boolean", right); ITypeBinding oldBinding = right.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(right), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); newType = newBinding; } else if (rightBoolean && !leftSafe) { ITypeBinding newBinding = ppaEngine.getRegistry().getPrimitiveBinding("boolean", left); ITypeBinding oldBinding = left.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(left), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); newType = newBinding; } else if (leftSafe && !rightSafe) { ITypeBinding newBinding = left.resolveTypeBinding(); ITypeBinding oldBinding = right.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(right), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); newType = newBinding; } else if (rightSafe && !leftSafe) { ITypeBinding newBinding = right.resolveTypeBinding(); ITypeBinding oldBinding = left.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(left), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); newType = newBinding; } else if (!rightSafe && !leftSafe && isPrimaryIndex) { ITypeBinding newBinding = factType; ITypeBinding oldBinding = left.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(left), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); newBinding = factType; oldBinding = right.resolveTypeBinding(); typeFact = new TypeFact(indexer.getMainIndex(right), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); newType = newBinding; } } // << >> >>> else if (operator == InfixExpression.Operator.LEFT_SHIFT || operator == InfixExpression.Operator.RIGHT_SHIFT_SIGNED || operator == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED) { if (!leftSafe) { ITypeBinding newBinding = ppaEngine.getRegistry().getPrimitiveBinding("int", left); ITypeBinding oldBinding = left.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(left), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); } if (!rightSafe) { ITypeBinding newBinding = ppaEngine.getRegistry().getPrimitiveBinding("int", right); ITypeBinding oldBinding = right.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(right), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); newType = newBinding; } } // - / * % < <= > >= else if (operator == InfixExpression.Operator.MINUS || operator == InfixExpression.Operator.DIVIDE || operator == InfixExpression.Operator.REMAINDER || operator == InfixExpression.Operator.TIMES || operator == InfixExpression.Operator.LESS || operator == InfixExpression.Operator.LESS_EQUALS || operator == InfixExpression.Operator.GREATER || operator == InfixExpression.Operator.GREATER_EQUALS) { if (leftSafe && !rightSafe) { ITypeBinding newBinding = left.resolveTypeBinding(); ITypeBinding oldBinding = right.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(right), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); newType = newBinding; } else if (rightSafe && !leftSafe) { ITypeBinding newBinding = right.resolveTypeBinding(); ITypeBinding oldBinding = left.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(left), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); newType = newBinding; } else if (!rightSafe && !leftSafe && !isPrimaryIndex) { ITypeBinding newBinding = ppaEngine.getRegistry().getPrimitiveBinding("int", left); ITypeBinding oldBinding = left.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(left), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); newBinding = ppaEngine.getRegistry().getPrimitiveBinding("int", right); oldBinding = right.resolveTypeBinding(); typeFact = new TypeFact(indexer.getMainIndex(right), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); newType = newBinding; } else if (!rightSafe && !leftSafe && isPrimaryIndex) { ITypeBinding newBinding = factType; ITypeBinding oldBinding = left.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(left), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); newBinding = factType; oldBinding = right.resolveTypeBinding(); typeFact = new TypeFact(indexer.getMainIndex(right), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); newType = newBinding; } } // == != else if (operator == InfixExpression.Operator.EQUALS || operator == InfixExpression.Operator.NOT_EQUALS) { if (leftSafe && !rightSafe) { ITypeBinding newBinding = left.resolveTypeBinding(); ITypeBinding oldBinding = right.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(right), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); } else if (rightSafe && !leftSafe) { ITypeBinding newBinding = right.resolveTypeBinding(); ITypeBinding oldBinding = left.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(left), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); } } // + else if (operator == InfixExpression.Operator.PLUS) { if (isPrimaryIndex && !factType.isPrimitive()) { ITypeBinding newBinding = factType; if (!leftSafe) { ITypeBinding oldBinding = left.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(left), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); } if (!rightSafe) { newBinding = factType; ITypeBinding oldBinding = right.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(right), oldBinding, TypeFact.UNKNOWN, newBinding, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); isNewType = isNewType && ppaEngine.reportTypeFact(typeFact); } newType = newBinding; } } if (newType != null && !isPrimaryIndex && isNewType) { ITypeBinding oldBinding = infix.resolveTypeBinding(); TypeFact typeFact = new TypeFact(indexer.getMainIndex(node), oldBinding, TypeFact.UNKNOWN, newType, TypeFact.SUBTYPE, TypeFact.BINARY_STRATEGY); ppaEngine.reportTypeFact(typeFact); // report new type for this expression. // fix this expression. } else if (isPrimaryIndex) { if (newType != null && isNewType) { ppaEngine.getRegistry().fixBinary(node, newType); } else if (factType != null) { ppaEngine.getRegistry().fixBinary(node, factType); } } }
From source file:ca.mcgill.cs.swevo.ppa.inference.BinaryInferenceStrategy.java
License:Open Source License
public boolean isSafe(ASTNode node) { InfixExpression infix = (InfixExpression) node; boolean leftSafe = true; boolean rightSafe = true; Expression left = infix.getLeftOperand(); Expression right = infix.getRightOperand(); leftSafe = indexer.isSafe(left);/*from ww w. j av a 2 s. c o m*/ rightSafe = indexer.isSafe(right); return leftSafe && rightSafe; }
From source file:ch.acanda.eclipse.pmd.java.resolution.design.UseCollectionIsEmptyQuickFix.java
License:Open Source License
/** * Replaces {@code x.size() == 0} or {@code 0 == x.size()} with {@code x.isEmpty()}. Replaces {@code x.size() != 0} * or {@code 0 != x.size()} with {@code !x.isEmpty()}. *///from w w w . ja va 2 s . com @Override protected boolean apply(final InfixExpression node) { final MethodInvocation size; if (node.getLeftOperand() instanceof MethodInvocation) { size = (MethodInvocation) node.getLeftOperand(); } else if (node.getRightOperand() instanceof MethodInvocation) { size = (MethodInvocation) node.getRightOperand(); } else { return false; } final AST ast = node.getAST(); final MethodInvocation invocation = (MethodInvocation) ast.createInstance(MethodInvocation.class); invocation.setExpression(ASTUtil.copy(size.getExpression())); final SimpleName isEmpty = (SimpleName) ast.createInstance(SimpleName.class); isEmpty.setIdentifier("isEmpty"); invocation.setName(isEmpty); final Expression replacement; if (isNotEmpty(node)) { final PrefixExpression not = (PrefixExpression) ast.createInstance(PrefixExpression.class); not.setOperator(org.eclipse.jdt.core.dom.PrefixExpression.Operator.NOT); not.setOperand(invocation); replacement = not; } else { replacement = invocation; } ASTUtil.replace(node, replacement); return true; }
From source file:ch.acanda.eclipse.pmd.java.resolution.optimization.AddEmptyStringQuickFix.java
License:Open Source License
@Override @SuppressWarnings("unchecked") protected boolean apply(final InfixExpression node) { final Expression rightOperand = node.getRightOperand(); // "" + "abc" -> "abc" // "" + x.toString() -> x.toString() if (isString(rightOperand)) { return replace(node, copy(rightOperand)); }/*from w w w .ja v a2s .c om*/ // "" + 'a' -> "a" if (isCharacterLiteral(rightOperand)) { final AST ast = node.getAST(); final StringLiteral stringLiteral = ast.newStringLiteral(); final String escapedCharacter = ((CharacterLiteral) rightOperand).getEscapedValue(); stringLiteral.setEscapedValue(convertToEscapedString(escapedCharacter)); return replace(node, stringLiteral); } // "" + x -> String.valueOf(x) final AST ast = node.getAST(); final MethodInvocation toString = ast.newMethodInvocation(); toString.setExpression(ast.newSimpleName("String")); toString.setName(ast.newSimpleName("valueOf")); toString.arguments().add(copy(rightOperand)); return replace(node, toString); }
From source file:coloredide.utils.CopiedNaiveASTFlattener.java
License:Open Source License
public boolean visit(InfixExpression node) { node.getLeftOperand().accept(this); this.buffer.append(' '); // for cases like x= i - -1; or x= i++ + // ++i;/*from www .j a va2s . co m*/ this.buffer.append(node.getOperator().toString()); this.buffer.append(' '); node.getRightOperand().accept(this); final List extendedOperands = node.extendedOperands(); if (extendedOperands.size() != 0) { this.buffer.append(' '); for (Iterator it = extendedOperands.iterator(); it.hasNext();) { this.buffer.append(node.getOperator().toString()).append(' '); Expression e = (Expression) it.next(); e.accept(this); } } return false; }
From source file:com.bsiag.eclipse.jdt.java.formatter.linewrap.WrapPreparator.java
License:Open Source License
private void findTokensToWrap(InfixExpression node, int depth) { Expression left = node.getLeftOperand(); if (left instanceof InfixExpression && samePrecedence(node, (InfixExpression) left)) { findTokensToWrap((InfixExpression) left, depth + 1); } else if (this.wrapIndexes.isEmpty() // always add first operand, it will be taken as wrap parent || !this.options.wrap_before_binary_operator) { this.wrapIndexes.add(this.tm.firstIndexIn(left, -1)); }//from ww w . j a v a 2 s . c om Expression right = node.getRightOperand(); List<Expression> extended = node.extendedOperands(); for (int i = -1; i < extended.size(); i++) { Expression operand = (i == -1) ? right : extended.get(i); if (operand instanceof InfixExpression && samePrecedence(node, (InfixExpression) operand)) { findTokensToWrap((InfixExpression) operand, depth + 1); } int indexBefore = this.tm.firstIndexBefore(operand, -1); while (this.tm.get(indexBefore).isComment()) indexBefore--; assert node.getOperator().toString().equals(this.tm.toString(indexBefore)); int indexAfter = this.tm.firstIndexIn(operand, -1); this.wrapIndexes.add(this.options.wrap_before_binary_operator ? indexBefore : indexAfter); if (!this.options.join_wrapped_lines) { // TODO there should be an option for never joining wraps on opposite side of the operator if (this.options.wrap_before_binary_operator) { if (this.tm.countLineBreaksBetween(this.tm.get(indexAfter - 1), this.tm.get(indexAfter)) > 0) this.wrapIndexes.add(indexAfter); } else { if (this.tm.countLineBreaksBetween(this.tm.get(indexBefore), this.tm.get(indexBefore - 1)) > 0) this.wrapIndexes.add(indexBefore); } } } }
From source file:com.bsiag.eclipse.jdt.java.formatter.SpacePreparator.java
License:Open Source License
@Override public boolean visit(InfixExpression node) { String operator = node.getOperator().toString(); handleOperator(operator, node.getRightOperand(), this.options.insert_space_before_binary_operator, this.options.insert_space_after_binary_operator); List<Expression> extendedOperands = node.extendedOperands(); for (Expression operand : extendedOperands) { handleOperator(operator, operand, this.options.insert_space_before_binary_operator, this.options.insert_space_after_binary_operator); }//from w ww.j a v a2 s . co m return true; }