List of usage examples for org.eclipse.jdt.core.dom InfixExpression getAST
public final AST getAST()
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 ww . j ava 2 s .c om @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)); }// w ww .java2s. co m // "" + '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:com.google.devtools.j2cpp.gen.CppStatementGenerator.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/* w w w . j a v a 2s . co m*/ public boolean visit(InfixExpression node) { InfixExpression.Operator op = node.getOperator(); ITypeBinding type = Types.getTypeBinding(node); if (Types.isJavaStringType(type) && op.equals(InfixExpression.Operator.PLUS)) { printStringConcatenation(node.getLeftOperand(), node.getRightOperand(), node.extendedOperands(), false); } else if (op.equals(InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED)) { printUnsignedRightShift(node.getLeftOperand(), node.getRightOperand()); } else if (op.equals(InfixExpression.Operator.REMAINDER) && isFloatingPoint(node)) { buffer.append(type.isEqualTo(node.getAST().resolveWellKnownType("float")) ? "fmodf" : "fmod"); buffer.append('('); node.getLeftOperand().accept(this); buffer.append(", "); node.getRightOperand().accept(this); buffer.append(')'); } else { node.getLeftOperand().accept(this); buffer.append(' '); buffer.append(node.getOperator().toString()); buffer.append(' '); node.getRightOperand().accept(this); final List<Expression> extendedOperands = node.extendedOperands(); if (extendedOperands.size() != 0) { buffer.append(' '); for (Iterator<Expression> it = extendedOperands.iterator(); it.hasNext();) { buffer.append(node.getOperator().toString()).append(' '); it.next().accept(this); } } } return false; }
From source file:com.google.devtools.j2cpp.types.ImplementationImportCollector.java
License:Open Source License
@Override public boolean visit(InfixExpression node) { if (Types.isJavaStringType(Types.getTypeBinding(node))) { boolean needsImport = false; if (Types.isBooleanType(Types.getTypeBinding(node.getLeftOperand())) || Types.isBooleanType(Types.getTypeBinding(node.getRightOperand()))) { needsImport = true;//from www . j a v a2s. com } else { @SuppressWarnings("unchecked") List<Expression> extendedExpressions = node.extendedOperands(); // Safe by definition for (Expression extendedExpression : extendedExpressions) { if (Types.isBooleanType(Types.getTypeBinding(extendedExpression))) { needsImport = true; break; } } } if (needsImport) { // Implicit conversion from boolean -> String translates into a // Boolean.toString(...) call, so add a reference to java.lang.Boolean. addReference(node.getAST().resolveWellKnownType("java.lang.Boolean")); } } return super.visit(node); }
From source file:net.sf.eclipsecs.ui.quickfixes.coding.StringLiteralEqualityQuickfix.java
License:Open Source License
/** * {@inheritDoc}/* ww w .j a v a 2 s .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); } } }; }