本文整理汇总了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;
}
示例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");
}
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
示例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();
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
}
示例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);
}
示例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);
}
示例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);
}