private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) { if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) { // e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation ... return false; } if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY || locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.MESSAGE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == SwitchStatement.EXPRESSION_PROPERTY || locationInParent == SwitchCase.EXPRESSION_PROPERTY || locationInParent == ArrayAccess.INDEX_PROPERTY || locationInParent == ThrowStatement.EXPRESSION_PROPERTY || locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY || locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) { return false; } return true; }
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; }
@Override public InfixExpression apply(Object object, MethodInvocation methodInvocation) { List arguments = methodInvocation.arguments(); if (arguments.size() == 2) { return astNodeFactory.infixExpression(EQUALS, argumentAsExpression(arguments.get(1)), argumentAsExpression(arguments.get(0))); } if (arguments.size() == 3) { return astNodeFactory.infixExpression(EQUALS, argumentAsExpression(arguments.get(2)), argumentAsExpression(arguments.get(1))); } throw new UnsupportedOperationException("Supported only 2-, 3-arity assertEquals/assertArrayEquals invocation"); }
@Override public InfixExpression apply(Object object, MethodInvocation methodInvocation) { List arguments = methodInvocation.arguments(); if (arguments.size() == 1) { return astNodeFactory.infixExpression(EQUALS, argumentAsExpression(arguments.get(0)), astNodeFactory.nullLiteral()); } if (arguments.size() == 2) { return astNodeFactory.infixExpression(EQUALS, argumentAsExpression(arguments.get(1)), astNodeFactory.nullLiteral()); } throw new UnsupportedOperationException("Supported only 1-, 2-arity assertNull invocation"); }
@Override public InfixExpression apply(Object object, MethodInvocation methodInvocation) { List arguments = methodInvocation.arguments(); if (arguments.size() == 1) { return astNodeFactory.infixExpression(NOT_EQUALS, argumentAsExpression(arguments.get(0)), astNodeFactory.nullLiteral()); } if (arguments.size() == 2) { return astNodeFactory.infixExpression(NOT_EQUALS, argumentAsExpression(arguments.get(1)), astNodeFactory.nullLiteral()); } throw new UnsupportedOperationException("Supported only 1-, 2-arity assertNotNull invocation"); }
@Override public InfixExpression apply(Object object, MethodInvocation verifyMethodInvocation) { List arguments = verifyMethodInvocation.arguments(); return nodeFactory.infixExpression(TIMES, nodeFactory.numberLiteral("0"), nodeFactory.fieldAccess("_", nodeFactory.clone((Expression) arguments.get(0)))); }
@Override public InfixExpression apply(Object object, MethodInvocation verifyMethodInvocation) { List arguments = verifyMethodInvocation.arguments(); MethodInvocation parentMethodInvocation = (MethodInvocation) verifyMethodInvocation.getParent(); return nodeFactory.infixExpression(TIMES, cardinality(arguments.size() == 2 ? Optional.of(arguments.get(1)) : empty()), nodeFactory.methodInvocation(parentMethodInvocation.getName().getFullyQualifiedName(), (List<Expression>) parentMethodInvocation.arguments().stream() .map(matcherHandler::applyMatchers).collect(toList()), nodeFactory.clone((Expression) arguments.get(0)))); }
public boolean visit(InfixExpression exp) { Operator op = exp.getOperator(); if(isCompareOperator(op)) { String leftExp = exp.getLeftOperand().toString(); String rightExp = exp.getRightOperand().toString(); Set<String> incVars = current.getOperations(VariableOperation.Type.INC, VariableOperation.Type.DEC); if(exp.getLeftOperand() instanceof SimpleName && incVars.contains(leftExp)) aux(leftExp, op, exp.getRightOperand()); if(exp.getRightOperand() instanceof SimpleName && incVars.contains(rightExp)) aux(rightExp, op, exp.getLeftOperand()); } return true; }
private static boolean isAcumulationAssign(Assignment assignment, InfixExpression.Operator op, Predicate<Expression> acceptExpression) { if(!( assignment.getRightHandSide() instanceof InfixExpression && assignment.getLeftHandSide() instanceof SimpleName && assignment.getOperator() == Assignment.Operator.ASSIGN)) return false; InfixExpression exp = (InfixExpression) assignment.getRightHandSide(); if(exp.getOperator() != op) return false; String assignVar = assignment.getLeftHandSide().toString(); if( exp.getLeftOperand() instanceof SimpleName && exp.getLeftOperand().toString().equals(assignVar) && acceptExpression.test(exp.getRightOperand())) return true; if( exp.getRightOperand() instanceof SimpleName && exp.getRightOperand().toString().equals(assignVar) && acceptExpression.test(exp.getLeftOperand())) return true; return false; }
@Override public boolean visit(ArrayAccess node) { String arrayName = node.getArray().toString(); List<String> iterators = iteratorsByArray.get(arrayName); if(iterators == null) { iterators= new ArrayList<>(); } String iteratorName = node.getIndex().getNodeType() == ASTNode.INFIX_EXPRESSION ? filterIteratorName((InfixExpression) node.getIndex()) : node.getIndex().toString(); if(!iterators.contains(iteratorName)) { isArrayPrimitiveFigure = true; iterators.add(iteratorName); allIterators.add(iteratorName); iteratorsByArray.put(arrayName, iterators); System.out.println("A variavel " + iteratorName + " est� a iterar sobre a array: " + node.getArray().toString()); } return super.visit(node); }
private static boolean isAllOperandsHaveSameType(InfixExpression expression, ITypeBinding leftOperandType, ITypeBinding rightOperandType) { ITypeBinding binding= leftOperandType; if (binding == null) { return false; } ITypeBinding current= rightOperandType; if (binding != current) { return false; } for (Iterator<Expression> iterator= expression.extendedOperands().iterator(); iterator.hasNext();) { Expression operand= iterator.next(); current= operand.resolveTypeBinding(); if (binding != current) { return false; } } return true; }
private static boolean isAssociative(InfixExpression.Operator operator, ITypeBinding infixExprType, boolean isAllOperandsHaveSameType) { if (operator == InfixExpression.Operator.PLUS) { return isStringType(infixExprType) || isIntegerType(infixExprType) && isAllOperandsHaveSameType; } if (operator == InfixExpression.Operator.TIMES) { return isIntegerType(infixExprType) && isAllOperandsHaveSameType; } if (operator == InfixExpression.Operator.CONDITIONAL_AND || operator == InfixExpression.Operator.CONDITIONAL_OR || operator == InfixExpression.Operator.AND || operator == InfixExpression.Operator.OR || operator == InfixExpression.Operator.XOR) { return true; } return false; }
/** * Returns the type of infix expression based on its operands and operator. * * @param operator the operator of infix expression * @param leftOperandType the type of left operand of infix expression * @param rightOperandType the type of right operand of infix expression * @return the type of infix expression if the type of both the operands is same or if the type * of either operand of a + operator is String, <code>null</code> otherwise. * * @since 3.9 */ private static ITypeBinding getInfixExpressionType(InfixExpression.Operator operator, ITypeBinding leftOperandType, ITypeBinding rightOperandType) { if (leftOperandType == rightOperandType) { return leftOperandType; } if (operator == InfixExpression.Operator.PLUS) { if (isStringType(leftOperandType)) { return leftOperandType; } else if (isStringType(rightOperandType)) { return rightOperandType; } } // If the left and right operand types are different, we assume that parentheses are needed. // This is to avoid complications of numeric promotions and for readability of complicated code. return null; }
private RefactoringStatus rewriteInfixExpression(ASTRewrite astRewrite, ImportRewrite importRewrite, InfixExpression ie, String fullyQualifiedTypeName) { final RefactoringStatus status = new RefactoringStatus(); final AST ast = ie.getAST(); final Expression leftExpCopy = (Expression) ASTNode.copySubtree(ast, ie.getLeftOperand()); final Expression rightExpCopy = (Expression) ASTNode.copySubtree(ast, ie.getRightOperand()); final NumberLiteral zero = ast.newNumberLiteral(); astRewrite.replace(ie.getRightOperand(), zero, null); final MethodInvocation newInvocation = ast.newMethodInvocation(); newInvocation.setExpression(leftExpCopy); newInvocation.setName(ast.newSimpleName("compareTo")); //$NON-NLS-1$ newInvocation.arguments().add(rightExpCopy); astRewrite.replace(ie.getLeftOperand(), newInvocation, null); if (((ASTNode) newInvocation.arguments().get(0)).getNodeType() == ASTNode.SIMPLE_NAME && this.fieldsToRefactor.contains(((SimpleName) ie .getRightOperand()).resolveBinding().getJavaElement())) this.rewriteReference(astRewrite, importRewrite, (SimpleName) newInvocation.arguments().get(0), fullyQualifiedTypeName); if (((ASTNode) newInvocation.getExpression()).getNodeType() == ASTNode.SIMPLE_NAME && this.fieldsToRefactor.contains(((SimpleName) ie .getLeftOperand()).resolveBinding().getJavaElement())) this.rewriteReference(astRewrite, importRewrite, (SimpleName) newInvocation.getExpression(), fullyQualifiedTypeName); return status; }
public static IExpressionFragment createSubPartFragmentBySourceRange( InfixExpression node, ISourceRange range, ICompilationUnit cu) throws JavaModelException { Assert.isNotNull(node); Assert.isNotNull(range); Assert.isTrue(!Util.covers(range, node)); Assert.isTrue(Util.covers(SourceRangeFactory.create(node), range)); if (!isAssociativeInfix(node)) return null; InfixExpression groupRoot = findGroupRoot(node); Assert.isTrue(isAGroupRoot(groupRoot)); List<Expression> groupMembers = AssociativeInfixExpressionFragment.findGroupMembersInOrderFor(groupRoot); List<Expression> subGroup = findSubGroupForSourceRange(groupMembers, range); if (subGroup.isEmpty() || rangeIncludesExtraNonWhitespace(range, subGroup, cu)) return null; return new AssociativeInfixExpressionFragment(groupRoot, subGroup); }
private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) { if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) { // e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation // ... return false; } if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY || locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.MESSAGE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == SwitchStatement.EXPRESSION_PROPERTY || locationInParent == SwitchCase.EXPRESSION_PROPERTY || locationInParent == ArrayAccess.INDEX_PROPERTY || locationInParent == ThrowStatement.EXPRESSION_PROPERTY || locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY || locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) { return false; } return true; }
private static boolean isAllOperandsHaveSameType( InfixExpression expression, ITypeBinding leftOperandType, ITypeBinding rightOperandType) { ITypeBinding binding = leftOperandType; if (binding == null) return false; ITypeBinding current = rightOperandType; if (binding != current) return false; for (Iterator<Expression> iterator = expression.extendedOperands().iterator(); iterator.hasNext(); ) { Expression operand = iterator.next(); current = operand.resolveTypeBinding(); if (binding != current) return false; } return true; }
private static boolean isAssociative( InfixExpression.Operator operator, ITypeBinding infixExprType, boolean isAllOperandsHaveSameType) { if (operator == InfixExpression.Operator.PLUS) return isStringType(infixExprType) || isIntegerType(infixExprType) && isAllOperandsHaveSameType; if (operator == InfixExpression.Operator.TIMES) return isIntegerType(infixExprType) && isAllOperandsHaveSameType; if (operator == InfixExpression.Operator.CONDITIONAL_AND || operator == InfixExpression.Operator.CONDITIONAL_OR || operator == InfixExpression.Operator.AND || operator == InfixExpression.Operator.OR || operator == InfixExpression.Operator.XOR) return true; return false; }
/** * Returns the type of infix expression based on its operands and operator. * * @param operator the operator of infix expression * @param leftOperandType the type of left operand of infix expression * @param rightOperandType the type of right operand of infix expression * @return the type of infix expression if the type of both the operands is same or if the type of * either operand of a + operator is String, <code>null</code> otherwise. * @since 3.9 */ private static ITypeBinding getInfixExpressionType( InfixExpression.Operator operator, ITypeBinding leftOperandType, ITypeBinding rightOperandType) { if (leftOperandType == rightOperandType) { return leftOperandType; } if (operator == InfixExpression.Operator.PLUS) { if (isStringType(leftOperandType)) { return leftOperandType; } else if (isStringType(rightOperandType)) { return rightOperandType; } } // If the left and right operand types are different, we assume that parentheses are needed. // This is to avoid complications of numeric promotions and for readability of complicated code. return null; }
public static ExpressionsFix createAddParanoidalParenthesisFix( CompilationUnit compilationUnit, ASTNode[] coveredNodes) { if (coveredNodes == null) return null; if (coveredNodes.length == 0) return null; // check sub-expressions in fully covered nodes final ArrayList<ASTNode> changedNodes = new ArrayList<ASTNode>(); for (int i = 0; i < coveredNodes.length; i++) { ASTNode covered = coveredNodes[i]; if (covered instanceof InfixExpression) covered.accept(new MissingParenthesisVisitor(changedNodes)); } if (changedNodes.isEmpty() || (changedNodes.size() == 1 && changedNodes.get(0).equals(coveredNodes[0]))) return null; CompilationUnitRewriteOperation op = new AddParenthesisOperation(changedNodes.toArray(new Expression[changedNodes.size()])); return new ExpressionsFix( FixMessages.ExpressionsFix_addParanoiacParentheses_description, compilationUnit, new CompilationUnitRewriteOperation[] {op}); }
public static ExpressionsFix createRemoveUnnecessaryParenthesisFix( CompilationUnit compilationUnit, ASTNode[] nodes) { // check sub-expressions in fully covered nodes final ArrayList<ParenthesizedExpression> changedNodes = new ArrayList<ParenthesizedExpression>(); for (int i = 0; i < nodes.length; i++) { ASTNode covered = nodes[i]; if (covered instanceof ParenthesizedExpression || covered instanceof InfixExpression) covered.accept(new UnnecessaryParenthesisVisitor(changedNodes)); } if (changedNodes.isEmpty()) return null; HashSet<ParenthesizedExpression> expressions = new HashSet<ParenthesizedExpression>(changedNodes); RemoveParenthesisOperation op = new RemoveParenthesisOperation(expressions); return new ExpressionsFix( FixMessages.ExpressionsFix_removeUnnecessaryParentheses_description, compilationUnit, new CompilationUnitRewriteOperation[] {op}); }
/** * Creates an {@link InfixExpression} which is linked to the group of the variableToIncrement. * * @param rewrite the current {@link ASTRewrite} instance * @param variableToIncrement the name of the variable to generate the {@link InfixExpression} for * @param rightHandSide the right hand side expression which shall be included in the {@link * InfixExpression} * @param operator the {@link org.eclipse.jdt.core.dom.InfixExpression.Operator} to use in the * {@link InfixExpression} to create * @return a filled, new {@link InfixExpression} instance */ private InfixExpression getLinkedInfixExpression( ASTRewrite rewrite, String variableToIncrement, Expression rightHandSide, InfixExpression.Operator operator) { AST ast = rewrite.getAST(); InfixExpression loopExpression = ast.newInfixExpression(); SimpleName name = ast.newSimpleName(variableToIncrement); addLinkedPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP, name.getIdentifier()); loopExpression.setLeftOperand(name); loopExpression.setOperator(operator); loopExpression.setRightOperand(rightHandSide); return loopExpression; }
/** * Helper to generate an index based <code>for</code> loop to iterate over a {@link List} * implementation. * * @param ast the current {@link AST} instance to generate the {@link ASTRewrite} for * @return an applicable {@link ASTRewrite} instance */ private ASTRewrite generateIndexBasedForRewrite(AST ast) { ASTRewrite rewrite = ASTRewrite.create(ast); ForStatement loopStatement = ast.newForStatement(); SimpleName loopVariableName = resolveLinkedVariableNameWithProposals(rewrite, "int", null, true); // $NON-NLS-1$ loopStatement.initializers().add(getForInitializer(ast, loopVariableName)); MethodInvocation listSizeExpression = ast.newMethodInvocation(); listSizeExpression.setName(ast.newSimpleName("size")); // $NON-NLS-1$ Expression listExpression = (Expression) rewrite.createCopyTarget(fCurrentExpression); listSizeExpression.setExpression(listExpression); loopStatement.setExpression( getLinkedInfixExpression( rewrite, loopVariableName.getIdentifier(), listSizeExpression, InfixExpression.Operator.LESS)); loopStatement .updaters() .add(getLinkedIncrementExpression(rewrite, loopVariableName.getIdentifier())); Block forLoopBody = ast.newBlock(); forLoopBody .statements() .add(ast.newExpressionStatement(getIndexBasedForBodyAssignment(rewrite, loopVariableName))); forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite)); loopStatement.setBody(forLoopBody); rewrite.replace(fCurrentNode, loopStatement, null); return rewrite; }
private VariableReference retrieveVariableReference(InfixExpression infixExpr, Class<?> exprType) { if (exprType == null) { exprType = retrieveTypeClass(infixExpr); } VariableReference ref = new VariableReferenceImpl(testCase.getReference(), exprType); VariableReference leftOperand = retrieveVariableReference(infixExpr.getLeftOperand(), null); leftOperand.setOriginalCode(infixExpr.getLeftOperand().toString()); Operator operator = Operator.toOperator(infixExpr.getOperator().toString()); VariableReference rightOperand = retrieveVariableReference(infixExpr.getRightOperand(), null); rightOperand.setOriginalCode(infixExpr.getRightOperand().toString()); PrimitiveExpression expr = new PrimitiveExpression(testCase.getReference(), ref, leftOperand, operator, rightOperand); testCase.addStatement(expr); return ref; }
public static IExpressionFragment createSubPartFragmentBySourceRange(InfixExpression node, ISourceRange range, ICompilationUnit cu) throws JavaModelException { Assert.isNotNull(node); Assert.isNotNull(range); Assert.isTrue(!Util.covers(range, node)); Assert.isTrue(Util.covers(SourceRangeFactory.create(node), range)); if(!isAssociativeInfix(node)) return null; InfixExpression groupRoot= findGroupRoot(node); Assert.isTrue(isAGroupRoot(groupRoot)); List<Expression> groupMembers= AssociativeInfixExpressionFragment.findGroupMembersInOrderFor(groupRoot); List<Expression> subGroup= findSubGroupForSourceRange(groupMembers, range); if(subGroup.isEmpty() || rangeIncludesExtraNonWhitespace(range, subGroup, cu)) return null; return new AssociativeInfixExpressionFragment(groupRoot, subGroup); }
private Expression createShiftAssignment(Expression shift1, Expression shift2) { // (int)(element ^ (element >>> 32)); // see implementation in Arrays.hashCode(), Double.hashCode() and // Long.hashCode() CastExpression ce= fAst.newCastExpression(); ce.setType(fAst.newPrimitiveType(PrimitiveType.INT)); InfixExpression unsignedShiftRight= fAst.newInfixExpression(); unsignedShiftRight.setLeftOperand(shift1); unsignedShiftRight.setRightOperand(fAst.newNumberLiteral("32")); //$NON-NLS-1$ unsignedShiftRight.setOperator(Operator.RIGHT_SHIFT_UNSIGNED); InfixExpression xor= fAst.newInfixExpression(); xor.setLeftOperand(shift2); xor.setRightOperand(parenthesize(unsignedShiftRight)); xor.setOperator(InfixExpression.Operator.XOR); ce.setExpression(parenthesize(xor)); return ce; }
@SuppressFBWarnings(value = "PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS", justification = "The extra local variables would make things more confusing.") @Override public boolean visit(InfixExpression node) { if (node.getOperator() == InfixExpression.Operator.EQUALS || node.getOperator() == InfixExpression.Operator.NOT_EQUALS) { Expression left = node.getLeftOperand(); Expression right = node.getRightOperand(); Object rightConst = right.resolveConstantExpressionValue(); Object leftConst = left.resolveConstantExpressionValue(); if (left instanceof MethodInvocation && rightConst instanceof Integer) { if (rightConst.equals(0)) { foundPotentialNewCollection((MethodInvocation) left, node); } } else if (right instanceof MethodInvocation && leftConst instanceof Integer) { if (leftConst.equals(0)) { foundPotentialNewCollection((MethodInvocation) right, node); } } } return true; }
@Override public boolean visit(ConditionalExpression node) { if (expressionToReplace != null) { return false; } if (node.getExpression() instanceof InfixExpression) { InfixExpression condExpr = (InfixExpression) node.getExpression(); boolean retVal = findFirstAndSecondFloat(node, condExpr); if (condExpr.getOperator() == InfixExpression.Operator.GREATER) { return retVal; } else if (condExpr.getOperator() == InfixExpression.Operator.LESS) { swapFirstAndSecondFloat(); return retVal; } } return true; }
private Statement prepareAssignment(Expression rightHand) { // result = PRIME*result + (...) InfixExpression mul= fAst.newInfixExpression(); mul.setLeftOperand(fAst.newSimpleName(VARIABLE_NAME_PRIME)); mul.setRightOperand(fAst.newSimpleName(VARIABLE_NAME_RESULT)); mul.setOperator(Operator.TIMES); Assignment ass= fAst.newAssignment(); ass.setLeftHandSide(fAst.newSimpleName(VARIABLE_NAME_RESULT)); InfixExpression plus= fAst.newInfixExpression(); plus.setLeftOperand(mul); plus.setOperator(Operator.PLUS); plus.setRightOperand(rightHand); ass.setRightHandSide(plus); return fAst.newExpressionStatement(ass); }
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); } }
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)); }
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; }
private static int isOperatorSelected(InfixExpression infixExpression, int offset, int length) { ASTNode left= infixExpression.getLeftOperand(); ASTNode right= infixExpression.getRightOperand(); if (isSelectingOperator(left, right, offset, length)) { return ASTNodes.getExclusiveEnd(left); } List<Expression> extended= infixExpression.extendedOperands(); for (int i= 0; i < extended.size(); i++) { left= right; right= extended.get(i); if (isSelectingOperator(left, right, offset, length)) { return ASTNodes.getExclusiveEnd(left); } } return -1; }
/** * Helper to generate an index based <code>for</code> loop to iterate over an array. * * @param ast the current {@link AST} instance to generate the {@link ASTRewrite} for * @return an applicable {@link ASTRewrite} instance */ private ASTRewrite generateForRewrite(AST ast) { ASTRewrite rewrite= ASTRewrite.create(ast); ForStatement loopStatement= ast.newForStatement(); SimpleName loopVariableName= resolveLinkedVariableNameWithProposals(rewrite, "int", null, true); //$NON-NLS-1$ loopStatement.initializers().add(getForInitializer(ast, loopVariableName)); FieldAccess getArrayLengthExpression= ast.newFieldAccess(); getArrayLengthExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression)); getArrayLengthExpression.setName(ast.newSimpleName("length")); //$NON-NLS-1$ loopStatement.setExpression(getLinkedInfixExpression(rewrite, loopVariableName.getIdentifier(), getArrayLengthExpression, InfixExpression.Operator.LESS)); loopStatement.updaters().add(getLinkedIncrementExpression(rewrite, loopVariableName.getIdentifier())); Block forLoopBody= ast.newBlock(); forLoopBody.statements().add(ast.newExpressionStatement(getForBodyAssignment(rewrite, loopVariableName))); forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite)); loopStatement.setBody(forLoopBody); rewrite.replace(fCurrentNode, loopStatement, null); return rewrite; }
private boolean findFirstAndSecondFloat(ConditionalExpression node, InfixExpression condExpr) { if (!handleTwoSimpleNames(node, condExpr)) { // this is a if diff > 0 case try { if (condExpr.getLeftOperand() instanceof SimpleName) { findDiffAndFloats((SimpleName) condExpr.getLeftOperand()); } else if (condExpr.getRightOperand() instanceof SimpleName) { findDiffAndFloats((SimpleName) condExpr.getRightOperand()); } else { return true; // unexpected comparison } floatOrDouble = getFloatOrDouble(firstFloat, secondFloat); expressionToReplace = node; } catch (CouldntFindDiffException e) { return true; // keep nesting if we have a problem } } return false; }
@Override public boolean visit(InfixExpression node) { if (!(node.getOperator() == InfixExpression.Operator.PLUS && STRING_IDENTIFIER.equals(node.resolveTypeBinding() .getQualifiedName()))) { return true; } this.infixExpression = node; nodesBeingConcatenated.add(node.getLeftOperand()); nodesBeingConcatenated.add(node.getRightOperand()); @SuppressWarnings("unchecked") List<Expression> extendedOperations = node.extendedOperands(); for (Expression expression : extendedOperations) { nodesBeingConcatenated.add(expression); } return false; // prevent traversal to any String Literals }