Example usage for org.eclipse.jdt.core.dom CompilationUnit imports

List of usage examples for org.eclipse.jdt.core.dom CompilationUnit imports

Introduction

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

Prototype

ASTNode.NodeList imports

To view the source code for org.eclipse.jdt.core.dom CompilationUnit imports.

Click Source Link

Document

The list of import declarations in textual order order; initially none (elementType: ImportDeclaration).

Usage

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

License:Open Source License

@Override
public boolean visit(CompilationUnit node) {
    if (node.getPackage() != null) {
        node.getPackage().accept(this);
    }/*  ww  w . j  av  a  2  s . com*/
    for (Iterator<ImportDeclaration> it = node.imports().iterator(); it.hasNext();) {
        ImportDeclaration d = it.next();
        d.accept(this);
    }
    for (Iterator<AbstractTypeDeclaration> it = node.types().iterator(); it.hasNext();) {
        AbstractTypeDeclaration d = it.next();
        d.accept(this);
    }
    return false;
}

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

License:Apache License

@Override
public boolean visit(CompilationUnit node) {
    //      b.setPosition(pos.build());
    PackageDeclaration pkg = node.getPackage();
    if (pkg == null) {
        b.setName("");
    } else {//w w  w .j a va  2s.  c  o m
        b.setName(pkg.getName().getFullyQualifiedName());
        for (Object a : pkg.annotations()) {
            ((Annotation) a).accept(this);
            b.addModifiers(modifiers.pop());
        }
    }
    for (Object i : node.imports()) {
        ImportDeclaration id = (ImportDeclaration) i;
        String imp = "";
        if (id.isStatic())
            imp += "static ";
        imp += id.getName().getFullyQualifiedName();
        if (id.isOnDemand())
            imp += ".*";
        imports.add(imp);
    }
    for (Object t : node.types()) {
        declarations.push(new ArrayList<boa.types.Ast.Declaration>());
        ((AbstractTypeDeclaration) t).accept(this);
        for (boa.types.Ast.Declaration d : declarations.pop())
            b.addDeclarations(d);
    }
    for (Object c : node.getCommentList())
        ((org.eclipse.jdt.core.dom.Comment) c).accept(this);
    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);/*from ww  w.  j a  v a  2s  .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: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.  j a  v a2  s  . com
    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(CompilationUnit node) {
    if (node.getPackage() != null) {
        node.getPackage().accept(this);
    }/*from  w w w.j  a  va 2 s .  c om*/
    for (Iterator it = node.imports().iterator(); it.hasNext();) {
        ImportDeclaration d = (ImportDeclaration) it.next();
        d.accept(this);
    }
    for (Iterator it = node.types().iterator(); it.hasNext();) {
        AbstractTypeDeclaration d = (AbstractTypeDeclaration) it.next();
        d.accept(this);
    }
    return false;
}

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

License:Open Source License

@Override
public boolean visit(CompilationUnit node) {
    List<ImportDeclaration> imports = node.imports();
    if (!imports.isEmpty()) {
        int index = this.tm.firstIndexIn(imports.get(0), -1);
        if (index > 0)
            this.tm.get(index).putLineBreaksBefore(this.options.blank_lines_before_imports + 1);
    }/*  ww  w. j a  va2 s  .  c o  m*/

    List<AnnotationTypeDeclaration> types = node.types();
    if (!types.isEmpty()) {
        if (!imports.isEmpty())
            this.tm.firstTokenIn(types.get(0), -1)
                    .putLineBreaksBefore(this.options.blank_lines_after_imports + 1);
        for (int i = 1; i < types.size(); i++) {
            this.tm.firstTokenIn(types.get(i), -1)
                    .putLineBreaksBefore(this.options.blank_lines_between_type_declarations + 1);
        }
    }
    return true;
}

From source file:com.bsiag.eclipse.jdt.java.formatter.linewrap.WrapPreparator.java

License:Open Source License

@Override
public boolean visit(CompilationUnit node) {
    List<ImportDeclaration> imports = node.imports();
    if (!imports.isEmpty()) {
        this.importsStart = this.tm.firstIndexIn(imports.get(0), -1);
        this.importsEnd = this.tm.lastIndexIn(imports.get(imports.size() - 1), -1);
    }/*from w  w w .  j  av  a  2  s  .  c o  m*/
    return true;
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.forward.ForwardJavaSrcDir_Files.java

License:Open Source License

/**
 * @throws CodeSyncException /*w w  w . j  a v a  2  s . c om*/
 * @flowerModelElementId _IEIN9erfEd6QaI9daHlzjA
 */
@SuppressWarnings("unchecked")
@Override
protected ReverseStatus forwardElement(Type modelElement, IContainer parentAstElement,
        CompilationUnit astElement) throws CodeSyncException {
    try {
        astElement.recordModifications();
    } catch (IllegalArgumentException e) {
        //it is ok, we swallow ,recordModification had been called already on creation of compilation unit 
    }
    // TODO :: should update only when something specific happens? moving in another package,renaming the package,moving the package?
    // this updates the package declaration
    AST ast = astElement.getAST();
    PackageDeclaration packageDeclaration = astElement.getPackage();
    if (packageDeclaration == null) {
        String fullyPackageName = SyncUtils.getFullyQualifiedPackageNameFromType(modelElement);
        // a java type in the default package can not have the package declaration
        if (fullyPackageName.length() > 0) {
            packageDeclaration = ast.newPackageDeclaration();
            astElement.setPackage(packageDeclaration);
            packageDeclaration.setName(ast.newName(fullyPackageName));
        }
    }

    // creating the list 
    HashSet<String> fullyQualifiedImports = new HashSet<String>();
    for (ImportDeclaration importDeclaration : (List<ImportDeclaration>) astElement.imports())
        fullyQualifiedImports.add(importDeclaration.getName().getFullyQualifiedName());
    setContextForImportDeclarations(astElement, fullyQualifiedImports, modelElement.getNearestPackage());

    ReverseStatus status = new ReverseStatus();
    if (modelElement instanceof Interface) {
        forwardJavaInterface.setParentPackage(parentAstElement);
        status.applyOr(forwardJavaInterface.forward((Interface) modelElement, astElement));
    } else if (modelElement instanceof Class) {
        forwardJavaClass.setParentPackage(parentAstElement);
        status.applyOr(forwardJavaClass.forward((Class) modelElement, astElement));
    } else
        throw new IllegalArgumentException("could not recognize instance of " + modelElement);

    IFile javaFile = (IFile) parentAstElement.getFile(new Path(modelElement.getName() + ".java"));

    try {
        JavaSyncUtils.writeJavafile(astElement, javaFile);
        codeSyncAlgorithm.addLogEntry(javaFile.getFullPath().toString() + " - file modified");
        ((TimeStampedSyncElement) modelElement).setSyncTimeStamp(Long.toString(javaFile.getLocalTimeStamp()));
    } catch (Exception e) {
        throw new RuntimeException("Error during file save: " + javaFile, e);
    }
    return status;
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.forward.ForwardJavaSrcDir_Files.java

License:Open Source License

/**
 * @flowerModelElementId _IEIOA-rfEd6QaI9daHlzjA
 *///from w w w  .  j  a v  a  2 s  .co  m
@SuppressWarnings("unchecked")
@Override
protected void addImportToCurrentCompilationUnit(String fullyQualifiedTypeName,
        CompilationUnit currentCompilationUnit) {
    AST ast = currentCompilationUnit.getAST();
    ImportDeclaration importDeclaration = ast.newImportDeclaration();
    try {
        importDeclaration.setName(ast.newName(fullyQualifiedTypeName));
        currentCompilationUnit.imports().add(importDeclaration);
    } catch (Exception e) {
        // @author Sorin
        // Usually this exception it thrown when from a generic type name we try to create an ast to insert it into the imports declarations.
        // Mostly this happens when the documentation for example is changed, and the forwarding mechanism will pass through all the features
        // which try to add an import declaration but this is not really needed.
    }
}

From source file:com.google.devtools.j2objc.jdt.JdtJ2ObjCIncompatibleStripper.java

License:Apache License

private JdtJ2ObjCIncompatibleStripper(CompilationUnit unit) {
    @SuppressWarnings("unchecked")
    List<ImportDeclaration> imports = unit.imports();
    for (ImportDeclaration importNode : imports) {
        String name = getLastComponent(importNode.getName());
        if (importNode.isStatic()) {
            unusedStaticImports.put(name, importNode);
        } else {/*w  ww  .  ja  v a2 s. c  om*/
            unusedImports.put(name, importNode);
        }
    }
}