Search in sources :

Example 91 with AST

use of org.eclipse.jdt.core.dom.AST in project eclipse-pmd by acanda.

the class SuppressWarningsQuickFix method apply.

@Override
protected boolean apply(final ASTNode node) {
    final ASTNode annotatableNode = findAnnotatableASTNode(node);
    if (annotatableNode != null) {
        final AST ast = node.getAST();
        final List<IExtendedModifier> modifiers = getModifiers(annotatableNode);
        final Annotation existingAnnotation = findExistingSuppressWarningsAnnotation(modifiers);
        final Annotation annotation = createReplacementSuppressWarningsAnnotation(existingAnnotation, ast);
        if (existingAnnotation == null) {
            final int position = findPosition(modifiers);
            modifiers.add(position, annotation);
        } else {
            ASTUtil.replace(existingAnnotation, annotation);
        }
        return !annotation.equals(existingAnnotation);
    }
    return false;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Annotation(org.eclipse.jdt.core.dom.Annotation) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 92 with AST

use of org.eclipse.jdt.core.dom.AST in project eclipse-pmd by acanda.

the class MethodReturnsInternalArrayQuickFix method apply.

@Override
protected boolean apply(final ReturnStatement node) {
    final Expression expression = node.getExpression();
    final AST ast = expression.getAST();
    final MethodInvocation replacement = create(ast, MethodInvocation.class);
    replacement.setExpression(copy(expression));
    final SimpleName name = create(ast, SimpleName.class);
    name.setIdentifier("clone");
    replacement.setName(name);
    return replace(expression, replacement);
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 93 with AST

use of org.eclipse.jdt.core.dom.AST in project eclipse-pmd by acanda.

the class UseCollectionIsEmptyQuickFix method apply.

/**
 * 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()}.
 */
@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;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 94 with AST

use of org.eclipse.jdt.core.dom.AST in project eclipse-pmd by acanda.

the class ByteInstantiationValueOfQuickFix method apply.

/**
 * Replaces the Byte instantiation with its argument, e.g. {@code new Byte(123 + x)} with
 * {@code Byte.valueOf(123 + x)}.
 */
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final ClassInstanceCreation node) {
    final AST ast = node.getAST();
    final MethodInvocation invocation = ast.newMethodInvocation();
    invocation.setExpression(ast.newSimpleName("Byte"));
    invocation.setName(ast.newSimpleName("valueOf"));
    invocation.arguments().add(copy((Expression) node.arguments().get(0)));
    replace(node, invocation);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 95 with AST

use of org.eclipse.jdt.core.dom.AST in project eclipse-pmd by acanda.

the class LongInstantiationValueOfQuickFix method apply.

/**
 * Replaces the Long instantiation with its argument, e.g. {@code new Long(123 + x)} with
 * {@code Long.valueOf(123 + x)}.
 */
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final ClassInstanceCreation node) {
    final AST ast = node.getAST();
    final MethodInvocation invocation = ast.newMethodInvocation();
    invocation.setExpression(ast.newSimpleName("Long"));
    invocation.setName(ast.newSimpleName("valueOf"));
    invocation.arguments().add(copy((Expression) node.arguments().get(0)));
    replace(node, invocation);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Aggregations

AST (org.eclipse.jdt.core.dom.AST)169 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)78 Expression (org.eclipse.jdt.core.dom.Expression)77 ASTNode (org.eclipse.jdt.core.dom.ASTNode)70 Type (org.eclipse.jdt.core.dom.Type)52 SimpleName (org.eclipse.jdt.core.dom.SimpleName)51 Block (org.eclipse.jdt.core.dom.Block)44 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)43 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)42 CastExpression (org.eclipse.jdt.core.dom.CastExpression)40 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)40 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)39 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)38 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)38 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)34 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)33 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)33 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)33 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)30 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)30