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


Java CompilationUnitTree.getTypeDecls方法代码示例

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


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

示例1: getTopLevelClassTree

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public static ClassTree getTopLevelClassTree(CompilationController controller) {
    String className = controller.getFileObject().getName();

    CompilationUnitTree cu = controller.getCompilationUnit();
    if (cu != null) {
        List<? extends Tree> decls = cu.getTypeDecls();
        for (Tree decl : decls) {
            if (!TreeUtilities.CLASS_TREE_KINDS.contains(decl.getKind())) {
                continue;
            }

            ClassTree classTree = (ClassTree) decl;

            if (classTree.getSimpleName().contentEquals(className) && classTree.getModifiers().getFlags().contains(Modifier.PUBLIC)) {
                return classTree;
            }
        }
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:21,代码来源:JavaSourceHelper.java

示例2: parse

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
static List<? extends Tree> parse(String srcfile) throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjects(srcfile);
    String classPath = System.getProperty("java.class.path");
    List<String> options = Arrays.asList("-classpath", classPath);
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    Context context = new Context();
    JavacTaskImpl task = (JavacTaskImpl) ((JavacTool) compiler).getTask(null, null,
            diagnostics, options, null, fileObjects, context);
    TrialParserFactory.instance(context);
    Iterable<? extends CompilationUnitTree> asts = task.parse();
    Iterator<? extends CompilationUnitTree> it = asts.iterator();
    if (it.hasNext()) {
        CompilationUnitTree cut = it.next();
        return cut.getTypeDecls();
    } else {
        throw new AssertionError("Expected compilation unit");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:JavacExtensionTest.java

示例3: generate

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
/**
 * Generates the code needed for retrieving and invoking
 * an instance of <code>javax.persistence.EntityManager</code>. The generated 
 * code depends on the given <code>strategyClass</code>. 
 * 
 * @param options the options for the generation. Must not be null.
 * @param strategyClass the generation strategy that should be used. Must not be null.
 * @return the modified file object of the target java class.
 */
public FileObject generate(final GenerationOptions options, 
        final Class<? extends EntityManagerGenerationStrategy> strategyClass) throws IOException{

    Parameters.notNull("options", options); //NOI18N
    Parameters.notNull("strategyClass", strategyClass); //NOI18N
    
    Task task = new Task<WorkingCopy>() {
        
        public void run(WorkingCopy workingCopy) throws Exception {
            
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            
            for (Tree typeDeclaration : cut.getTypeDecls()){
                if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDeclaration.getKind())){
                    ClassTree clazz = (ClassTree) typeDeclaration;
                    EntityManagerGenerationStrategy strategy = instantiateStrategy(strategyClass, workingCopy, make, clazz, options);
                    workingCopy.rewrite(clazz, strategy.generate());
                }
            }
        }
    };
    
    targetSource.runModificationTask(task).commit();
    
    return targetFo;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:38,代码来源:EntityManagerGenerator.java

示例4: generate

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
protected FileObject generate(FileObject targetFo, final GenerationOptions options) throws IOException{
    
    JavaSource targetSource = JavaSource.forFileObject(targetFo);
    
    CancellableTask task = new CancellableTask<WorkingCopy>() {
        
        public void cancel() {
        }
        
        public void run(WorkingCopy workingCopy) throws Exception {
            
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            
            for (Tree typeDeclaration : cut.getTypeDecls()){
                if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDeclaration.getKind())){
                    ClassTree clazz = (ClassTree) typeDeclaration;
                    ClassTree modifiedClazz = getStrategy(workingCopy, make, clazz, options).generate();
                    workingCopy.rewrite(clazz, modifiedClazz);
                }
            }
            
        }
    };
    targetSource.runModificationTask(task).commit();
    
    return targetFo;
    
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:31,代码来源:EntityManagerGenerationTestSupport.java

示例5: findClass

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
private static TypeElement findClass(CompilationController ctrl, String className) {
    CompilationUnitTree cunit = ctrl.getCompilationUnit();
    for (Tree declTree : cunit.getTypeDecls()) {
        ClassTree classTree = (ClassTree) declTree;
        if (className.equals(classTree.getSimpleName().toString())) {
            Trees trees = ctrl.getTrees();
            TypeElement classElm = (TypeElement) trees.getElement(trees.getPath(cunit, classTree));
            return classElm;
        }
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:13,代码来源:SrcFinder.java

示例6: testRemoveLast

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testRemoveLast() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile, 
        "package hierbas.del.litoral;\n\n" +
        "import java.util.*;\n\n" +
        "public class Test implements List, Collection, Serializable {\n" +
        "    public void taragui() {\n" +
        "    }\n" +
        "}\n"
        );
    String golden =
        "package hierbas.del.litoral;\n\n" +
        "import java.util.*;\n\n" +
        "public class Test implements List, Collection {\n" +
        "    public void taragui() {\n" +
        "    }\n" +
        "}\n";

    JavaSource src = getJavaSource(testFile);
    Task task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            for (Tree typeDecl : cut.getTypeDecls()) {
                // should check kind, here we can be sure!
                ClassTree clazz = (ClassTree) typeDecl;
                ClassTree copy = make.removeClassImplementsClause(clazz, 2);
                workingCopy.rewrite(clazz, copy);
            }
        }
        
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:40,代码来源:ClassImplementsTest.java

示例7: renameFO

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
/**
 * XXX: Change this when there will be a write model.
 * When there will be a refactoring it shoud be called only in case of handleCreateFromTemplate
 */    
static void renameFO(final FileObject fileToUpdate, 
        final String packageName, 
        final String newName, 
        final String originalName) throws IOException 
{
    JavaSource javaSource = JavaSource.forFileObject (fileToUpdate);

    Task<WorkingCopy> task = new Task<WorkingCopy>() {
        
        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            TreeMaker make = workingCopy.getTreeMaker();
            CompilationUnitTree compilationUnitTree = workingCopy.getCompilationUnit();
            // change the package when file was move to different dir.
            CompilationUnitTree cutCopy = make.CompilationUnit(
                    compilationUnitTree.getPackageAnnotations(),
                    "".equals(packageName) ? null : make.Identifier(packageName),
                    compilationUnitTree.getImports(),
                    compilationUnitTree.getTypeDecls(),
                    compilationUnitTree.getSourceFile()
            );
            workingCopy.rewrite(compilationUnitTree, cutCopy);
            // go to rename also the top level class too...
            if (originalName != null && !originalName.equals(newName)) {
                for (Tree typeDecl : compilationUnitTree.getTypeDecls()) {
                    if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) {
                        ClassTree clazz = (ClassTree) typeDecl;
                        if (originalName.contentEquals(clazz.getSimpleName())) {
                            Tree copy = make.setLabel(typeDecl, newName);
                            workingCopy.rewrite(typeDecl, copy);
                        }
                    }
                }
            }
        }                
    };
    javaSource.runModificationTask(task).commit();
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:43,代码来源:JavaDataObject.java

示例8: testRemoveMid

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testRemoveMid() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile, 
        "package hierbas.del.litoral;\n\n" +
        "import java.io.*;\n\n" +
        "public class Test {\n" +
        "    public void taragui() throws IOException, FileNotFoundException, Exception {\n" +
        "    }\n" +
        "}\n"
        );
    String golden =
        "package hierbas.del.litoral;\n\n" +
        "import java.io.*;\n\n" +
        "public class Test {\n" +
        "    public void taragui() throws IOException, Exception {\n" +
        "    }\n" +
        "}\n";

    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();

            for (Tree typeDecl : cut.getTypeDecls()) {
                // should check kind, here we can be sure!
                ClassTree clazz = (ClassTree) typeDecl;
                MethodTree method = (MethodTree) clazz.getMembers().get(1);
                MethodTree copy = make.removeMethodThrows(method, 1);
                workingCopy.rewrite(method, copy);
            }
        }

    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:41,代码来源:MethodThrowsTest.java

示例9: testRenameInImpl

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testRenameInImpl() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile, 
        "package hierbas.del.litoral;\n\n" +
        "import java.util.*;\n\n" +
        "public class Test<E> extends Object implements List {\n" +
        "    public void taragui() {\n" +
        "    }\n" +
        "}\n"
        );
    String golden =
        "package hierbas.del.litoral;\n\n" +
        "import java.util.*;\n\n" +
        "public class Test<E> extends Object implements Seznam {\n" +
        "    public void taragui() {\n" +
        "    }\n" +
        "}\n";
    
    JavaSource src = getJavaSource(testFile);
    Task task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            for (Tree typeDecl : cut.getTypeDecls()) {
                // should check kind, here we can be sure!
                ClassTree clazz = (ClassTree) typeDecl;
                IdentifierTree ident = (IdentifierTree) clazz.getImplementsClause().get(0);
                workingCopy.rewrite(ident, make.setLabel(ident, "Seznam"));
            }
        }
    
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:40,代码来源:ClassImplementsTest.java

示例10: testRemoveJust

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testRemoveJust() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile, 
        "package hierbas.del.litoral;\n\n" +
        "import java.util.*;\n\n" +
        "public class Test implements List, Collection {\n" +
        "    public void taragui() {\n" +
        "    }\n" +
        "}\n"
        );
    String golden =
        "package hierbas.del.litoral;\n\n" +
        "import java.util.*;\n\n" +
        "public class Test {\n" +
        "    public void taragui() {\n" +
        "    }\n" +
        "}\n";

    JavaSource src = getJavaSource(testFile);
    Task task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            for (Tree typeDecl : cut.getTypeDecls()) {
                // should check kind, here we can be sure!
                ClassTree clazz = (ClassTree) typeDecl;
                ClassTree copy = make.removeClassImplementsClause(clazz, 1);
                copy = make.removeClassImplementsClause(copy, 0);
                workingCopy.rewrite(clazz, copy);
            }
        }
        
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:41,代码来源:ClassImplementsTest.java

示例11: testRemoveTwoExtends

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testRemoveTwoExtends() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile, 
        "package hierbas.del.litoral;\n\n" +
        "import java.util.*;\n\n" +
        "public interface Test extends Serializable, Externalizable {\n" +
        "    public void taragui();\n" +
        "}\n"
        );
    String golden =
        "package hierbas.del.litoral;\n\n" +
        "import java.util.*;\n\n" +
        "public interface Test {\n" +
        "    public void taragui();\n" +
        "}\n";
    JavaSource src = getJavaSource(testFile);
    
    Task task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();

            for (Tree typeDecl : cut.getTypeDecls()) {
                // ensure that it is correct type declaration, i.e. class
                if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) {
                    ClassTree classTree = (ClassTree) typeDecl;
                    ClassTree copy = make.removeClassImplementsClause(classTree, 0);
                    copy = make.removeClassImplementsClause(copy, 0);
                    workingCopy.rewrite(classTree, copy);
                }
            }
        }
        
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:41,代码来源:InterfaceExtendsTest.java

示例12: visitCompilationUnit

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
@SuppressWarnings("static-method")
public void visitCompilationUnit(CompilationUnitTree node, TreeVisitor<?, ?> visitor) {
  for(Tree decl: node.getTypeDecls()) {
    if (!(decl instanceof ModuleTree)) {  // skip unnecessary nodes: imports, etc
      continue;
    }
    accept(visitor, decl);
  }
}
 
开发者ID:forax,项目名称:moduletools,代码行数:10,代码来源:JavacModuleParser.java

示例13: testAddFirstToBadFormatted

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testAddFirstToBadFormatted() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile, 
        "package hierbas.del.litoral;\n\n" +
        "import java.io.*;\n\n" +
        "public class Test {\n" +
        "    public void taragui(){\n" +
        "    }\n" +
        "}\n"
        );
    String golden =
        "package hierbas.del.litoral;\n\n" +
        "import java.io.*;\n\n" +
        "public class Test {\n" +
        "    public void taragui() throws IOException{\n" +
        "    }\n" +
        "}\n";

    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();

            for (Tree typeDecl : cut.getTypeDecls()) {
                // should check kind, here we can be sure!
                ClassTree clazz = (ClassTree) typeDecl;
                MethodTree method = (MethodTree) clazz.getMembers().get(1);
                if ("taragui".contentEquals(method.getName())) {
                    MethodTree copy = make.addMethodThrows(
                        method, make.Identifier("IOException")
                    );
                    workingCopy.rewrite(method, copy);
                }
            }
        }
        
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:46,代码来源:MethodThrowsTest.java

示例14: testAddFirst

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testAddFirst() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile, 
        "package hierbas.del.litoral;\n\n" +
        "import java.util.*;\n\n" +
        "public class Test {\n" +
        "    public void taragui() {\n" +
        "    }\n" +
        "}\n"
        );
    String golden =
        "package hierbas.del.litoral;\n\n" +
        "import java.util.*;\n\n" +
        "public class Test implements List {\n" +
        "    public void taragui() {\n" +
        "    }\n" +
        "}\n";

    JavaSource src = getJavaSource(testFile);
    Task task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            for (Tree typeDecl : cut.getTypeDecls()) {
                // should check kind, here we can be sure!
                ClassTree clazz = (ClassTree) typeDecl;
                ClassTree copy = make.addClassImplementsClause(
                    clazz, make.Identifier("List")
                );
                workingCopy.rewrite(clazz, copy);
            }
        }
        
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:42,代码来源:ClassImplementsTest.java

示例15: testAddFirst2

import com.sun.source.tree.CompilationUnitTree; //导入方法依赖的package包/类
public void testAddFirst2() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile, 
        "package hierbas.del.litoral;\n" +
        "\n" +
        "import java.io.*;\n" +
        "\n" +
        "public class Test {\n" +
        "    public void taragui()\n" +
        "    {\n" +
        "    }\n" +
        "}\n"
        );
    String golden =
        "package hierbas.del.litoral;\n" +
        "\n" +
        "import java.io.*;\n" +
        "\n" +
        "public class Test {\n" +
        "    public void taragui() throws IOException\n" +
        "    {\n" +
        "    }\n" +
        "}\n";

    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();

            for (Tree typeDecl : cut.getTypeDecls()) {
                // should check kind, here we can be sure!
                ClassTree clazz = (ClassTree) typeDecl;
                MethodTree method = (MethodTree) clazz.getMembers().get(1);
                MethodTree copy = make.addMethodThrows(
                    method, make.Identifier("IOException")
                );
                workingCopy.rewrite(method, copy);
            }
        }

    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:50,代码来源:MethodThrowsTest.java


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