Example usage for org.eclipse.jdt.core.dom AST createInstance

List of usage examples for org.eclipse.jdt.core.dom AST createInstance

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom AST createInstance.

Prototype

public ASTNode createInstance(int nodeType) 

Source Link

Document

Creates an unparented node of the given node type.

Usage

From source file:ch.acanda.eclipse.pmd.java.resolution.ASTUtil.java

License:Open Source License

/**
 * Creates an unparented node of the given node class.
 *
 * @param ast The AST to which the created node will be attached. Must not be {@code null}.
 * @param nodeClass The AST node class. Must not be {@code null}.
 * @return The new unparented node owned by the AST.
 *//*from w  w  w .  j av a2  s  . c  o m*/
@SuppressWarnings("unchecked")
public static <T extends ASTNode> T create(final AST ast, final Class<T> nodeClass) {
    return (T) ast.createInstance(nodeClass);
}

From source file:ch.acanda.eclipse.pmd.java.resolution.design.EqualsNullQuickFix.java

License:Open Source License

/**
 * Replaces {@code x.equals(null)} with {@code x == null}.
 */// ww  w .j  a va2  s . c o  m
@Override
protected boolean apply(final MethodInvocation node) {
    final AST ast = node.getAST();
    final InfixExpression infix = (InfixExpression) ast.createInstance(InfixExpression.class);
    infix.setOperator(Operator.EQUALS);
    infix.setLeftOperand(ASTUtil.copy(node.getExpression()));
    infix.setRightOperand((NullLiteral) ast.createInstance(NullLiteral.class));
    ASTUtil.replace(node, infix);
    return true;
}

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 w w.ja v a 2s . 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.design.UseUtilityClassQuickFix.java

License:Open Source License

@SuppressWarnings("unchecked")
private void addPrivateConstructor(final TypeDeclaration typeDeclaration, final ASTRewrite rewrite) {
    final AST ast = typeDeclaration.getAST();
    final MethodDeclaration constructor = (MethodDeclaration) ast.createInstance(MethodDeclaration.class);
    constructor.setConstructor(true);//  w  w  w.j a v a  2 s  .c o m

    final Modifier modifier = (Modifier) ast.createInstance(Modifier.class);
    modifier.setKeyword(ModifierKeyword.PRIVATE_KEYWORD);
    constructor.modifiers().add(modifier);

    constructor.setName(ASTUtil.copy(typeDeclaration.getName()));

    final Block body = (Block) ast.createInstance(Block.class);
    constructor.setBody(body);

    final ListRewrite statementRewrite = rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
    final ASTNode comment = rewrite.createStringPlaceholder("// hide constructor of utility class",
            ASTNode.EMPTY_STATEMENT);
    statementRewrite.insertFirst(comment, null);

    final int position = findConstructorPosition(typeDeclaration);
    final ListRewrite bodyDeclarationRewrite = rewrite.getListRewrite(typeDeclaration,
            TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
    bodyDeclarationRewrite.insertAt(constructor, position, null);
}

From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java

License:Open Source License

private <T extends Annotation> T createAnnotation(final AST ast, final Class<T> cls) {
    @SuppressWarnings("unchecked")
    final T annotation = (T) ast.createInstance(cls);
    final SimpleName name = (SimpleName) ast.createInstance(SimpleName.class);
    name.setIdentifier("SuppressWarnings");
    annotation.setTypeName(name);//from  ww  w.j  a  v a2s.co m
    return annotation;
}

From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java

License:Open Source License

/**
 * Create the "PMD.<i>RuleName</i>" string literal for the {@code @SuppressWarnings} annotation.
 *///  w  w w  .  j  av  a  2  s . co  m
private StringLiteral createPMDLiteralValue(final AST ast) {
    final StringLiteral newValue = (StringLiteral) ast.createInstance(StringLiteral.class);
    newValue.setLiteralValue("PMD." + marker.getRuleName());
    return newValue;
}

From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java

License:Open Source License

/**
 * Creates the member value pairs of the annotation.
 */// w w w  .  j ava2s.c o m
@SuppressWarnings("unchecked")
private void createAnnotationValues(final NormalAnnotation existingAnnotation,
        final NormalAnnotation annotation) {
    final AST ast = annotation.getAST();
    final List<MemberValuePair> values = annotation.values();
    final List<MemberValuePair> existingValues = existingAnnotation.values();
    for (final MemberValuePair existingPair : existingValues) {
        if ("value".equals(existingPair.getName().getFullyQualifiedName())) {
            final MemberValuePair pair = (MemberValuePair) ast.createInstance(MemberValuePair.class);
            pair.setName(ASTUtil.copy(existingPair.getName()));
            pair.setValue(createArrayInitializer(existingPair.getValue()));
            values.add(pair);
        } else {
            values.add(ASTUtil.copy(existingPair));
        }
    }
}

From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java

License:Open Source License

@SuppressWarnings("unchecked")
private ArrayInitializer createArrayInitializer(final Expression value) {
    final AST ast = value.getAST();
    final ArrayInitializer array;
    if (value instanceof ArrayInitializer) {
        array = createArrayInitializerAndCopyExpressions(ast, (ArrayInitializer) value);

    } else {//from ww w .j a va 2 s  .com
        array = (ArrayInitializer) ast.createInstance(ArrayInitializer.class);
        array.expressions().add(ASTUtil.copy(value));
    }

    array.expressions().add(createPMDLiteralValue(ast));
    return array;
}

From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java

License:Open Source License

@SuppressWarnings("unchecked")
private ArrayInitializer createArrayInitializerAndCopyExpressions(final AST ast,
        final ArrayInitializer existingArray) {
    final ArrayInitializer array;
    array = (ArrayInitializer) ast.createInstance(ArrayInitializer.class);
    final List<Expression> expressions = array.expressions();
    final List<Expression> existingExpressions = existingArray.expressions();
    for (final Expression existingExpression : existingExpressions) {
        expressions.add(ASTUtil.copy(existingExpression));
    }//from  w  ww  .j  ava 2 s  .  c  o  m
    return array;
}

From source file:com.google.currysrc.api.process.ast.AstNodes.java

License:Apache License

public static TagElement createTextTagElement(AST ast, String text) {
    TagElement element = (TagElement) ast.createInstance(TagElement.class);
    TextElement textElement = createTextElement(ast, text);
    element.fragments().add(textElement);
    return element;
}