Example usage for org.eclipse.jdt.core.dom InfixExpression setOperator

List of usage examples for org.eclipse.jdt.core.dom InfixExpression setOperator

Introduction

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

Prototype

public void setOperator(InfixExpression.Operator operator) 

Source Link

Document

Sets the operator of this infix expression.

Usage

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}.
 *//*w  ww .ja  v a  2  s  .  co  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.optimization.SimplifyStartsWithQuickFix.java

License:Open Source License

/**
 * Rewrites <code>s.startsWith("a")</code> as <code>s.charAt(0) == 'a'</code>.
 *//*from   w ww. j av  a  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: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 .jav  a  2s  .co 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);
}

From source file:com.ashigeru.eclipse.util.jdt.internal.ui.handlers.InsertAssertionHandler.java

License:Apache License

private AssertStatement createAssertion(AST factory, String paramName) {
    assert factory != null;
    assert paramName != null;
    AssertStatement assertion = factory.newAssertStatement();
    InfixExpression notNull = factory.newInfixExpression();
    notNull.setLeftOperand(factory.newSimpleName(paramName));
    notNull.setOperator(Operator.NOT_EQUALS);
    notNull.setRightOperand(factory.newNullLiteral());
    assertion.setExpression(notNull);/*from  w w  w  .  j a  v  a  2 s.  c  om*/
    return assertion;
}

From source file:com.google.devtools.j2cpp.translate.Autoboxer.java

License:Open Source License

private InfixExpression newInfixExpression(Expression lhs, Expression rhs, Assignment.Operator op,
        ITypeBinding lhType) {//from   w ww.jav  a 2s. c o  m
    InfixExpression newRhs = ast.newInfixExpression();
    newRhs.setLeftOperand(unbox(lhs));
    newRhs.setRightOperand(unbox(rhs));
    InfixExpression.Operator infixOp;
    // op isn't an enum, so this can't be a switch.
    if (op == Assignment.Operator.PLUS_ASSIGN) {
        infixOp = InfixExpression.Operator.PLUS;
    } else if (op == Assignment.Operator.MINUS_ASSIGN) {
        infixOp = InfixExpression.Operator.MINUS;
    } else if (op == Assignment.Operator.TIMES_ASSIGN) {
        infixOp = InfixExpression.Operator.TIMES;
    } else if (op == Assignment.Operator.DIVIDE_ASSIGN) {
        infixOp = InfixExpression.Operator.DIVIDE;
    } else if (op == Assignment.Operator.BIT_AND_ASSIGN) {
        infixOp = InfixExpression.Operator.AND;
    } else if (op == Assignment.Operator.BIT_OR_ASSIGN) {
        infixOp = InfixExpression.Operator.OR;
    } else if (op == Assignment.Operator.BIT_XOR_ASSIGN) {
        infixOp = InfixExpression.Operator.XOR;
    } else if (op == Assignment.Operator.REMAINDER_ASSIGN) {
        infixOp = InfixExpression.Operator.REMAINDER;
    } else if (op == Assignment.Operator.LEFT_SHIFT_ASSIGN) {
        infixOp = InfixExpression.Operator.LEFT_SHIFT;
    } else if (op == Assignment.Operator.RIGHT_SHIFT_SIGNED_ASSIGN) {
        infixOp = InfixExpression.Operator.RIGHT_SHIFT_SIGNED;
    } else if (op == Assignment.Operator.RIGHT_SHIFT_UNSIGNED_ASSIGN) {
        infixOp = InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED;
    } else {
        throw new IllegalArgumentException();
    }
    newRhs.setOperator(infixOp);
    Types.addBinding(newRhs, Types.getPrimitiveType(lhType));
    return newRhs;
}

From source file:com.google.devtools.j2objc.translate.ASTFactory.java

License:Apache License

public static InfixExpression newInfixExpression(AST ast, Expression lhs, InfixExpression.Operator op,
        Expression rhs, ITypeBinding type) {
    InfixExpression expr = ast.newInfixExpression();
    expr.setOperator(op);
    expr.setLeftOperand(lhs);//from   w  w  w.j a va  2 s  .  c  om
    expr.setRightOperand(rhs);
    Types.addBinding(expr, type);
    return expr;
}

From source file:com.motorola.studio.android.generatecode.AbstractCodeGenerator.java

License:Apache License

/**
 * Creates a chain og else if and else statement for the given if statement
 * @param ifSt/*  ww  w . j  a  v  a 2  s . c  o  m*/
 * @param invocation
 * @param guiQN
 */
protected void createElseIfAndElseStatements(IfStatement ifSt, MethodInvocation invocation,
        QualifiedName guiQN) {
    InfixExpression infixExp = typeDeclaration.getAST().newInfixExpression();
    infixExp.setOperator(InfixExpression.Operator.EQUALS);

    infixExp.setLeftOperand(invocation);
    infixExp.setRightOperand(guiQN);

    //first verifies if the expression of the if statement is missing, it means we created it, just need to add the expression.
    //Otherwise, the "else if" chain must be verified before add the new if statement
    if (ifSt.getExpression().toString().equals(JavaViewBasedOnLayoutModifierConstants.EXPRESSION_MISSING)) {
        ifSt.setExpression(infixExp);
    } else {
        boolean expressionAlreadyExists = false;
        //verifies if the first if's expression already verifies the current menu item or radio button
        if (ifChainContainsExpression(ifSt, infixExp)) {
            expressionAlreadyExists = true;
        }

        if (!expressionAlreadyExists) {
            IfStatement lastIfStatement = getLastIfStatementInChain(ifSt);
            if (lastIfStatement != null) {
                IfStatement elseSt = typeDeclaration.getAST().newIfStatement();
                elseSt.setExpression(infixExp);
                if (lastIfStatement.getElseStatement() != null) {
                    Statement oldElseStatement = lastIfStatement.getElseStatement();
                    elseSt.setElseStatement((Statement) ASTNode.copySubtree(elseSt.getAST(), oldElseStatement));
                    lastIfStatement.setElseStatement(elseSt);
                } else {
                    lastIfStatement.setElseStatement(elseSt);
                }
            }
        }
    }
}

From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.CreateAndOddnessCheckResolution.java

License:Open Source License

/**
 * Creates the new <CODE>InfixExpression</CODE> <CODE>(x & 1) == 1</CODE>.
 *//*from ww w  .  j ava  2  s.co  m*/
@Override
protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) {
    Assert.isNotNull(rewrite);
    Assert.isNotNull(numberExpression);

    final AST ast = rewrite.getAST();
    InfixExpression andOddnessCheck = ast.newInfixExpression();
    ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
    InfixExpression andExpression = ast.newInfixExpression();

    andExpression.setLeftOperand((Expression) rewrite.createMoveTarget(numberExpression));
    andExpression.setOperator(AND);
    andExpression.setRightOperand(ast.newNumberLiteral("1"));
    parenthesizedExpression.setExpression(andExpression);
    andOddnessCheck.setLeftOperand(parenthesizedExpression);
    andOddnessCheck.setOperator(EQUALS);
    andOddnessCheck.setRightOperand(ast.newNumberLiteral("1"));

    return andOddnessCheck;

}

From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.CreateRemainderOddnessCheckResolution.java

License:Open Source License

/**
 * Creates the new <CODE>InfixExpression</CODE> <CODE>x % 2 != 0</CODE>
 *///from  ww w  .  j a va  2s.  c o  m
@Override
protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) {
    Assert.isNotNull(rewrite);
    Assert.isNotNull(numberExpression);

    final AST ast = rewrite.getAST();
    InfixExpression correctOddnessCheck = ast.newInfixExpression();
    InfixExpression remainderExp = ast.newInfixExpression();

    correctOddnessCheck.setLeftOperand(remainderExp);
    correctOddnessCheck.setOperator(NOT_EQUALS);
    correctOddnessCheck.setRightOperand(ast.newNumberLiteral("0"));

    remainderExp.setLeftOperand((Expression) rewrite.createMoveTarget(numberExpression));
    remainderExp.setOperator(REMAINDER);
    remainderExp.setRightOperand(ast.newNumberLiteral("2"));

    return correctOddnessCheck;
}

From source file:edu.umd.cs.findbugs.quickfix.resolution.CreateAndOddnessCheckResolution.java

License:Open Source License

/**
 * Creates the new <CODE>InfixExpression</CODE> <CODE>(x & 1) == 1</CODE>.
 *///  w  w  w  .  j a v  a  2s  .co  m
@Override
protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) {
    assert rewrite != null;
    assert numberExpression != null;

    final AST ast = rewrite.getAST();
    InfixExpression andOddnessCheck = ast.newInfixExpression();
    ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
    InfixExpression andExpression = ast.newInfixExpression();

    andExpression.setLeftOperand((Expression) rewrite.createMoveTarget(numberExpression));
    andExpression.setOperator(AND);
    andExpression.setRightOperand(ast.newNumberLiteral("1"));
    parenthesizedExpression.setExpression(andExpression);
    andOddnessCheck.setLeftOperand(parenthesizedExpression);
    andOddnessCheck.setOperator(EQUALS);
    andOddnessCheck.setRightOperand(ast.newNumberLiteral("1"));

    return andOddnessCheck;

}