Example usage for org.eclipse.jdt.core.dom SimpleName setIdentifier

List of usage examples for org.eclipse.jdt.core.dom SimpleName setIdentifier

Introduction

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

Prototype

public void setIdentifier(String identifier) 

Source Link

Document

Sets the identifier of this node to the given value.

Usage

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()}.
 *//*  w w w  .j a v a  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.sunsecure.MethodReturnsInternalArrayQuickFix.java

License:Open Source License

@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);/*from   w  ww  . j  a v  a  2  s. com*/
    return replace(expression, replacement);
}

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. c o m
    return annotation;
}

From source file:de.ovgu.cide.export.physical.ahead.JakFeatureRefactorer.java

License:Open Source License

private void replaceParamterAccess(final MethodDeclaration method, final SingleVariableDeclaration parameter,
        FieldDeclaration parameterField) {
    final IVariableBinding oldParameterBinding = parameter.resolveBinding();
    method.getBody().accept(new ASTVisitor() {
        public boolean visit(SimpleName node) {
            if (oldParameterBinding.isEqualTo(node.resolveBinding()))
                node.setIdentifier(getParameterFieldName(method, parameter));
            return true;
        }/*from   w  w w .j a  va2 s .  c  o m*/
    });

}

From source file:org.eclipse.wb.internal.core.utils.ast.AstEditor.java

License:Open Source License

/**
 * Sets new identifier of given {@link SimpleName}.
 *//*from w w w . jav  a 2  s  . c  o m*/
public void setIdentifier(SimpleName simpleName, String newIdentifier) throws Exception {
    replaceSubstring(simpleName, newIdentifier);
    simpleName.setIdentifier(newIdentifier);
}

From source file:org.eclipse.wb.tests.designer.core.util.ast.AstEditorTest.java

License:Open Source License

public void test_replaceSubstring_4() throws Exception {
    TypeDeclaration typeDeclaration = createTypeDeclaration_TestD("// filler filler filler",
            "// filler filler filler", "class Test {", "  int m_value;", "}");
    // prepare field
    FieldDeclaration fieldDeclaration = typeDeclaration.getFields()[0];
    VariableDeclarationFragment declarationFragment = (VariableDeclarationFragment) fieldDeclaration.fragments()
            .get(0);//w w  w. ja  va 2  s .  c o  m
    SimpleName nameNode = declarationFragment.getName();
    // do change and compare
    String newName = "myField";
    m_lastEditor.replaceSubstring(nameNode, newName);
    nameNode.setIdentifier(newName);
    assertEditor(getSourceDQ("// filler filler filler", "// filler filler filler", "class Test {",
            "  int myField;", "}"), m_lastEditor);
}

From source file:org.jboss.forge.roaster.model.impl.PropertyImpl.java

License:Open Source License

@Override
public PropertySource<O> setName(final String name) {
    Assert.isFalse(Strings.isBlank(name), "Property name cannot be null/empty/blank");

    if (hasField()) {
        getField().setName(name);/*from  w  w  w  .  j av  a2  s .  c  o m*/
    }

    final String oldName = this.name;
    final boolean visitDocTags = true;

    final ASTVisitor renameVisitor = new ASTVisitor(visitDocTags) {
        @Override
        public boolean visit(SimpleName node) {
            if (Strings.areEqual(oldName, node.getIdentifier())) {
                node.setIdentifier(name);
            }
            return super.visit(node);
        }

        @Override
        public boolean visit(TextElement node) {
            final String text = node.getText();
            if (!text.contains(oldName)) {
                return super.visit(node);
            }
            final int matchLength = oldName.length();
            final int textLength = text.length();
            final StringBuilder buf = new StringBuilder(text.length());
            final ParsePosition pos = new ParsePosition(0);

            while (pos.getIndex() < textLength) {
                final int index = pos.getIndex();
                final char c = text.charAt(index);
                if (Character.isJavaIdentifierStart(c)) {
                    final int next = index + matchLength;

                    if (next <= textLength && Strings.areEqual(oldName, text.substring(index, next))) {
                        buf.append(name);
                        pos.setIndex(next);
                        continue;
                    }
                }
                buf.append(c);
                pos.setIndex(index + 1);
            }

            node.setText(buf.toString());
            return super.visit(node);
        }
    };

    if (isAccessible()) {
        final MethodSource<O> accessor = getAccessor();
        final String prefix = accessor.getReturnType().isType(boolean.class) ? "is" : "get";
        accessor.setName(methodName(prefix, name));
        ((MethodDeclaration) accessor.getInternal()).accept(renameVisitor);
    }

    if (isMutable()) {
        final MethodSource<O> mutator = getMutator();
        mutator.setName(methodName("set", name));
        ((MethodDeclaration) mutator.getInternal()).accept(renameVisitor);
    }

    this.name = name;

    return this;
}