Example usage for org.eclipse.jdt.core.dom TypeDeclaration bodyDeclarations

List of usage examples for org.eclipse.jdt.core.dom TypeDeclaration bodyDeclarations

Introduction

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

Prototype

public List bodyDeclarations() 

Source Link

Document

Returns the live ordered list of body declarations of this type declaration.

Usage

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(TypeDeclaration node) {
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }/*from www .  j  a v  a 2 s  . co m*/
    if (node.getAST().apiLevel() >= JLS3) {
        printModifiers(node.modifiers());
    }
    this.fBuffer.append(node.isInterface() ? "interface " : "class ");//$NON-NLS-2$//$NON-NLS-1$
    node.getName().accept(this);
    if (node.getAST().apiLevel() >= JLS3) {
        if (!node.typeParameters().isEmpty()) {
            this.fBuffer.append("<");//$NON-NLS-1$
            for (Iterator<TypeParameter> it = node.typeParameters().iterator(); it.hasNext();) {
                TypeParameter t = it.next();
                t.accept(this);
                if (it.hasNext()) {
                    this.fBuffer.append(",");//$NON-NLS-1$
                }
            }
            this.fBuffer.append(">");//$NON-NLS-1$
        }
    }
    this.fBuffer.append(" ");//$NON-NLS-1$
    if (node.getAST().apiLevel() >= JLS3) {
        if (node.getSuperclassType() != null) {
            this.fBuffer.append("extends ");//$NON-NLS-1$
            node.getSuperclassType().accept(this);
            this.fBuffer.append(" ");//$NON-NLS-1$
        }
        if (!node.superInterfaceTypes().isEmpty()) {
            this.fBuffer.append(node.isInterface() ? "extends " : "implements ");//$NON-NLS-2$//$NON-NLS-1$
            for (Iterator<Type> it = node.superInterfaceTypes().iterator(); it.hasNext();) {
                Type t = it.next();
                t.accept(this);
                if (it.hasNext()) {
                    this.fBuffer.append(", ");//$NON-NLS-1$
                }
            }
            this.fBuffer.append(" ");//$NON-NLS-1$
        }
    }
    this.fBuffer.append("{");//$NON-NLS-1$
    BodyDeclaration prev = null;
    for (Iterator<BodyDeclaration> it = node.bodyDeclarations().iterator(); it.hasNext();) {
        BodyDeclaration d = it.next();
        if (prev instanceof EnumConstantDeclaration) {
            // enum constant declarations do not include punctuation
            if (d instanceof EnumConstantDeclaration) {
                // enum constant declarations are separated by commas
                this.fBuffer.append(", ");//$NON-NLS-1$
            } else {
                // semicolon separates last enum constant declaration from
                // first class body declarations
                this.fBuffer.append("; ");//$NON-NLS-1$
            }
        }
        d.accept(this);
        prev = d;
    }
    this.fBuffer.append("}");//$NON-NLS-1$
    return false;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(TypeDeclaration node) {
    boa.types.Ast.Declaration.Builder b = boa.types.Ast.Declaration.newBuilder();
    //      b.setPosition(pos.build());
    b.setName(node.getName().getFullyQualifiedName());
    if (node.isInterface())
        b.setKind(boa.types.Ast.TypeKind.INTERFACE);
    else//from w w  w.j a va2 s.  com
        b.setKind(boa.types.Ast.TypeKind.CLASS);
    for (Object m : node.modifiers()) {
        if (((IExtendedModifier) m).isAnnotation())
            ((Annotation) m).accept(this);
        else
            ((org.eclipse.jdt.core.dom.Modifier) m).accept(this);
        b.addModifiers(modifiers.pop());
    }
    for (Object t : node.typeParameters()) {
        boa.types.Ast.Type.Builder tb = boa.types.Ast.Type.newBuilder();
        String name = ((TypeParameter) t).getName().getFullyQualifiedName();
        String bounds = "";
        for (Object o : ((TypeParameter) t).typeBounds()) {
            if (bounds.length() > 0)
                bounds += " & ";
            bounds += typeName((org.eclipse.jdt.core.dom.Type) o);
        }
        if (bounds.length() > 0)
            name = name + " extends " + bounds;
        tb.setName(getIndex(name));
        tb.setKind(boa.types.Ast.TypeKind.GENERIC);
        b.addGenericParameters(tb.build());
    }
    if (node.getSuperclassType() != null) {
        boa.types.Ast.Type.Builder tb = boa.types.Ast.Type.newBuilder();
        tb.setName(getIndex(typeName(node.getSuperclassType())));
        tb.setKind(boa.types.Ast.TypeKind.CLASS);
        b.addParents(tb.build());
    }
    for (Object t : node.superInterfaceTypes()) {
        boa.types.Ast.Type.Builder tb = boa.types.Ast.Type.newBuilder();
        tb.setName(getIndex(typeName((org.eclipse.jdt.core.dom.Type) t)));
        tb.setKind(boa.types.Ast.TypeKind.INTERFACE);
        b.addParents(tb.build());
    }
    for (Object d : node.bodyDeclarations()) {
        if (d instanceof FieldDeclaration) {
            fields.push(new ArrayList<boa.types.Ast.Variable>());
            ((FieldDeclaration) d).accept(this);
            for (boa.types.Ast.Variable v : fields.pop())
                b.addFields(v);
        } else if (d instanceof MethodDeclaration) {
            methods.push(new ArrayList<boa.types.Ast.Method>());
            ((MethodDeclaration) d).accept(this);
            for (boa.types.Ast.Method m : methods.pop())
                b.addMethods(m);
        } else if (d instanceof Initializer) {
            methods.push(new ArrayList<boa.types.Ast.Method>());
            ((Initializer) d).accept(this);
            for (boa.types.Ast.Method m : methods.pop())
                b.addMethods(m);
        } else {
            declarations.push(new ArrayList<boa.types.Ast.Declaration>());
            ((BodyDeclaration) d).accept(this);
            for (boa.types.Ast.Declaration nd : declarations.pop())
                b.addNestedDeclarations(nd);
        }
    }
    // TODO initializers
    // TODO enum constants
    // TODO annotation type members
    declarations.peek().add(b.build());
    return false;
}

From source file:br.com.objectos.way.core.code.jdt.ASTTest.java

License:Apache License

@SuppressWarnings("unchecked")
public void stackoverflow_answer() {
    AST ast = AST.newAST(AST.JLS8);//  ww  w.java2  s  .  c  o m
    CompilationUnit cu = ast.newCompilationUnit();

    PackageDeclaration p1 = ast.newPackageDeclaration();
    p1.setName(ast.newSimpleName("foo"));
    cu.setPackage(p1);

    ImportDeclaration id = ast.newImportDeclaration();
    id.setName(ast.newName(new String[] { "java", "util", "Set" }));
    cu.imports().add(id);

    TypeDeclaration td = ast.newTypeDeclaration();
    td.setName(ast.newSimpleName("Foo"));
    TypeParameter tp = ast.newTypeParameter();
    tp.setName(ast.newSimpleName("X"));
    td.typeParameters().add(tp);
    cu.types().add(td);

    MethodDeclaration md = ast.newMethodDeclaration();
    md.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
    md.setName(ast.newSimpleName("bar"));

    SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
    var.setType(ast.newSimpleType(ast.newSimpleName("String")));
    var.setName(ast.newSimpleName("a"));
    md.parameters().add(var);
    td.bodyDeclarations().add(md);

    Block block = ast.newBlock();
    md.setBody(block);

    MethodInvocation mi = ast.newMethodInvocation();
    mi.setName(ast.newSimpleName("x"));

    ExpressionStatement e = ast.newExpressionStatement(mi);
    block.statements().add(e);

    System.out.println(cu);
}

From source file:br.com.objectos.way.core.code.jdt.MethodDeclarationWriter.java

License:Apache License

void writeTo(TypeDeclaration type) {
    type.bodyDeclarations().add(method);
}

From source file:ch.acanda.eclipse.pmd.java.resolution.design.UseUtilityClassQuickFix.java

License:Open Source License

/**
 * The new private constructor should be inserted before the first method declaration.
 *//*from w  w w  .  j a  v a  2 s  .  com*/
private int findConstructorPosition(final TypeDeclaration typeDeclaration) {
    @SuppressWarnings("unchecked")
    final List<BodyDeclaration> bodyDeclarations = typeDeclaration.bodyDeclarations();
    for (int i = 0; i < bodyDeclarations.size(); i++) {
        if (bodyDeclarations.get(i) instanceof MethodDeclaration) {
            return i;
        }
    }
    return bodyDeclarations.size();
}

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   w w w . j av  a  2 s  .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:coloredide.utils.CopiedNaiveASTFlattener.java

License:Open Source License

public boolean visit(TypeDeclaration node) {
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }/*from  ww w.  j  a  v  a 2 s  .c  om*/
    if (node.getAST().apiLevel() >= AST.JLS3) {
        printModifiers(node.modifiers());
    }
    this.buffer.append(node.isInterface() ? "interface " : "class ");//$NON-NLS-2$//$NON-NLS-1$
    node.getName().accept(this);
    if (node.getAST().apiLevel() >= AST.JLS3) {
        if (!node.typeParameters().isEmpty()) {
            this.buffer.append("<");//$NON-NLS-1$
            for (Iterator it = node.typeParameters().iterator(); it.hasNext();) {
                TypeParameter t = (TypeParameter) it.next();
                t.accept(this);
                if (it.hasNext()) {
                    this.buffer.append(",");//$NON-NLS-1$
                }
            }
            this.buffer.append(">");//$NON-NLS-1$
        }
    }
    this.buffer.append(" ");//$NON-NLS-1$
    if (node.getAST().apiLevel() >= AST.JLS3) {
        if (node.getSuperclassType() != null) {
            this.buffer.append("extends ");//$NON-NLS-1$
            node.getSuperclassType().accept(this);
            this.buffer.append(" ");//$NON-NLS-1$
        }
        if (!node.superInterfaceTypes().isEmpty()) {
            this.buffer.append(node.isInterface() ? "extends " : "implements ");//$NON-NLS-2$//$NON-NLS-1$
            for (Iterator it = node.superInterfaceTypes().iterator(); it.hasNext();) {
                Type t = (Type) it.next();
                t.accept(this);
                if (it.hasNext()) {
                    this.buffer.append(", ");//$NON-NLS-1$
                }
            }
            this.buffer.append(" ");//$NON-NLS-1$
        }
    }
    this.buffer.append("{\n");//$NON-NLS-1$
    this.indent++;
    BodyDeclaration prev = null;
    for (Iterator it = node.bodyDeclarations().iterator(); it.hasNext();) {
        BodyDeclaration d = (BodyDeclaration) it.next();
        if (prev instanceof EnumConstantDeclaration) {
            // enum constant declarations do not include punctuation
            if (d instanceof EnumConstantDeclaration) {
                // enum constant declarations are separated by commas
                this.buffer.append(", ");//$NON-NLS-1$
            } else {
                // semicolon separates last enum constant declaration from
                // first class body declarations
                this.buffer.append("; ");//$NON-NLS-1$
            }
        }
        d.accept(this);
    }
    this.indent--;
    printIndent();
    this.buffer.append("}\n");//$NON-NLS-1$
    return false;
}

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

License:Open Source License

private void processInlineLicense(IDocument doc) throws Exception {
    CompilationUnit cu = getAST(doc);//from www  .j a v a  2  s . c o m
    cu.recordModifications();
    AST ast = cu.getAST();

    if (cu.types().get(0) instanceof TypeDeclaration) {
        TypeDeclaration td = (TypeDeclaration) cu.types().get(0);
        FieldDeclaration[] fd = td.getFields();
        if (fd.length == 0) {
            td.bodyDeclarations().add(0, createLiceseInLineField(ast));
        } else {
            FieldDeclaration firstFd = fd[0];
            VariableDeclarationFragment vdf = (VariableDeclarationFragment) firstFd.fragments().get(0);
            if (vdf.getName().getIdentifier().equals("COPYRIGHT")) {
                td.bodyDeclarations().remove(0);
                td.bodyDeclarations().add(0, createLiceseInLineField(ast));
            } else {
                td.bodyDeclarations().add(0, createLiceseInLineField(ast));
            }
        }
    }

    //record changes
    TextEdit edits = cu.rewrite(doc, null);
    edits.apply(doc);
}

From source file:com.architexa.diagrams.relo.jdt.ParseUtilities.java

License:Open Source License

static private MethodDeclaration findMethodDeclaration(TypeDeclaration type, IMethod method)
        throws JavaModelException {
    ISourceRange sourceRange = method.getSourceRange();
    for (Iterator<?> I = type.bodyDeclarations().iterator(); I.hasNext();) {
        BodyDeclaration declaration = (BodyDeclaration) I.next();
        if (!(declaration instanceof MethodDeclaration)) {
            continue;
        }//w w  w .j a  va  2  s  . c o  m
        if (isNodeInRange(declaration, sourceRange)) {
            return (MethodDeclaration) declaration;
        }
    }
    return null;
}

From source file:com.bsiag.eclipse.jdt.java.formatter.LineBreaksPreparator.java

License:Open Source License

public boolean visit(TypeDeclaration node) {
    handleBodyDeclarations(node.bodyDeclarations());

    if (node.getName().getStartPosition() == -1)
        return true; // this is a fake type created by parsing in class body mode

    breakLineBefore(node);/*  w ww.j  a v a  2  s.  c o  m*/

    handleBracedCode(node, node.getName(), this.options.brace_position_for_type_declaration,
            this.options.indent_body_declarations_compare_to_type_header,
            this.options.insert_new_line_in_empty_type_declaration);

    this.declarationModifierVisited = false;
    return true;
}