Search in sources :

Example 6 with ImportTree

use of com.sun.source.tree.ImportTree in project error-prone by google.

the class WildcardImport method createFix.

/**
 * Creates a {@link Fix} that replaces wildcard imports.
 */
static Fix createFix(ImmutableList<ImportTree> wildcardImports, Set<TypeToImport> typesToImport, VisitorState state) {
    Map<Symbol, List<TypeToImport>> toFix = typesToImport.stream().collect(Collectors.groupingBy(TypeToImport::owner));
    final SuggestedFix.Builder fix = SuggestedFix.builder();
    for (ImportTree importToDelete : wildcardImports) {
        String importSpecification = importToDelete.getQualifiedIdentifier().toString();
        if (importToDelete.isStatic()) {
            fix.removeStaticImport(importSpecification);
        } else {
            fix.removeImport(importSpecification);
        }
    }
    for (Map.Entry<Symbol, List<TypeToImport>> entry : toFix.entrySet()) {
        final Symbol owner = entry.getKey();
        if (entry.getValue().size() > MAX_MEMBER_IMPORTS) {
            qualifiedNameFix(fix, owner, state);
        } else {
            for (TypeToImport toImport : entry.getValue()) {
                toImport.addFix(fix);
            }
        }
    }
    return fix.build();
}
Also used : SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Symbol(com.sun.tools.javac.code.Symbol) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ImportTree(com.sun.source.tree.ImportTree) Map(java.util.Map)

Example 7 with ImportTree

use of com.sun.source.tree.ImportTree in project error-prone by google.

the class WildcardImport method getWildcardImports.

/**
 * Collect all on demand imports.
 */
private static ImmutableList<ImportTree> getWildcardImports(List<? extends ImportTree> imports) {
    ImmutableList.Builder<ImportTree> result = ImmutableList.builder();
    for (ImportTree tree : imports) {
        // javac represents on-demand imports as a member select where the selected name is '*'.
        Tree ident = tree.getQualifiedIdentifier();
        if (!(ident instanceof MemberSelectTree)) {
            continue;
        }
        MemberSelectTree select = (MemberSelectTree) ident;
        if (select.getIdentifier().contentEquals("*")) {
            result.add(tree);
        }
    }
    return result.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) MemberSelectTree(com.sun.source.tree.MemberSelectTree) IdentifierTree(com.sun.source.tree.IdentifierTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) CaseTree(com.sun.source.tree.CaseTree) JCTree(com.sun.tools.javac.tree.JCTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) ImportTree(com.sun.source.tree.ImportTree)

Example 8 with ImportTree

use of com.sun.source.tree.ImportTree in project ceylon by eclipse.

the class T6963934 method main.

public static void main(String[] args) throws Exception {
    File testSrc = new File(System.getProperty("test.src"));
    File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjects(thisSrc));
    CompilationUnitTree tree = task.parse().iterator().next();
    int count = 0;
    for (ImportTree importTree : tree.getImports()) {
        System.out.println(importTree);
        count++;
    }
    int expected = 7;
    if (count != expected)
        throw new Exception("unexpected number of imports found: " + count + ", expected: " + expected);
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File) ImportTree(com.sun.source.tree.ImportTree)

Example 9 with ImportTree

use of com.sun.source.tree.ImportTree in project ceylon-compiler by ceylon.

the class T6963934 method main.

public static void main(String[] args) throws Exception {
    File testSrc = new File(System.getProperty("test.src"));
    File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjects(thisSrc));
    CompilationUnitTree tree = task.parse().iterator().next();
    int count = 0;
    for (ImportTree importTree : tree.getImports()) {
        System.out.println(importTree);
        count++;
    }
    int expected = 7;
    if (count != expected)
        throw new Exception("unexpected number of imports found: " + count + ", expected: " + expected);
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File) ImportTree(com.sun.source.tree.ImportTree)

Aggregations

ImportTree (com.sun.source.tree.ImportTree)9 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)5 ImmutableList (com.google.common.collect.ImmutableList)3 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)3 Tree (com.sun.source.tree.Tree)3 Symbol (com.sun.tools.javac.code.Symbol)3 IdentifierTree (com.sun.source.tree.IdentifierTree)2 MemberSelectTree (com.sun.source.tree.MemberSelectTree)2 VariableTree (com.sun.source.tree.VariableTree)2 JavacTask (com.sun.source.util.JavacTask)2 File (java.io.File)2 List (java.util.List)2 JavaCompiler (javax.tools.JavaCompiler)2 StandardJavaFileManager (javax.tools.StandardJavaFileManager)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets (com.google.common.collect.Sets)1 VisitorState (com.google.errorprone.VisitorState)1 Fix (com.google.errorprone.fixes.Fix)1 ASTHelpers.getSymbol (com.google.errorprone.util.ASTHelpers.getSymbol)1 BlockTree (com.sun.source.tree.BlockTree)1