Search in sources :

Example 1 with JCImport

use of com.sun.tools.javac.tree.JCTree.JCImport in project lombok by rzwitserloot.

the class JavacImportList method applyNameToStarImports.

@Override
public Collection<String> applyNameToStarImports(String startsWith, String name) {
    ArrayList<String> out = new ArrayList<String>();
    if (pkgStr != null && topLevelName(pkgStr).equals(startsWith))
        out.add(pkgStr + "." + name);
    for (JCTree def : defs) {
        if (!(def instanceof JCImport))
            continue;
        if (((JCImport) def).staticImport)
            continue;
        JCTree qual = ((JCImport) def).qualid;
        if (!(qual instanceof JCFieldAccess))
            continue;
        String simpleName = ((JCFieldAccess) qual).name.toString();
        if (!"*".equals(simpleName))
            continue;
        String topLevelName = topLevelName(qual);
        if (topLevelName.equals(startsWith)) {
            out.add(((JCFieldAccess) qual).selected.toString() + "." + name);
        }
    }
    return out;
}
Also used : JCImport(com.sun.tools.javac.tree.JCTree.JCImport) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) ArrayList(java.util.ArrayList) JCTree(com.sun.tools.javac.tree.JCTree)

Example 2 with JCImport

use of com.sun.tools.javac.tree.JCTree.JCImport in project lombok by rzwitserloot.

the class PrettyPrinter method visitTopLevel.

@Override
public void visitTopLevel(JCCompilationUnit tree) {
    printDocComment(tree);
    if (tree.pid != null) {
        consumeComments(tree);
        aPrint("package ");
        print(tree.pid);
        println(";", tree.pid);
    }
    boolean first = true;
    for (JCTree child : tree.defs) {
        if (!(child instanceof JCImport))
            continue;
        if (first)
            println();
        first = false;
        print(child);
    }
    for (JCTree child : tree.defs) {
        if (child instanceof JCImport)
            continue;
        print(child);
    }
    consumeComments(Integer.MAX_VALUE);
}
Also used : JCImport(com.sun.tools.javac.tree.JCTree.JCImport) JCTree(com.sun.tools.javac.tree.JCTree)

Example 3 with JCImport

use of com.sun.tools.javac.tree.JCTree.JCImport in project ceylon-compiler by ceylon.

the class ClassDocImpl method importedPackages.

/**
     * Get the list of packages declared as imported.
     * These are called "type-import-on-demand declarations" in the JLS.
     * This method is deprecated in the ClassDoc interface.
     *
     * @return an array of PackageDocImpl representing the imported packages.
     *
     * ###NOTE: the syntax supports importing all inner classes from a class as well.
     * @deprecated  Import declarations are implementation details that
     *          should not be exposed here.  In addition, this method's
     *          return type does not allow for all type-import-on-demand
     *          declarations to be returned.
     */
@Deprecated
public PackageDoc[] importedPackages() {
    // information is not available for binary classfiles
    if (tsym.sourcefile == null)
        return new PackageDoc[0];
    ListBuffer<PackageDocImpl> importedPackages = new ListBuffer<PackageDocImpl>();
    //### Add the implicit "import java.lang.*" to the result
    Names names = tsym.name.table.names;
    importedPackages.append(env.getPackageDoc(env.reader.enterPackage(names.java_lang)));
    Env<AttrContext> compenv = env.enter.getEnv(tsym);
    if (compenv == null)
        return new PackageDocImpl[0];
    for (JCTree t : compenv.toplevel.defs) {
        if (t.getTag() == JCTree.IMPORT) {
            JCTree imp = ((JCImport) t).qualid;
            if (TreeInfo.name(imp) == names.asterisk) {
                JCFieldAccess sel = (JCFieldAccess) imp;
                Symbol s = sel.selected.type.tsym;
                PackageDocImpl pdoc = env.getPackageDoc(s.packge());
                if (!importedPackages.contains(pdoc))
                    importedPackages.append(pdoc);
            }
        }
    }
    return importedPackages.toArray(new PackageDocImpl[importedPackages.length()]);
}
Also used : Names(com.sun.tools.javac.util.Names) JCImport(com.sun.tools.javac.tree.JCTree.JCImport) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) Symbol(com.sun.tools.javac.code.Symbol) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCTree(com.sun.tools.javac.tree.JCTree) AttrContext(com.sun.tools.javac.comp.AttrContext)

Example 4 with JCImport

use of com.sun.tools.javac.tree.JCTree.JCImport in project error-prone by google.

the class ImportStatementsTest method stubImport.

/**
   * A helper method to create a JCImport stub.
   *
   * @param typeName the fully-qualified name of the type being imported
   * @param isStatic whether the import is static
   * @param startPos the start position of the import statement
   * @param endPos the end position of the import statement
   * @return a new JCImport stub
   */
private static JCImport stubImport(String typeName, boolean isStatic, int startPos, int endPos) {
    JCImport result = mock(JCImport.class);
    when(result.isStatic()).thenReturn(isStatic);
    when(result.getStartPosition()).thenReturn(startPos);
    when(result.getEndPosition(any(EndPosTable.class))).thenReturn(endPos);
    // craft import string
    StringBuilder returnSB = new StringBuilder("import ");
    if (isStatic) {
        returnSB.append("static ");
    }
    returnSB.append(typeName);
    returnSB.append(";\n");
    when(result.toString()).thenReturn(returnSB.toString());
    return result;
}
Also used : JCImport(com.sun.tools.javac.tree.JCTree.JCImport) EndPosTable(com.sun.tools.javac.tree.EndPosTable)

Example 5 with JCImport

use of com.sun.tools.javac.tree.JCTree.JCImport in project lombok by rzwitserloot.

the class JavacHandlerUtil method deleteImportFromCompilationUnit.

public static void deleteImportFromCompilationUnit(JavacNode node, String name) {
    if (inNetbeansEditor(node))
        return;
    if (!node.shouldDeleteLombokAnnotations())
        return;
    ListBuffer<JCTree> newDefs = new ListBuffer<JCTree>();
    JCCompilationUnit unit = (JCCompilationUnit) node.top().get();
    for (JCTree def : unit.defs) {
        boolean delete = false;
        if (def instanceof JCImport) {
            JCImport imp0rt = (JCImport) def;
            delete = (!imp0rt.staticImport && imp0rt.qualid.toString().equals(name));
        }
        if (!delete)
            newDefs.append(def);
    }
    unit.defs = newDefs.toList();
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JCImport(com.sun.tools.javac.tree.JCTree.JCImport) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCTree(com.sun.tools.javac.tree.JCTree)

Aggregations

JCImport (com.sun.tools.javac.tree.JCTree.JCImport)9 JCTree (com.sun.tools.javac.tree.JCTree)8 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)5 ListBuffer (com.sun.tools.javac.util.ListBuffer)3 AttrContext (com.sun.tools.javac.comp.AttrContext)2 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)2 Symbol (com.sun.tools.javac.code.Symbol)1 EndPosTable (com.sun.tools.javac.tree.EndPosTable)1 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)1 JCArrayAccess (com.sun.tools.javac.tree.JCTree.JCArrayAccess)1 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)1 JCAssert (com.sun.tools.javac.tree.JCTree.JCAssert)1 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)1 JCAssignOp (com.sun.tools.javac.tree.JCTree.JCAssignOp)1 JCBinary (com.sun.tools.javac.tree.JCTree.JCBinary)1 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)1 JCBreak (com.sun.tools.javac.tree.JCTree.JCBreak)1 JCCase (com.sun.tools.javac.tree.JCTree.JCCase)1 JCCatch (com.sun.tools.javac.tree.JCTree.JCCatch)1 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)1