List of usage examples for org.eclipse.jdt.core.dom PrefixExpression setOperand
public void setOperand(Expression expression)
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()}. *///ww w .j av a 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:com.google.devtools.j2objc.translate.ASTFactory.java
License:Apache License
public static PrefixExpression newPrefixExpression(AST ast, PrefixExpression.Operator op, Expression operand, String type) {/*from www . j a v a 2 s . co 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:java5totext.input.JDTVisitor.java
License:Open Source License
@Override public void endVisit(org.eclipse.jdt.core.dom.PrefixExpression node) { PrefixExpression element = (PrefixExpression) this.binding.get(node); this.initializeNode(element, node); element.setOperator(node.getOperator().toString()); if (this.binding.get(node.getOperand()) != null) element.setOperand((Expression) this.binding.get(node.getOperand())); }
From source file:net.sf.eclipsecs.ui.quickfixes.coding.SimplifyBooleanReturnQuickfix.java
License:Open Source License
/** * {@inheritDoc}/* w ww .jav a 2s . c o 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:net.sf.eclipsecs.ui.quickfixes.coding.StringLiteralEqualityQuickfix.java
License:Open Source License
/** * {@inheritDoc}// ww w . j a va 2s . c o m */ protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartPosition) { return new ASTVisitor() { public boolean visit(InfixExpression node) { if (containsPosition(lineInfo, node.getStartPosition())) { StringLiteral literal = null; Expression otherOperand = null; if (node.getLeftOperand() instanceof StringLiteral) { literal = (StringLiteral) node.getLeftOperand(); otherOperand = node.getRightOperand(); } else if (node.getRightOperand() instanceof StringLiteral) { literal = (StringLiteral) node.getRightOperand(); otherOperand = node.getLeftOperand(); } else { return true; } Expression replacementNode = null; MethodInvocation equalsInvocation = node.getAST().newMethodInvocation(); equalsInvocation.setName(node.getAST().newSimpleName("equals")); //$NON-NLS-1$ equalsInvocation.setExpression((Expression) ASTNode.copySubtree(node.getAST(), literal)); equalsInvocation.arguments().add(ASTNode.copySubtree(node.getAST(), otherOperand)); // if the string was compared with != create a not // expression if (node.getOperator().equals(InfixExpression.Operator.NOT_EQUALS)) { PrefixExpression prefixExpression = node.getAST().newPrefixExpression(); prefixExpression.setOperator(PrefixExpression.Operator.NOT); prefixExpression.setOperand(equalsInvocation); replacementNode = prefixExpression; } else { replacementNode = equalsInvocation; } replaceNode(node, replacementNode); } return true; } /** * Replaces the given node with the replacement node (using * reflection since I am not aware of a proper API to do this). * * @param node * the node to replace * @param replacementNode * the replacement */ private void replaceNode(ASTNode node, ASTNode replacementNode) { try { if (node.getLocationInParent().isChildProperty()) { String property = node.getLocationInParent().getId(); String setterMethodName = "set" + StringUtils.capitalize(property); Class testClass = node.getClass(); while (testClass != null) { try { Method setterMethod = node.getParent().getClass().getMethod(setterMethodName, testClass); setterMethod.invoke(node.getParent(), replacementNode); break; } catch (NoSuchMethodException e) { testClass = testClass.getSuperclass(); } } } else if (node.getLocationInParent().isChildListProperty()) { Method listMethod = node.getParent().getClass() .getMethod(node.getLocationInParent().getId(), (Class<?>[]) null); List list = (List) listMethod.invoke(node.getParent(), (Object[]) null); list.set(list.indexOf(node), replacementNode); } } catch (InvocationTargetException e) { CheckstyleLog.log(e); } catch (IllegalAccessException e) { CheckstyleLog.log(e); } catch (NoSuchMethodException e) { CheckstyleLog.log(e); } } }; }
From source file:org.autorefactor.refactoring.ASTBuilder.java
License:Open Source License
private Expression prefixExpr(PrefixExpression.Operator operator, Expression operand) { final PrefixExpression pe = ast.newPrefixExpression(); pe.setOperator(operator);/*w w w. j a v a 2 s . co m*/ pe.setOperand(operand); return pe; }
From source file:org.autorefactor.refactoring.rules.WakeLockRefactoring.java
License:Open Source License
private Statement createWakelockReleaseNode(MethodInvocation methodInvocation) { final ASTBuilder b = this.ctx.getASTBuilder(); IfStatement ifStatement = b.getAST().newIfStatement(); // test-clause PrefixExpression prefixExpression = b.getAST().newPrefixExpression(); prefixExpression.setOperator(PrefixExpression.Operator.NOT); MethodInvocation isHeldInvocation = b.getAST().newMethodInvocation(); isHeldInvocation.setName(b.simpleName("isHeld")); isHeldInvocation.setExpression(b.copyExpression(methodInvocation)); prefixExpression.setOperand(isHeldInvocation); ifStatement.setExpression(prefixExpression); // then/*from www. j a va 2 s . c o m*/ MethodInvocation releaseInvocation = b.getAST().newMethodInvocation(); releaseInvocation.setName(b.simpleName("release")); releaseInvocation.setExpression(b.copyExpression(methodInvocation)); ExpressionStatement releaseExpressionStatement = b.getAST().newExpressionStatement(releaseInvocation); ifStatement.setThenStatement(b.block(releaseExpressionStatement)); return ifStatement; }
From source file:org.decojer.cavaj.transformers.TrExpressions.java
License:Open Source License
/** * Handle pre increments / decrements via LOAD & LOAD/DUP -> ADD/SUB -> STORE. * * @param bb//from w w w .j a v a2 s . co m * BB * @param assignment * assignment * @return {@code true} - is rewritten pre increment / decrement */ private boolean rewriteStorePreIncDec(@Nonnull final BB bb, @Nonnull final Assignment assignment) { if (bb.isStackEmpty()) { return false; } final Expression leftHandSide = assignment.getLeftHandSide(); final Expression peek = bb.peek(); if (!leftHandSide.subtreeMatch(new ASTMatcher(), peek)) { return false; } Expression expression = assignment.getRightHandSide(); if (expression instanceof CastExpression) { expression = ((CastExpression) expression).getExpression(); } if (expression instanceof ParenthesizedExpression) { expression = ((ParenthesizedExpression) expression).getExpression(); } if (!(expression instanceof InfixExpression)) { return false; } final InfixExpression infixExpression = (InfixExpression) expression; if (!infixExpression.getLeftOperand().subtreeMatch(new ASTMatcher(), peek)) { return false; } if (!(infixExpression.getRightOperand() instanceof NumberLiteral)) { return false; } if (!((NumberLiteral) infixExpression.getRightOperand()).getToken().equals("1")) { return false; } final InfixExpression.Operator operator = infixExpression.getOperator(); if (operator == InfixExpression.Operator.MINUS || operator == InfixExpression.Operator.PLUS) { final PrefixExpression prefixExpression = getAst().newPrefixExpression(); prefixExpression .setOperator(operator == InfixExpression.Operator.MINUS ? PrefixExpression.Operator.DECREMENT : PrefixExpression.Operator.INCREMENT); prefixExpression.setOperand(wrap(bb.pop(), Priority.PREFIX_OR_POSTFIX)); bb.push(prefixExpression); return true; } return false; }
From source file:org.decojer.cavaj.utils.Expressions.java
License:Open Source License
/** * New prefix expression./*w w w . java 2 s . c om*/ * * @param operator * prefix expression operator * @param operand * operand expression * @param op * originating operation * @return expression */ @Nonnull public static Expression newPrefixExpression(@Nullable final PrefixExpression.Operator operator, @Nonnull final Expression operand, @Nonnull final Op op) { if (operator == PrefixExpression.Operator.NOT) { return not(operand); } final PrefixExpression prefixExpression = setOp(operand.getAST().newPrefixExpression(), op); prefixExpression.setOperator(operator); prefixExpression.setOperand(wrap(operand, Priority.priority(prefixExpression))); return prefixExpression; }