@Override public boolean visit(PrefixExpression exp) { if(exp.getOperand() instanceof SimpleName) { String varName = exp.getOperand().toString(); VariableOperation op = null; if(exp.getOperator() == PrefixExpression.Operator.INCREMENT) op = new VariableOperation(varName, VariableOperation.Type.INC); else if(exp.getOperator() == PrefixExpression.Operator.DECREMENT) op = new VariableOperation(varName, VariableOperation.Type.DEC); if(op != null) current.addOperation(op); } return true; }
/** * @param expression */ public void analyze(Expression expression) { // param == null, null == param if (expression.getNodeType() == ASTNode.NULL_LITERAL) { setNullLiteral((NullLiteral) expression); } else if (expression.getNodeType() == ASTNode.SIMPLE_NAME) { setSimpleName((SimpleName) expression); } else if (expression.getNodeType() == ASTNode.NUMBER_LITERAL) { setNumberLiteral((NumberLiteral) expression); } else if (expression.getNodeType() == ASTNode.PREFIX_EXPRESSION) { setPrefixExpression((PrefixExpression) expression); } else if (expression.getNodeType() == ASTNode.POSTFIX_EXPRESSION) { setPostfixExpression((PostfixExpression) expression); } else if (expression.getNodeType() == ASTNode.PARENTHESIZED_EXPRESSION || expression.getNodeType() == ASTNode.INFIX_EXPRESSION || expression.getNodeType() == ASTNode.METHOD_INVOCATION) { // addTestCasesToMerge(processIfExpressions(expression, // tmlMethod)); } else { // TODO System.out.println("Expression could not be analyzed: " + expression.getNodeType() + ". Expression: " + expression.toString()); } }
protected String getNumber(PrefixExpression prefixExpression) { String number = null; Expression operand = prefixExpression.getOperand(); if (operand.getNodeType() == ASTNode.NUMBER_LITERAL) { org.eclipse.jdt.core.dom.PrefixExpression.Operator operator = prefixExpression .getOperator(); if (org.eclipse.jdt.core.dom.PrefixExpression.Operator.MINUS .equals(operator)) { number = "-" + operand.toString(); } else if (org.eclipse.jdt.core.dom.PrefixExpression.Operator.PLUS .equals(operator) || org.eclipse.jdt.core.dom.PrefixExpression.Operator.DECREMENT .equals(operator) || org.eclipse.jdt.core.dom.PrefixExpression.Operator.INCREMENT .equals(operator)) { number = operand.toString(); } else { number = "0"; } } return number; }
@Override public boolean visit(PrefixExpression node) { Expression operand = node.getOperand(); if (!considerBinding(resolveBinding(operand), operand)) return true; PrefixExpression.Operator operator = node.getOperator(); if (operator != PrefixExpression.Operator.INCREMENT && operator != PrefixExpression.Operator.DECREMENT) return true; checkParent(node); fRewriter.replace( node, createInvocation(node.getAST(), node.getOperand(), node.getOperator().toString()), createGroupDescription(PREFIX_ACCESS)); return false; }
@Override public boolean visit(PrefixExpression prefixExpression) { if (prefixExpression.getOperand() == null) return true; if (! (prefixExpression.getOperand() instanceof SimpleName)) return true; if (! prefixExpression.getOperator().equals(Operator.DECREMENT) && ! prefixExpression.getOperator().equals(Operator.INCREMENT)) return true; SimpleName simpleName= (SimpleName)prefixExpression.getOperand(); if (! isNameReferenceToTemp(simpleName)) return true; fFirstAssignment= prefixExpression; return false; }
private static boolean isLeftValue(ASTNode node) { ASTNode parent= node.getParent(); if (parent instanceof Assignment) { Assignment assignment= (Assignment) parent; if (assignment.getLeftHandSide() == node) return true; } if (parent instanceof PostfixExpression) return true; if (parent instanceof PrefixExpression) { PrefixExpression.Operator op= ((PrefixExpression) parent).getOperator(); if (op.equals(PrefixExpression.Operator.DECREMENT)) return true; if (op.equals(PrefixExpression.Operator.INCREMENT)) return true; return false; } return false; }
@Override public boolean visit(PrefixExpression node) { Expression operand= node.getOperand(); if (!considerBinding(resolveBinding(operand), operand)) return true; PrefixExpression.Operator operator= node.getOperator(); if (operator != PrefixExpression.Operator.INCREMENT && operator != PrefixExpression.Operator.DECREMENT) return true; checkParent(node); fRewriter.replace(node, createInvocation(node.getAST(), node.getOperand(), node.getOperator().toString()), createGroupDescription(PREFIX_ACCESS)); return false; }
private Statement createOuterComparison() { MethodInvocation outer1= fAst.newMethodInvocation(); outer1.setName(fAst.newSimpleName(METHODNAME_OUTER_TYPE)); MethodInvocation outer2= fAst.newMethodInvocation(); outer2.setName(fAst.newSimpleName(METHODNAME_OUTER_TYPE)); outer2.setExpression(fAst.newSimpleName(VARIABLE_NAME_EQUALS_CASTED)); MethodInvocation outerEql= fAst.newMethodInvocation(); outerEql.setName(fAst.newSimpleName(METHODNAME_EQUALS)); outerEql.setExpression(outer1); outerEql.arguments().add(outer2); PrefixExpression not= fAst.newPrefixExpression(); not.setOperand(outerEql); not.setOperator(PrefixExpression.Operator.NOT); IfStatement notEqNull= fAst.newIfStatement(); notEqNull.setExpression(not); notEqNull.setThenStatement(getThenStatement(getReturnFalse())); return notEqNull; }
private Statement createArrayComparison(String name) { MethodInvocation invoc= fAst.newMethodInvocation(); invoc.setName(fAst.newSimpleName(METHODNAME_EQUALS)); invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS)); invoc.arguments().add(getThisAccessForEquals(name)); invoc.arguments().add(getOtherAccess(name)); PrefixExpression pe= fAst.newPrefixExpression(); pe.setOperator(PrefixExpression.Operator.NOT); pe.setOperand(invoc); IfStatement ifSt= fAst.newIfStatement(); ifSt.setExpression(pe); ifSt.setThenStatement(getThenStatement(getReturnFalse())); return ifSt; }
private Statement createMultiArrayComparison(String name) { MethodInvocation invoc= fAst.newMethodInvocation(); invoc.setName(fAst.newSimpleName(METHODNAME_DEEP_EQUALS)); invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS)); invoc.arguments().add(getThisAccessForEquals(name)); invoc.arguments().add(getOtherAccess(name)); PrefixExpression pe= fAst.newPrefixExpression(); pe.setOperator(PrefixExpression.Operator.NOT); pe.setOperand(invoc); IfStatement ifSt= fAst.newIfStatement(); ifSt.setExpression(pe); ifSt.setThenStatement(getThenStatement(getReturnFalse())); return ifSt; }
@SuppressWarnings("unchecked") private Expression makeFixedExpression(ASTRewrite rewrite, IsNANVisitor visitor) { AST ast = rewrite.getAST(); MethodInvocation fixedMethod = ast.newMethodInvocation(); fixedMethod.setName(ast.newSimpleName("isNaN")); if (visitor.isPrimitive) { // make a reference to Double or Float SimpleName staticType = ast.newSimpleName(doubleOrFloat(visitor.isDouble)); fixedMethod.setExpression(staticType); fixedMethod.arguments().add(rewrite.createMoveTarget(visitor.testedVariable)); } else { // call isNaN directly on the boxed variable fixedMethod.setExpression((Expression) rewrite.createMoveTarget(visitor.testedVariable)); } if (!visitor.isEquals) { PrefixExpression not = ast.newPrefixExpression(); not.setOperator(PrefixExpression.Operator.NOT); not.setOperand(fixedMethod); return not; } return fixedMethod; }
private Expression makeCallToIsEmpty(ASTRewrite rewrite, ResolutionBundle bundle) { AST ast = rewrite.getAST(); MethodInvocation callToEmpty = ast.newMethodInvocation(); callToEmpty.setName(ast.newSimpleName("isEmpty")); callToEmpty.setExpression((Expression) rewrite.createCopyTarget(bundle.collectionToFix)); if (bundle.wasNegated) { PrefixExpression negatedExpression = ast.newPrefixExpression(); negatedExpression.setOperator(PrefixExpression.Operator.NOT); negatedExpression.setOperand(callToEmpty); return negatedExpression; } return callToEmpty; }
@Override public DSubTree handle() { DSubTree sub = new DOMExpression(expression.getOperand()).handle(); if (expression.getOperator() == PrefixExpression.Operator.NOT && sub.getNodes().size() == 1) { // encode the ! predicate into the API call name DAPICall call = (DAPICall) sub.getNodes().get(0); call.setNotPredicate(); } return sub; }
@Override public boolean visit(PrefixExpression node) { Object operator= node.getOperator(); if (operator == PrefixExpression.Operator.INCREMENT || operator == PrefixExpression.Operator.DECREMENT) { fSideEffectNodes.add(node); return false; } return true; }
@Override public void endVisit(PrefixExpression node) { PrefixExpression.Operator op = node.getOperator(); if (PrefixExpression.Operator.INCREMENT.equals(op) || PrefixExpression.Operator.DECREMENT.equals(op)) { endVisitIncDecOperation(node, node.getOperand()); } else { assignFlowInfo(node, node.getOperand()); } }
/** * Returns the precedence of the expression. Expression * with higher precedence are executed before expressions * with lower precedence. * i.e. in: * <br><code> int a= ++3--;</code></br> * * the precedence order is * <ul> * <li>3</li> * <li>++</li> * <li>--</li> * <li>=</li> * </ul> * 1. 3 -(++)-> 4<br> * 2. 4 -(--)-> 3<br> * 3. 3 -(=)-> a<br> * * @param expression the expression to determine the precedence for * @return the precedence the higher to stronger the binding to its operand(s) */ public static int getExpressionPrecedence(Expression expression) { if (expression instanceof InfixExpression) { return getOperatorPrecedence(((InfixExpression)expression).getOperator()); } else if (expression instanceof Assignment) { return ASSIGNMENT; } else if (expression instanceof ConditionalExpression) { return CONDITIONAL; } else if (expression instanceof InstanceofExpression) { return RELATIONAL; } else if (expression instanceof CastExpression) { return TYPEGENERATION; } else if (expression instanceof ClassInstanceCreation) { return POSTFIX; } else if (expression instanceof PrefixExpression) { return PREFIX; } else if (expression instanceof FieldAccess) { return POSTFIX; } else if (expression instanceof MethodInvocation) { return POSTFIX; } else if (expression instanceof ArrayAccess) { return POSTFIX; } else if (expression instanceof PostfixExpression) { return POSTFIX; } return Integer.MAX_VALUE; }
/** * @param expression */ public void setPrefixExpression(PrefixExpression expression) { this.prefixExpressions.add(expression); Expression operand = expression.getOperand(); expressionPrefixes.put(operand, expression.getOperator()); analyze(operand); }
@Override public boolean visit(PrefixExpression node) { Object operator = node.getOperator(); if (operator == PrefixExpression.Operator.INCREMENT || operator == PrefixExpression.Operator.DECREMENT) { fSideEffectNodes.add(node); return false; } return true; }
@Override public boolean visit(PrefixExpression prefixExpression) { if (prefixExpression.getOperand() == null) return true; if (!(prefixExpression.getOperand() instanceof SimpleName)) return true; if (!prefixExpression.getOperator().equals(Operator.DECREMENT) && !prefixExpression.getOperator().equals(Operator.INCREMENT)) return true; SimpleName simpleName = (SimpleName) prefixExpression.getOperand(); if (!isNameReferenceToTemp(simpleName)) return true; fFirstAssignment = prefixExpression; return false; }
private static boolean isLeftValue(ASTNode node) { ASTNode parent = node.getParent(); if (parent instanceof Assignment) { Assignment assignment = (Assignment) parent; if (assignment.getLeftHandSide() == node) return true; } if (parent instanceof PostfixExpression) return true; if (parent instanceof PrefixExpression) { PrefixExpression.Operator op = ((PrefixExpression) parent).getOperator(); if (op.equals(PrefixExpression.Operator.DECREMENT)) return true; if (op.equals(PrefixExpression.Operator.INCREMENT)) return true; return false; } return false; }
/** * Returns the precedence of the expression. Expression with higher precedence are executed before * expressions with lower precedence. i.e. in: <br> * <code> int a= ++3--;</code></br> * * <p>the precedence order is * * <ul> * <li>3 * <li>++ * <li>-- * <li>= * </ul> * * 1. 3 -(++)-> 4<br> * 2. 4 -(--)-> 3<br> * 3. 3 -(=)-> a<br> * * @param expression the expression to determine the precedence for * @return the precedence the higher to stronger the binding to its operand(s) */ public static int getExpressionPrecedence(Expression expression) { if (expression instanceof InfixExpression) { return getOperatorPrecedence(((InfixExpression) expression).getOperator()); } else if (expression instanceof Assignment) { return ASSIGNMENT; } else if (expression instanceof ConditionalExpression) { return CONDITIONAL; } else if (expression instanceof InstanceofExpression) { return RELATIONAL; } else if (expression instanceof CastExpression) { return TYPEGENERATION; } else if (expression instanceof ClassInstanceCreation) { return POSTFIX; } else if (expression instanceof PrefixExpression) { return PREFIX; } else if (expression instanceof FieldAccess) { return POSTFIX; } else if (expression instanceof MethodInvocation) { return POSTFIX; } else if (expression instanceof ArrayAccess) { return POSTFIX; } else if (expression instanceof PostfixExpression) { return POSTFIX; } return Integer.MAX_VALUE; }
@Override public boolean visit(PrefixExpression node) { Object operator = node.getOperator(); if (operator == PrefixExpression.Operator.INCREMENT || operator == PrefixExpression.Operator.DECREMENT) { fSideEffectNodes.add(node); } return false; }
private VariableReference retrieveVariableReference(PrefixExpression prefixExpr) { if (prefixExpr.getOperator() == org.eclipse.jdt.core.dom.PrefixExpression.Operator.MINUS) { Class<?> numberClass = retrieveTypeClass(prefixExpr.getOperand()); return retrieveVariableReference(prefixExpr, numberClass); } throw new UnsupportedOperationException("Prefix " + prefixExpr + " not implemented!"); }
private VariableReference retrieveVariableReference(PrefixExpression prefixExpr, Class<?> numberClass) { if (prefixExpr.getOperator() == org.eclipse.jdt.core.dom.PrefixExpression.Operator.MINUS) { NumberLiteral numberLiteral = (NumberLiteral) prefixExpr.getOperand(); Number value = (Number) numberLiteral.resolveConstantExpressionValue(); value = negate(value); PrimitiveStatement<?> numberAssignment = createPrimitiveStatement(numberClass, value); testCase.addStatement(numberAssignment); return numberAssignment.getReturnValue(); } throw new UnsupportedOperationException("Prefix " + prefixExpr + " not implemented!"); }
@Override public final boolean visit(final PrefixExpression node) { final IVariableBinding binding= getFieldBinding(node.getOperand()); if (binding != null) fWritten.add(binding.getKey()); return false; }
@Override public void endVisit(PrefixExpression node) { PrefixExpression.Operator op= node.getOperator(); if (PrefixExpression.Operator.INCREMENT.equals(op) || PrefixExpression.Operator.DECREMENT.equals(op)) { endVisitIncDecOperation(node, node.getOperand()); } else { assignFlowInfo(node, node.getOperand()); } }
@Override public boolean visit(PrefixExpression node) { PrefixExpression.Operator operator= node.getOperator(); if (operator == Operator.INCREMENT || operator == Operator.DECREMENT) { SimpleName name= getSimpleName(node.getOperand()); if (name != null) addWrite(name, name.resolveBinding()); } return true; }
public static boolean isWriteAccess(Name selectedNode) { ASTNode curr= selectedNode; ASTNode parent= curr.getParent(); while (parent != null) { switch (parent.getNodeType()) { case ASTNode.QUALIFIED_NAME: if (((QualifiedName) parent).getQualifier() == curr) { return false; } break; case ASTNode.FIELD_ACCESS: if (((FieldAccess) parent).getExpression() == curr) { return false; } break; case ASTNode.SUPER_FIELD_ACCESS: break; case ASTNode.ASSIGNMENT: return ((Assignment) parent).getLeftHandSide() == curr; case ASTNode.VARIABLE_DECLARATION_FRAGMENT: case ASTNode.SINGLE_VARIABLE_DECLARATION: return ((VariableDeclaration) parent).getName() == curr; case ASTNode.POSTFIX_EXPRESSION: return true; case ASTNode.PREFIX_EXPRESSION: PrefixExpression.Operator op= ((PrefixExpression) parent).getOperator(); return op == PrefixExpression.Operator.DECREMENT || op == PrefixExpression.Operator.INCREMENT; default: return false; } curr= parent; parent= curr.getParent(); } return false; }
private static Expression getInversedNotExpression(ASTRewrite rewrite, Expression expression, AST ast) { PrefixExpression prefixExpression= ast.newPrefixExpression(); prefixExpression.setOperator(PrefixExpression.Operator.NOT); ParenthesizedExpression parenthesizedExpression= getParenthesizedExpression(ast, (Expression)rewrite.createCopyTarget(expression)); prefixExpression.setOperand(parenthesizedExpression); return prefixExpression; }
private static boolean isNegated(Expression expression) { if (!(expression.getParent() instanceof ParenthesizedExpression)) return false; ParenthesizedExpression parenthesis= (ParenthesizedExpression) expression.getParent(); if (!(parenthesis.getParent() instanceof PrefixExpression)) return false; PrefixExpression prefix= (PrefixExpression) parenthesis.getParent(); if (!(prefix.getOperator() == PrefixExpression.Operator.NOT)) return false; return true; }
private static Expression getBooleanExpression(ASTNode node) { if (!(node instanceof Expression)) { return null; } // check if the node is a location where it can be negated StructuralPropertyDescriptor locationInParent= node.getLocationInParent(); if (locationInParent == QualifiedName.NAME_PROPERTY) { node= node.getParent(); locationInParent= node.getLocationInParent(); } while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) { node= node.getParent(); locationInParent= node.getLocationInParent(); } Expression expression= (Expression) node; if (!isBoolean(expression)) { return null; } if (expression.getParent() instanceof InfixExpression) { return expression; } if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY || locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY || locationInParent == MethodInvocation.ARGUMENTS_PROPERTY || locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY || locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY || locationInParent == ConditionalExpression.EXPRESSION_PROPERTY || locationInParent == PrefixExpression.OPERAND_PROPERTY) { return expression; } return null; }
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; }