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

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

Introduction

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

Prototype

public InfixExpression newInfixExpression() 

Source Link

Document

Creates and returns a new unparented infix expression node owned by this AST.

Usage

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   ww w  .  j  a  va 2  s  . co 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();//from www  .j a  va  2  s  . c  om
    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  ww  w .  ja  v a  2 s . com
    return assertion;
}

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);/*w  ww .ja va 2s  .c  om*/
    expr.setLeftOperand(lhs);
    expr.setRightOperand(rhs);
    Types.addBinding(expr, type);
    return expr;
}

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>.
 *//*ww  w  .  j a v  a  2s  .  c o 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>
 *///  w w  w .  j av a  2  s .  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>.
 *///from   ww w .  j av a2  s.c  o  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;

}

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

License:Open Source License

/**
 * Creates the new <CODE>InfixExpression</CODE> <CODE>x % 2 != 0</CODE>
 *///  w ww.  jav  a2 s.  c  o  m
@Override
protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) {
    assert rewrite != null;
    assert numberExpression != null;

    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:org.autorefactor.refactoring.rules.OperatorEnumTest.java

License:Open Source License

@Test
public void simpleTestCompareExpressions() {
    final AST ast = AST.newAST(AST.JLS4);
    final Assignment op1 = ast.newAssignment();
    op1.setOperator(Assignment.Operator.ASSIGN);
    final InfixExpression op2 = ast.newInfixExpression();
    op2.setOperator(InfixExpression.Operator.CONDITIONAL_AND);

    assertTrue(OperatorEnum.compareTo(op1, op2) < 0);

    assertEquals("Comparing unknown objects result in no decision", 0, OperatorEnum.compareTo(op1, null));
}

From source file:org.eclipse.objectteams.otdt.ui.tests.dom.rewrite.ASTRewritingModifyingRoleTest.java

License:Open Source License

/**
 * add role guard predicate//  w w w  .  ja va 2  s.  co  m
 */
public void test0009() throws Exception {
    IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test0009", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test0009;\n");
    buf.append("public team class Team {\n");
    buf.append("   public class Role playedBy Base\n");
    buf.append("    {\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("Team.java", buf.toString(), false, null);

    CompilationUnit astRoot = createCU(cu, false);

    astRoot.recordModifications();

    AST ast = astRoot.getAST();

    TypeDeclaration aTeam = (TypeDeclaration) astRoot.types().get(0);
    RoleTypeDeclaration role = (RoleTypeDeclaration) aTeam.bodyDeclarations().get(0);

    GuardPredicateDeclaration guard = ast.newGuardPredicateDeclaration();

    guard.setBase(true);

    InfixExpression expr = ast.newInfixExpression();
    NumberLiteral lhs = ast.newNumberLiteral();
    lhs.setToken("4");
    expr.setLeftOperand(lhs);
    NumberLiteral rhs = ast.newNumberLiteral();
    rhs.setToken("5");
    expr.setRightOperand(rhs);
    expr.setOperator(Operator.EQUALS);
    guard.setExpression(expr);

    role.setGuardPredicate(guard);

    String preview = evaluateRewrite(cu.getSource(), astRoot);

    buf = new StringBuffer();
    buf.append("package test0009;\n");
    buf.append("public team class Team {\n");
    buf.append("   public class Role playedBy Base\n");
    buf.append("        base when (4 == 5)\n");
    buf.append("    {\n");
    buf.append("    }\n");
    buf.append("}\n");
    assertEqualString(preview, buf.toString());
}