List of usage examples for org.eclipse.jdt.core.dom AST newPrefixExpression
public PrefixExpression newPrefixExpression()
From source file:com.google.devtools.j2objc.translate.ASTFactory.java
License:Apache License
public static PrefixExpression newPrefixExpression(AST ast, PrefixExpression.Operator op, Expression operand, String type) {/*from ww w. j av a2 s . c o m*/ PrefixExpression expr = ast.newPrefixExpression(); expr.setOperator(op); expr.setOperand(operand); Types.addBinding(expr, ast.resolveWellKnownType(type)); return expr; }
From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.UseEqualsResolution.java
License:Open Source License
protected Expression createNotEqualsExpression(ASTRewrite rewrite, InfixExpression stringEqualityCheck) { Expression equalsExpression = createEqualsExpression(rewrite, stringEqualityCheck); final AST ast = rewrite.getAST(); PrefixExpression prefixExpression = ast.newPrefixExpression(); prefixExpression.setOperator(PrefixExpression.Operator.NOT); prefixExpression.setOperand(equalsExpression); return prefixExpression; }
From source file:net.sf.eclipsecs.ui.quickfixes.coding.SimplifyBooleanReturnQuickfix.java
License:Open Source License
/** * {@inheritDoc}/*from w w w . j ava 2 s . co m*/ */ protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) { return new ASTVisitor() { @Override public boolean visit(final IfStatement node) { if (containsPosition(node, markerStartOffset)) { final Boolean isThenStatementTrue = isReturnStatementTrue(node.getThenStatement()); if (isThenStatementTrue == null) { // the AST structure of the if statement is not as expected return true; } final Expression condition = removeNotFromCondition(node.getExpression()); final boolean isNotCondition = condition != node.getExpression(); final ReturnStatement replacement; if (isThenStatementTrue ^ isNotCondition) { // create replacement: return condition; replacement = node.getAST().newReturnStatement(); replacement.setExpression(copy(condition)); } else { // create replacement: return !(condition); final AST ast = node.getAST(); replacement = ast.newReturnStatement(); final PrefixExpression not = ast.newPrefixExpression(); not.setOperator(Operator.NOT); if (omitParantheses(condition)) { not.setOperand(copy(condition)); } else { final ParenthesizedExpression parentheses = ast.newParenthesizedExpression(); parentheses.setExpression(copy(condition)); not.setOperand(parentheses); } replacement.setExpression(not); } replace(node, replacement); } return true; } private Boolean isReturnStatementTrue(final Statement node) { if (node instanceof ReturnStatement) { final Expression expression = ((ReturnStatement) node).getExpression(); if (expression instanceof BooleanLiteral) { return ((BooleanLiteral) expression).booleanValue(); } } else if (node instanceof Block) { // the return statement might be wrapped in a block statement @SuppressWarnings("unchecked") final List<Statement> statements = ((Block) node).statements(); if (statements.size() > 0) { return isReturnStatementTrue(statements.get(0)); } } return null; } private Expression removeNotFromCondition(final Expression condition) { if (condition instanceof PrefixExpression) { final PrefixExpression prefix = (PrefixExpression) condition; if (PrefixExpression.Operator.NOT.equals(prefix.getOperator())) { return prefix.getOperand(); } } return condition; } private boolean omitParantheses(final Expression condition) { return OMIT_PARANETHESES_CLASSES.contains(condition.getClass()); } }; }
From source file:org.jboss.forge.roaster.model.impl.expressions.UnaryImpl.java
License:Open Source License
@Override public PrefixExpression materialize(AST ast) { if (isMaterialized()) { return expr; }/*w ww .j a v a 2 s. c o m*/ expr = ast.newPrefixExpression(); expr.setOperator(PrefixExpression.Operator.toOperator(op.getOp())); if (arg != null) { expr.setOperand(wireAndGetExpression(arg, this, ast)); } return expr; }
From source file:ptolemy.backtrack.eclipse.ast.transform.AssignmentTransformer.java
License:Open Source License
/** Create the body of an assignment method, which backs up the field * before a new value is assigned to it. * * @param ast The {@link AST} object./* ww w . ja v a2s . co m*/ * @param state The current state of the type analyzer. * @param fieldName The name of the field. * @param fieldType The type of the left-hand side (with <tt>indices</tt> * dimensions less than the original field type). * @param indices The number of indices. * @param special Whether to handle special assign operators. * @return The body of the assignment method. */ private Block _createAssignmentBlock(AST ast, TypeAnalyzerState state, String fieldName, Type fieldType, int indices, boolean special) { Block block = ast.newBlock(); // Test if the checkpoint object is not null. IfStatement ifStatement = ast.newIfStatement(); InfixExpression testExpression = ast.newInfixExpression(); InfixExpression condition1 = ast.newInfixExpression(); condition1.setLeftOperand(ast.newSimpleName(CHECKPOINT_NAME)); condition1.setOperator(InfixExpression.Operator.NOT_EQUALS); condition1.setRightOperand(ast.newNullLiteral()); InfixExpression condition2 = ast.newInfixExpression(); MethodInvocation getTimestamp = ast.newMethodInvocation(); getTimestamp.setExpression(ast.newSimpleName(CHECKPOINT_NAME)); getTimestamp.setName(ast.newSimpleName("getTimestamp")); condition2.setLeftOperand(getTimestamp); condition2.setOperator(InfixExpression.Operator.GREATER); condition2.setRightOperand(ast.newNumberLiteral("0")); testExpression.setLeftOperand(condition1); testExpression.setOperator(InfixExpression.Operator.CONDITIONAL_AND); testExpression.setRightOperand(condition2); ifStatement.setExpression(testExpression); // The "then" branch. Block thenBranch = ast.newBlock(); // Method call to store old value. MethodInvocation recordInvocation = ast.newMethodInvocation(); recordInvocation.setExpression(ast.newSimpleName(_getRecordName(fieldName))); recordInvocation.setName(ast.newSimpleName("add")); // If there are indices, create an integer array of those indices, // and add it as an argument. if (indices == 0) { recordInvocation.arguments().add(ast.newNullLiteral()); } else { ArrayCreation arrayCreation = ast.newArrayCreation(); ArrayType arrayType = ast.newArrayType(ast.newPrimitiveType(PrimitiveType.INT)); ArrayInitializer initializer = ast.newArrayInitializer(); for (int i = 0; i < indices; i++) { initializer.expressions().add(ast.newSimpleName("index" + i)); } arrayCreation.setType(arrayType); arrayCreation.setInitializer(initializer); recordInvocation.arguments().add(arrayCreation); } // If there are indices, add them ("index0", "index1", ...) after the // field. Expression field = ast.newSimpleName(fieldName); if (indices > 0) { for (int i = 0; i < indices; i++) { ArrayAccess arrayAccess = ast.newArrayAccess(); arrayAccess.setArray(field); arrayAccess.setIndex(ast.newSimpleName("index" + i)); field = arrayAccess; } } // Set the field as the next argument. recordInvocation.arguments().add(field); // Get current timestamp from the checkpoint object. MethodInvocation timestampGetter = ast.newMethodInvocation(); timestampGetter.setExpression(ast.newSimpleName(CHECKPOINT_NAME)); timestampGetter.setName(ast.newSimpleName("getTimestamp")); // Set the timestamp as the next argument. recordInvocation.arguments().add(timestampGetter); // The statement of the method call. ExpressionStatement recordStatement = ast.newExpressionStatement(recordInvocation); thenBranch.statements().add(recordStatement); ifStatement.setThenStatement(thenBranch); block.statements().add(ifStatement); // Finally, assign the new value to the field. Assignment assignment = ast.newAssignment(); assignment.setLeftHandSide((Expression) ASTNode.copySubtree(ast, field)); assignment.setRightHandSide(ast.newSimpleName("newValue")); assignment.setOperator(Assignment.Operator.ASSIGN); // Set the checkpoint object of the new value, if necessary. Class c; try { c = fieldType.toClass(state.getClassLoader()); } catch (ClassNotFoundException e) { throw new ASTClassNotFoundException(fieldType.getName()); } if (hasMethod(c, _getSetCheckpointMethodName(false), new Class[] { Checkpoint.class }) || state.getCrossAnalyzedTypes().contains(c.getName())) { block.statements().add(_createSetCheckpointInvocation(ast)); } else { addToLists(_fixSetCheckpoint, c.getName(), block); } // Return the result of the assignment. if (special && _assignOperators.containsKey(fieldType.getName())) { String[] operators = _assignOperators.get(fieldType.getName()); SwitchStatement switchStatement = ast.newSwitchStatement(); switchStatement.setExpression(ast.newSimpleName("operator")); boolean isPostfix = true; for (int i = 0; i < operators.length; i++) { String operator = operators[i]; SwitchCase switchCase = ast.newSwitchCase(); switchCase.setExpression(ast.newNumberLiteral(Integer.toString(i))); switchStatement.statements().add(switchCase); ReturnStatement returnStatement = ast.newReturnStatement(); if (operator.equals("=")) { Assignment newAssignment = (Assignment) ASTNode.copySubtree(ast, assignment); returnStatement.setExpression(newAssignment); } else if (operator.equals("++") || operator.equals("--")) { Expression expression; if (isPostfix) { PostfixExpression postfix = ast.newPostfixExpression(); postfix.setOperand((Expression) ASTNode.copySubtree(ast, assignment.getLeftHandSide())); postfix.setOperator(PostfixExpression.Operator.toOperator(operator)); expression = postfix; // Produce prefix operators next time. if (operator.equals("--")) { isPostfix = false; } } else { PrefixExpression prefix = ast.newPrefixExpression(); prefix.setOperand((Expression) ASTNode.copySubtree(ast, assignment.getLeftHandSide())); prefix.setOperator(PrefixExpression.Operator.toOperator(operator)); expression = prefix; } returnStatement.setExpression(expression); } else { Assignment newAssignment = (Assignment) ASTNode.copySubtree(ast, assignment); newAssignment.setOperator(Assignment.Operator.toOperator(operator)); returnStatement.setExpression(newAssignment); } switchStatement.statements().add(returnStatement); } // The default statement: just return the old value. // This case should not be reached. SwitchCase defaultCase = ast.newSwitchCase(); defaultCase.setExpression(null); switchStatement.statements().add(defaultCase); ReturnStatement defaultReturn = ast.newReturnStatement(); defaultReturn.setExpression((Expression) ASTNode.copySubtree(ast, assignment.getLeftHandSide())); switchStatement.statements().add(defaultReturn); block.statements().add(switchStatement); } else { ReturnStatement returnStatement = ast.newReturnStatement(); returnStatement.setExpression(assignment); block.statements().add(returnStatement); } return block; }