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

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

Introduction

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

Prototype

public List newModifiers(int flags) 

Source Link

Document

Creates and returns a list of new unparented modifier nodes for the given modifier flags.

Usage

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();//ww  w  .  j  ava2  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);
}

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

License:Apache License

private static void genInnerSQLiteOpenHelper(AnonymousClassDeclaration acd, AST ast,
        List<String> tableCreators) {
    MethodDeclaration md = ast.newMethodDeclaration();
    md.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC)));
    md.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
    md.setName(ast.newSimpleName("onCreate"));
    SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
    svd.setName(ast.newSimpleName("db"));
    svd.setType(ast.newSimpleType(ast.newSimpleName("SQLiteDatabase")));
    md.parameters().add(svd);/*from ww w . j  ava2s . co  m*/
    Block innerBlock = ast.newBlock();
    md.setBody(innerBlock);
    VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName("sql"));
    StringLiteral sl = ast.newStringLiteral();
    sl.setLiteralValue("");
    vdf.setInitializer(sl);
    VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
    vds.setType(ast.newSimpleType(ast.newSimpleName("String")));
    innerBlock.statements().add(vds);
    for (String creator : tableCreators) {
        String[] lines = creator.split(SourceAnalysis.LF);
        for (String line : lines) {
            Assignment a = ast.newAssignment();
            a.setOperator(Assignment.Operator.PLUS_ASSIGN);
            a.setLeftHandSide(ast.newSimpleName("sql"));
            StringLiteral temp = ast.newStringLiteral();
            temp.setLiteralValue(line);
            a.setRightHandSide(temp);
            innerBlock.statements().add(ast.newExpressionStatement(a));
        }

        MethodInvocation mi = ast.newMethodInvocation();
        mi.setName(ast.newSimpleName("execSQL"));
        mi.setExpression(ast.newSimpleName("db"));
        mi.arguments().add(ast.newSimpleName("sql"));
        innerBlock.statements().add(ast.newExpressionStatement(mi));
    }

    acd.bodyDeclarations().add(md);
    // onUpgrade
    md = ast.newMethodDeclaration();
    md.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC)));
    md.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
    md.setName(ast.newSimpleName("onUpgrade"));
    svd = ast.newSingleVariableDeclaration();
    svd.setName(ast.newSimpleName("db"));
    svd.setType(ast.newSimpleType(ast.newSimpleName("SQLiteDatabase")));
    md.parameters().add(svd);

    svd = ast.newSingleVariableDeclaration();
    svd.setName(ast.newSimpleName("oldVersion"));
    svd.setType(ast.newPrimitiveType(PrimitiveType.INT));
    md.parameters().add(svd);

    svd = ast.newSingleVariableDeclaration();
    svd.setName(ast.newSimpleName("newVersion"));
    svd.setType(ast.newPrimitiveType(PrimitiveType.INT));
    md.parameters().add(svd);

    innerBlock = ast.newBlock();
    md.setBody(innerBlock);
    acd.bodyDeclarations().add(md);
}

From source file:com.alex.example.fixlicense.actions.SampleAction.java

License:Open Source License

private FieldDeclaration createLiceseInLineField(AST ast) {
    VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName("COPYRIGHT"));
    StringLiteral sl = ast.newStringLiteral();
    sl.setLiteralValue(license_inline);/*  w  w w  . ja  v  a  2  s. c  o  m*/
    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")));

    return fd;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
private void addMethod(String name, int modifiers, ITypeBinding type, List<Statement> initStatements,
        List<BodyDeclaration> members, AST ast, boolean isConstructor) {
    Block body = ast.newBlock();//from   ww  w . ja v a2  s. co m
    List<Statement> stmts = body.statements(); // safe by definition
    for (Statement stmt : initStatements) {
        Statement newStmt = NodeCopier.copySubtree(ast, stmt);
        stmts.add(newStmt);
    }
    MethodDeclaration method = ast.newMethodDeclaration();
    GeneratedMethodBinding binding = new GeneratedMethodBinding(name, modifiers, null, type, isConstructor,
            false, true);
    Types.addBinding(method, binding);
    Type returnType = ast.newPrimitiveType(PrimitiveType.VOID);
    Types.addBinding(returnType, ast.resolveWellKnownType("void"));
    method.setReturnType2(returnType);
    method.setBody(body);
    method.setConstructor(isConstructor);
    method.modifiers().addAll(ast.newModifiers(modifiers));
    SimpleName nameNode = NameTable.unsafeSimpleName(name, ast);
    Types.addBinding(nameNode, binding);
    method.setName(nameNode);
    members.add(method);
    Symbols.resolve(method, binding);
}

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

License:Open Source License

/**
 * Create an unbound accessor method, minus its code.
 *///from   w w w.  ja  va 2  s.c om
@SuppressWarnings("unchecked") // safe by specification
private MethodDeclaration createBlankAccessor(VariableDeclarationFragment var, String name, int modifiers,
        Type returnType) {
    AST ast = var.getAST();
    MethodDeclaration accessor = ast.newMethodDeclaration();
    accessor.setName(ast.newSimpleName(name));
    accessor.modifiers().addAll(ast.newModifiers(modifiers));
    accessor.setBody(ast.newBlock());
    accessor.setReturnType2(NodeCopier.copySubtree(ast, returnType));
    return accessor;
}

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

License:Apache License

@SuppressWarnings("unchecked")
public static List<Modifier> newModifiers(AST ast, int flags) {
    return ast.newModifiers(flags);
}

From source file:com.idega.eclipse.ejbwizards.BeanCreator.java

License:Open Source License

protected TypeDeclaration getTypeDeclaration(AST ast, String name, boolean isInterface, String superClass,
        String[] interfaces, Set imports) {
    TypeDeclaration classType = ast.newTypeDeclaration();
    classType.setInterface(isInterface);
    classType.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    classType.setName(ast.newSimpleName(name));
    if (isInterface) {
        classType.superInterfaceTypes().add(ast.newSimpleType(ast.newSimpleName(superClass)));
    } else {//  w  ww.j a  v  a 2 s . c  o m
        classType.setSuperclassType(ast.newSimpleType(ast.newSimpleName(superClass)));
    }

    if (interfaces != null) {
        for (int i = 0; i < interfaces.length; i++) {
            if (!Signature.getSignatureSimpleName(interfaces[i]).equals(name)) {
                classType.superInterfaceTypes().add(
                        ast.newSimpleType(ast.newSimpleName(Signature.getSignatureSimpleName(interfaces[i]))));
                imports.add(getImportSignature(Signature.toString(interfaces[i])));
            }
        }
    }

    return classType;
}

From source file:com.idega.eclipse.ejbwizards.BeanCreator.java

License:Open Source License

protected MethodDeclaration getMethodDeclaration(AST ast, IMethod method, String methodName, String returnType,
        Set imports, boolean addJavadoc) throws JavaModelException {
    String[] exceptions = method.getExceptionTypes();
    String[] parameterTypes = method.getParameterTypes();
    String[] parameterNames = method.getParameterNames();

    MethodDeclaration methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    methodConstructor.setReturnType2(getType(ast, returnType));
    methodConstructor.setName(ast.newSimpleName(methodName));
    if (returnType != null) {
        imports.add(getImportSignature(returnType));
    }/*  ww w.j a va2s.c om*/

    for (int i = 0; i < exceptions.length; i++) {
        methodConstructor.thrownExceptions()
                .add(ast.newSimpleName(Signature.getSignatureSimpleName(exceptions[i])));
        imports.add(getImportSignature(Signature.toString(exceptions[i])));
    }

    for (int i = 0; i < parameterTypes.length; i++) {
        String parameterType = getReturnType(parameterTypes[i]);

        SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration();
        variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
        variableDeclaration.setType(getType(ast, parameterType));
        variableDeclaration.setName(ast.newSimpleName(parameterNames[i]));
        methodConstructor.parameters().add(variableDeclaration);

        imports.add(getImportSignature(Signature.toString(parameterTypes[i])));
    }

    if (addJavadoc) {
        methodConstructor.setJavadoc(getJavadoc(ast, method));
    }

    return methodConstructor;
}

From source file:com.idega.eclipse.ejbwizards.IBOEntityCreator.java

License:Open Source License

private void createHomeInterface(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage,
        String name) throws JavaModelException, MalformedTreeException, BadLocationException {
    iUnit.getBuffer().setContents("");
    String source = iUnit.getBuffer().getContents();
    Document document = new Document(source);

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(iUnit);/*from   w  ww .  j  a  v a2 s.  c o m*/

    CompilationUnit unit = (CompilationUnit) parser.createAST(null);
    unit.recordModifications();

    AST ast = unit.getAST();

    // Package statement
    unit.setPackage(getPackageDeclaration(ast, typePackage));

    // class declaration
    TypeDeclaration classType = getTypeDeclaration(ast, name + "Home", true, "IBOHome", null,
            getHomeInterfaceImports());
    addHomeInterfaceImport("com.idega.business.IBOHome");
    unit.types().add(classType);

    // create() method
    MethodDeclaration methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
    methodConstructor.setName(ast.newSimpleName("create"));
    methodConstructor.thrownExceptions().add(ast.newName("CreateException"));
    addHomeInterfaceImport("javax.ejb.CreateException");
    methodConstructor.thrownExceptions().add(ast.newName("RemoteException"));
    addHomeInterfaceImport("java.rmi.RemoteException");
    classType.bodyDeclarations().add(methodConstructor);

    writeImports(ast, unit, getHomeInterfaceImports());
    commitChanges(iUnit, unit, document);
}

From source file:com.idega.eclipse.ejbwizards.IBOEntityCreator.java

License:Open Source License

private void createHomeImplementation(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage,
        String name) throws JavaModelException, MalformedTreeException, BadLocationException {
    iUnit.getBuffer().setContents("");
    String source = iUnit.getBuffer().getContents();
    Document document = new Document(source);

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(iUnit);//from ww  w  .ja v  a  2 s.  co m

    CompilationUnit unit = (CompilationUnit) parser.createAST(monitor);
    unit.recordModifications();

    AST ast = unit.getAST();

    // Package statement
    PackageDeclaration packageDeclaration = ast.newPackageDeclaration();
    unit.setPackage(packageDeclaration);
    packageDeclaration.setName(ast.newName(typePackage));

    // class declaration
    TypeDeclaration classType = getTypeDeclaration(ast, name + "HomeImpl", false, "IBOHomeImpl", null,
            getHomeImplImports());
    classType.superInterfaceTypes().add(ast.newSimpleType(ast.newSimpleName(name + "Home")));
    addHomeImplImport("com.idega.business.IBOHomeImpl");
    unit.types().add(classType);

    // create() method
    MethodDeclaration methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    if (this.isJDK1_5) {
        MarkerAnnotation annotation = ast.newMarkerAnnotation();
        annotation.setTypeName(ast.newSimpleName("Override"));
        methodConstructor.modifiers().add(annotation);
    }
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    if (this.isJDK1_5) {
        ParameterizedType returnType = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("Class")));
        returnType.typeArguments().add(ast.newSimpleType(ast.newSimpleName(name)));
        methodConstructor.setReturnType2(returnType);
    } else {
        methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName("Class")));
    }
    methodConstructor.setName(ast.newSimpleName("getBeanInterfaceClass"));

    classType.bodyDeclarations().add(methodConstructor);

    Block constructorBlock = ast.newBlock();
    methodConstructor.setBody(constructorBlock);

    TypeLiteral typeLiteral = ast.newTypeLiteral();
    typeLiteral.setType(ast.newSimpleType(ast.newSimpleName(name)));

    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(typeLiteral);
    constructorBlock.statements().add(returnStatement);

    // create() method
    methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
    methodConstructor.setName(ast.newSimpleName("create"));
    methodConstructor.thrownExceptions().add(ast.newName("CreateException"));
    addHomeImplImport("javax.ejb.CreateException");
    classType.bodyDeclarations().add(methodConstructor);

    constructorBlock = ast.newBlock();
    methodConstructor.setBody(constructorBlock);

    SuperMethodInvocation superMethodInvocation = ast.newSuperMethodInvocation();
    superMethodInvocation.setName(ast.newSimpleName("createIBO"));

    CastExpression ce = ast.newCastExpression();
    ce.setType(ast.newSimpleType(ast.newSimpleName(name)));
    ce.setExpression(superMethodInvocation);

    returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ce);
    constructorBlock.statements().add(returnStatement);

    writeImports(ast, unit, getHomeImplImports());
    commitChanges(iUnit, unit, document);
}