Example usage for org.eclipse.jdt.core.dom MethodInvocation setExpression

List of usage examples for org.eclipse.jdt.core.dom MethodInvocation setExpression

Introduction

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

Prototype

public void setExpression(Expression expression) 

Source Link

Document

Sets or clears the expression of this method invocation expression.

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  o m
@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.migration.ByteInstantiationValueOfQuickFix.java

License:Open Source License

/**
 * Replaces the Byte instantiation with its argument, e.g. {@code new Byte(123 + x)} with
 * {@code Byte.valueOf(123 + x)}.//from   www.j  av  a 2 s  .c o  m
 */
@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;
}

From source file:ch.acanda.eclipse.pmd.java.resolution.migration.IntegerInstantiationValueOfQuickFix.java

License:Open Source License

/**
 * Replaces the Integer instantiation with its argument, e.g. {@code new Integer(123 + x)} with
 * {@code Integer.valueOf(123 + x)}./*ww w . j  a  v  a  2 s .  com*/
 */
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final ClassInstanceCreation node) {
    final AST ast = node.getAST();
    final MethodInvocation invocation = ast.newMethodInvocation();
    invocation.setExpression(ast.newSimpleName("Integer"));
    invocation.setName(ast.newSimpleName("valueOf"));
    invocation.arguments().add(copy((Expression) node.arguments().get(0)));
    replace(node, invocation);
    return true;
}

From source file:ch.acanda.eclipse.pmd.java.resolution.migration.LongInstantiationValueOfQuickFix.java

License:Open Source License

/**
 * Replaces the Long instantiation with its argument, e.g. {@code new Long(123 + x)} with
 * {@code Long.valueOf(123 + x)}./*from  ww  w . ja  v a 2  s .  c  o  m*/
 */
@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;
}

From source file:ch.acanda.eclipse.pmd.java.resolution.migration.ShortInstantiationValueOfQuickFix.java

License:Open Source License

/**
 * Replaces the Short instantiation with its argument, e.g. {@code new Short(123 + x)} with
 * {@code Short.valueOf(123 + x)}.//from w  w  w  . j  a va  2  s .  co m
 */
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final ClassInstanceCreation node) {
    final AST ast = node.getAST();
    final MethodInvocation invocation = ast.newMethodInvocation();
    invocation.setExpression(ast.newSimpleName("Short"));
    invocation.setName(ast.newSimpleName("valueOf"));
    invocation.arguments().add(copy((Expression) node.arguments().get(0)));
    replace(node, invocation);
    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));
    }/*from  ww w .ja v  a 2  s.c  o 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:ch.acanda.eclipse.pmd.java.resolution.optimization.SimplifyStartsWithQuickFix.java

License:Open Source License

/**
 * Rewrites <code>s.startsWith("a")</code> as <code>s.charAt(0) == 'a'</code>.
 *///from w  w w  . j ava  2s. c o  m
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final MethodInvocation node) {
    final AST ast = node.getAST();

    final MethodInvocation charAt = ast.newMethodInvocation();
    charAt.setExpression(copy(node.getExpression()));
    charAt.setName(ast.newSimpleName("charAt"));
    charAt.arguments().add(ast.newNumberLiteral("0"));

    final CharacterLiteral character = ast.newCharacterLiteral();
    final StringLiteral s = (StringLiteral) node.arguments().get(0);
    character.setEscapedValue(s.getEscapedValue().replace('"', '\''));

    final InfixExpression eq = ast.newInfixExpression();
    eq.setOperator(Operator.EQUALS);
    eq.setLeftOperand(charAt);
    eq.setRightOperand(character);

    replace(node, eq);
    return true;
}

From source file:ch.acanda.eclipse.pmd.java.resolution.stringandstringbuffer.UnnecessaryCaseChangeQuickFix.java

License:Open Source License

/**
 * Removes the <code>.toString()</code> from <code>"foo".toString()</code> if the expression is only a part of an
 * statement. Removes the expression completely if it is the whole statement.
 *//* w ww.j a v  a 2  s .  c om*/
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final MethodInvocation node) {
    final AST ast = node.getAST();
    final MethodInvocation invocation = ast.newMethodInvocation();
    if (node.getExpression().getNodeType() == ASTNode.METHOD_INVOCATION) {
        invocation.setExpression(removeCaseChange((MethodInvocation) node.getExpression()));
        invocation.setName(ast.newSimpleName("equalsIgnoreCase"));
        invocation.arguments().add(copy((Expression) node.arguments().get(0)));
        return replace(node, invocation);
    }
    return false;
}

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 ww  w .jav  a 2  s  .c  om
    return replace(expression, replacement);
}

From source file:cn.ieclipse.adt.ext.jdt.SourceGenerator.java

License:Apache License

private static void merge(CompilationUnit unit, String pkgName, String typeName, String auth, String dbName,
        List<String> tableCreators) {
    unit.recordModifications();/* w  ww. ja v  a2  s  . c o  m*/
    AST ast = unit.getAST();
    TypeDeclaration type = (TypeDeclaration) unit.types().get(0);
    ImportDeclaration id = ast.newImportDeclaration();
    id.setName(ast.newName("cn.ieclipse.aorm.Session"));
    unit.imports().add(id);

    id = ast.newImportDeclaration();
    id.setName(ast.newName("android.content.UriMatcher"));
    unit.imports().add(id);

    id = ast.newImportDeclaration();
    id.setName(ast.newName("android.database.sqlite.SQLiteDatabase"));
    unit.imports().add(id);

    id = ast.newImportDeclaration();
    id.setName(ast.newName("android.database.sqlite.SQLiteOpenHelper"));
    unit.imports().add(id);

    id = ast.newImportDeclaration();
    id.setName(ast.newName("java.net.Uri"));
    unit.imports().add(id);

    id = ast.newImportDeclaration();
    id.setName(ast.newName("android.content.ContentValue"));
    unit.imports().add(id);

    VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName("AUTH"));
    StringLiteral sl = ast.newStringLiteral();
    sl.setLiteralValue(auth);
    vdf.setInitializer(sl);

    FieldDeclaration fd = ast.newFieldDeclaration(vdf);
    fd.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL)));
    fd.setType(ast.newSimpleType(ast.newSimpleName("String")));

    int i = 0;
    type.bodyDeclarations().add(i++, fd);

    // URI = Uri.parse("content://" + AUTH);
    vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName("URI"));

    MethodInvocation mi = ast.newMethodInvocation();
    mi.setExpression(ast.newSimpleName("Uri"));
    mi.setName(ast.newSimpleName("parse"));

    InfixExpression fix = ast.newInfixExpression();
    fix.setOperator(InfixExpression.Operator.PLUS);
    sl = ast.newStringLiteral();
    sl.setLiteralValue("content://");
    fix.setLeftOperand(sl);
    fix.setRightOperand(ast.newSimpleName("AUTH"));

    mi.arguments().add(fix);

    vdf.setInitializer(mi);
    fd = ast.newFieldDeclaration(vdf);
    fd.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL)));
    fd.setType(ast.newSimpleType(ast.newSimpleName("Uri")));

    type.bodyDeclarations().add(i++, fd);

    // private mOpenHelper;
    vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName("mOpenHelper"));

    fd = ast.newFieldDeclaration(vdf);
    fd.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD));
    fd.setType(ast.newSimpleType(ast.newName("SQLiteOpenHelper")));
    type.bodyDeclarations().add(i++, fd);

    // private static session;
    vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName("session"));

    fd = ast.newFieldDeclaration(vdf);
    fd.modifiers().addAll(ast.newModifiers((Modifier.PRIVATE | Modifier.STATIC)));
    fd.setType(ast.newSimpleType(ast.newName("Session")));
    type.bodyDeclarations().add(i++, fd);

    // public static Session getSession(){
    // return session;
    // }

    MethodDeclaration md = ast.newMethodDeclaration();
    md.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC | Modifier.STATIC)));
    md.setReturnType2(ast.newSimpleType(ast.newName("Session")));
    md.setName(ast.newSimpleName("getSession"));

    Block methodBlock = ast.newBlock();
    ReturnStatement returnStmt = ast.newReturnStatement();
    returnStmt.setExpression(ast.newSimpleName("session"));
    methodBlock.statements().add(returnStmt);
    md.setBody(methodBlock);
    type.bodyDeclarations().add(i, md);

    // modify onCreate
    rewriteOnCreate(unit, dbName, tableCreators);
}