当前位置: 首页>>代码示例>>Java>>正文


Java ImportTree类代码示例

本文整理汇总了Java中com.sun.source.tree.ImportTree的典型用法代码示例。如果您正苦于以下问题:Java ImportTree类的具体用法?Java ImportTree怎么用?Java ImportTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


ImportTree类属于com.sun.source.tree包,在下文中一共展示了ImportTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createImport

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
public static CompilationUnitTree createImport(WorkingCopy wc, CompilationUnitTree modifiedCut, String fq) {
    if (modifiedCut == null) {
        modifiedCut = wc.getCompilationUnit();  //use committed cut as modifiedCut
    }
    List<? extends ImportTree> imports = modifiedCut.getImports();
    boolean found = false;
    for (ImportTree imp : imports) {
       if (fq.equals(imp.getQualifiedIdentifier().toString())) {
           found = true; 
           break;
       }
    }
    if (!found) {
        TreeMaker make = wc.getTreeMaker();
        CompilationUnitTree newCut = make.addCompUnitImport(
            modifiedCut, 
            make.Import(make.Identifier(fq), false)
        );                                              //create a newCut from modifiedCut
        wc.rewrite(wc.getCompilationUnit(), newCut);    //replace committed cut with newCut in change map
        return newCut;                                  //return the newCut we just created
    }
    return modifiedCut; //no newCut created from modifiedCut, so just return modifiedCut
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:24,代码来源:JpaControllerUtil.java

示例2: testAddFirstImport

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
public void testAddFirstImport() throws IOException, FileStateInvalidException {
    System.err.println("testAddFirstImport");
    JavaSource src = getJavaSource(testFile);
    
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            List<ImportTree> imports = new ArrayList<ImportTree>(cut.getImports());
            imports.add(0, make.Import(make.Identifier("java.util.AbstractList"), false));
            CompilationUnitTree unit = make.CompilationUnit(
                    cut.getPackageName(),
                    imports,
                    cut.getTypeDecls(),
                    cut.getSourceFile()
            );
            workingCopy.rewrite(cut, unit);
        }
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertFiles("testAddFirstImport_ImportFormatTest.pass");
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:27,代码来源:ImportFormatTest.java

示例3: testAddLastImport

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
public void testAddLastImport() throws IOException, FileStateInvalidException {
    JavaSource src = getJavaSource(testFile);
    
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            List<ImportTree> imports = new ArrayList<ImportTree>(cut.getImports());
            imports.add(make.Import(make.Identifier("java.io.IOException"), false));
            CompilationUnitTree unit = make.CompilationUnit(
                    cut.getPackageName(),
                    imports,
                    cut.getTypeDecls(),
                    cut.getSourceFile()
            );
            workingCopy.rewrite(cut, unit);
        }
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertFiles("testAddLastImport_ImportFormatTest.pass");
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:26,代码来源:ImportFormatTest.java

示例4: testRemoveInnerImport

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
public void testRemoveInnerImport() throws IOException, FileStateInvalidException {
    JavaSource src = getJavaSource(testFile);
    
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            List<ImportTree> imports = new ArrayList<ImportTree>(cut.getImports());
            imports.remove(1);
            CompilationUnitTree unit = make.CompilationUnit(
                    cut.getPackageName(),
                    imports,
                    cut.getTypeDecls(),
                    cut.getSourceFile()
            );
            workingCopy.rewrite(cut, unit);
        }
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertFiles("testRemoveInnerImport_ImportFormatTest.pass");
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:26,代码来源:ImportFormatTest.java

示例5: testRemoveFirstImport

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
public void testRemoveFirstImport() throws IOException, FileStateInvalidException {
    JavaSource src = getJavaSource(testFile);
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            List<ImportTree> imports = new ArrayList<ImportTree>(cut.getImports());
            imports.remove(0);
            CompilationUnitTree unit = make.CompilationUnit(
                    cut.getPackageName(),
                    imports,
                    cut.getTypeDecls(),
                    cut.getSourceFile()
            );
            workingCopy.rewrite(cut, unit);
        }
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertFiles("testRemoveFirstImport_ImportFormatTest.pass");
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:25,代码来源:ImportFormatTest.java

示例6: testRemoveLastImport

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
public void testRemoveLastImport() throws IOException, FileStateInvalidException {
    JavaSource src = getJavaSource(testFile);
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            TreeMaker make = workingCopy.getTreeMaker();
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            List<ImportTree> imports = new ArrayList<ImportTree>(cut.getImports());
            imports.remove(1);
            CompilationUnitTree unit = make.CompilationUnit(
                    cut.getPackageName(),
                    imports,
                    cut.getTypeDecls(),
                    cut.getSourceFile()
            );
            workingCopy.rewrite(cut, unit);
        }
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertFiles("testRemoveLastImport_ImportFormatTest.pass");
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:25,代码来源:ImportFormatTest.java

示例7: testRemoveInside

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
public void testRemoveInside() throws IOException, FileStateInvalidException {
    JavaSource src = getJavaSource(testFile);
    
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            List<ImportTree> imports = new ArrayList<ImportTree>(cut.getImports());
            imports.remove(4);
            CompilationUnitTree unit = make.CompilationUnit(
                    cut.getPackageName(),
                    imports,
                    cut.getTypeDecls(),
                    cut.getSourceFile()
            );
            workingCopy.rewrite(cut, unit);
        }
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertFiles("testRemoveInside_ImportFormatTest.pass");
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:26,代码来源:ImportFormatTest.java

示例8: testMoveFirst

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
public void testMoveFirst() throws IOException, FileStateInvalidException {
    JavaSource src = getJavaSource(testFile);
    
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            List<ImportTree> imports = new ArrayList<ImportTree>(cut.getImports());
            ImportTree oneImport = imports.remove(0);
            imports.add(3, oneImport);
            CompilationUnitTree unit = make.CompilationUnit(
                    cut.getPackageName(),
                    imports,
                    cut.getTypeDecls(),
                    cut.getSourceFile()
            );
            workingCopy.rewrite(cut, unit);
        }
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertFiles("testMoveFirst_ImportFormatTest.pass");
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:27,代码来源:ImportFormatTest.java

示例9: testMoveLast

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
public void testMoveLast() throws IOException, FileStateInvalidException {
    JavaSource src = getJavaSource(testFile);
    
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            List<ImportTree> imports = new ArrayList<ImportTree>(cut.getImports());
            ImportTree oneImport = imports.remove(7);
            imports.add(1, oneImport);
            CompilationUnitTree unit = make.CompilationUnit(
                    cut.getPackageName(),
                    imports,
                    cut.getTypeDecls(),
                    cut.getSourceFile()
            );
            workingCopy.rewrite(cut, unit);
        }
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertFiles("testMoveLast_ImportFormatTest.pass");
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:27,代码来源:ImportFormatTest.java

示例10: testReplaceLine

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
public void testReplaceLine() throws IOException, FileStateInvalidException {
    JavaSource src = getJavaSource(testFile);
    
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            List<ImportTree> imports = new ArrayList<ImportTree>(cut.getImports());
            ImportTree oneImport = imports.remove(4);
            imports.add(4, make.Import(make.Identifier("java.util.Collection"), false));
            CompilationUnitTree unit = make.CompilationUnit(
                    cut.getPackageName(),
                    imports,
                    cut.getTypeDecls(),
                    cut.getSourceFile()
            );
            workingCopy.rewrite(cut, unit);
        }
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertFiles("testReplaceLine_ImportFormatTest.pass");
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:27,代码来源:ImportFormatTest.java

示例11: visitImport

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
@Override
public Void visitImport(ImportTree node, Stack<Tree> p) {
    if (node.isStatic() && toFind.getModifiers().contains(Modifier.STATIC)) {
        Tree qualIdent = node.getQualifiedIdentifier();
        if (qualIdent.getKind() == Kind.MEMBER_SELECT) {
            MemberSelectTree mst = (MemberSelectTree) qualIdent;
            if (toFind.getSimpleName().contentEquals(mst.getIdentifier())) {
                Element el = info.getTrees().getElement(new TreePath(getCurrentPath(), mst.getExpression()));
                if (el != null && el.equals(toFind.getEnclosingElement())) {
                    Token<JavaTokenId> t = Utilities.getToken(info, doc, new TreePath(getCurrentPath(), mst));
                    if (t != null)
                        usages.add(t);
                }
            }
        }
    }
    return super.visitImport(node, p);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:19,代码来源:FindLocalUsagesQuery.java

示例12: typeUsed

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
private void typeUsed(Element decl, TreePath expr, boolean methodInvocation) {
    if (decl != null && (expr == null || expr.getLeaf().getKind() == Kind.IDENTIFIER || expr.getLeaf().getKind() == Kind.PARAMETERIZED_TYPE)) {
        if (!isErroneous(decl)) {
            ImportTree imp = element2Import.get(decl);

            if (imp != null) {
                addUsage(imp);
                if (isStar(imp)) {
                    //TODO: explain
                    handleUnresolvableImports(decl, methodInvocation, false);
                }
            }
        } else {
            handleUnresolvableImports(decl, methodInvocation, true);
            
            for (Entry<Element, ImportTree> e : element2Import.entrySet()) {
                if (importedBySingleImport.contains(e.getKey())) continue;
                
                if (e.getKey().getSimpleName().equals(decl.getSimpleName())) {
                    import2Highlight.remove(e.getValue());
                }
            }
        }
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:26,代码来源:UnusedImports.java

示例13: handleUnresolvableImports

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
private void handleUnresolvableImports(Element decl,
        boolean methodInvocation, boolean removeStarImports) {
    Name simpleName = decl.getSimpleName();
    if (simpleName != null) {
        Collection<ImportTree> imps = simpleName2UnresolvableImports.get(simpleName.toString());

        if (imps != null) {
            for (ImportTree imp : imps) {
                if (!methodInvocation || imp.isStatic()) {
                    import2Highlight.remove(imp);
                }
            }
        } else {
            if (removeStarImports) {
                //TODO: explain
                for (ImportTree unresolvable : unresolvablePackageImports) {
                    if (!methodInvocation || unresolvable.isStatic()) {
                        import2Highlight.remove(unresolvable);
                    }
                }
            }
        }
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:25,代码来源:UnusedImports.java

示例14: visitImport

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
@Override
public Void visitImport(ImportTree node, Void p) {
    if (node.isStatic() && toFind.getModifiers().contains(Modifier.STATIC)) {
        Tree qualIdent = node.getQualifiedIdentifier();
        if (qualIdent.getKind() == Kind.MEMBER_SELECT) {
            MemberSelectTree mst = (MemberSelectTree) qualIdent;
            if (toFind.getSimpleName().contentEquals(mst.getIdentifier())) {
                Element el = info.getTrees().getElement(new TreePath(getCurrentPath(), mst.getExpression()));
                if (el != null && el.equals(toFind.getEnclosingElement())) {
                    try {
                        int[] span = treeUtils.findNameSpan(mst);
                        if(span != null) {
                            MutablePositionRegion region = createRegion(doc, span[0], span[1]);
                            usages.add(region);
                        }
                    } catch (BadLocationException ex) {
                        Exceptions.printStackTrace(ex);
                    }
                }
            }
        }
    }
    return super.visitImport(node, p);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:25,代码来源:FindLocalUsagesQuery.java

示例15: exlucded

import com.sun.source.tree.ImportTree; //导入依赖的package包/类
@Hint(displayName = "#DN_Imports_EXCLUDED", description = "#DESC_Imports_EXCLUDED", category="imports", id="Imports_EXCLUDED", options=Options.QUERY)
@TriggerTreeKind(Kind.IMPORT)
public static ErrorDescription exlucded(HintContext ctx) throws IOException {
    ImportTree it = (ImportTree) ctx.getPath().getLeaf();

    if (it.isStatic() || !(it.getQualifiedIdentifier() instanceof MemberSelectTree)) {
        return null; // XXX
    }

    MemberSelectTree ms = (MemberSelectTree) it.getQualifiedIdentifier();
    String pkg = ms.getExpression().toString();
    String klass = ms.getIdentifier().toString();
    String exp = pkg + "." + (!klass.equals("*") ? klass : ""); //NOI18N
    if (Utilities.isExcluded(exp)) {
        return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), NbBundle.getMessage(Imports.class, "DN_Imports_EXCLUDED"));
    }

    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:20,代码来源:Imports.java


注:本文中的com.sun.source.tree.ImportTree类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。