Java Code Examples for com.sun.source.tree.CompilationUnitTree#getTypeDecls()
The following examples show how to use
com.sun.source.tree.CompilationUnitTree#getTypeDecls() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaSourceHelper.java From netbeans with Apache License 2.0 | 6 votes |
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; }
Example 2
Source File: InterceptorGenerator.java From netbeans with Apache License 2.0 | 6 votes |
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; }
Example 3
Source File: JavaSourceHelper.java From netbeans with Apache License 2.0 | 6 votes |
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; }
Example 4
Source File: JUnitTestUtil.java From netbeans with Apache License 2.0 | 6 votes |
/** * Finds a main class. * * @param compInfo defines scope in which the class is to be found * @param className name of the class to be found * @return the found class; or <code>null</code> if the class was not * found (e.g. because of a broken source file) */ public static ClassTree findMainClass(final CompilationInfo compInfo) { final String className = compInfo.getFileObject().getName(); CompilationUnitTree compUnitTree = compInfo.getCompilationUnit(); String shortClassName = getSimpleName(className); for (Tree typeDecl : compUnitTree.getTypeDecls()) { if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) { ClassTree clazz = (ClassTree) typeDecl; if (clazz.getSimpleName().toString().equals(shortClassName)) { return clazz; } } } return null; }
Example 5
Source File: PackagetoTreePathHandleTask.java From netbeans with Apache License 2.0 | 6 votes |
@Override public void run(CompilationController info) throws Exception { //TODO:Should this be a WeakReference? info.toPhase(Phase.ELEMENTS_RESOLVED); cinfo = info; CompilationUnitTree unit = info.getCompilationUnit(); for (Tree tree : unit.getTypeDecls()) { Element element = info.getTrees().getElement(TreePath.getPath(unit, tree)); if (element == null || !(element.getKind().isClass() || element.getKind().isInterface())) { // syntax errors #111195 continue; } //TODO:Revisit this check if (!element.getModifiers().contains(Modifier.PRIVATE)) { TreePathHandle typeHandle = TreePathHandle.create(TreePath.getPath(unit, tree), info); handles.add(typeHandle); } } }
Example 6
Source File: TreeAnalyzer.java From meghanada-server with GNU General Public License v3.0 | 5 votes |
private static void analyzeCompilationUnitTree(SourceContext context, CompilationUnitTree cut) { Source src = context.source; log.trace("file={}", src.getFile()); EndPosTable endPosTable = ((JCTree.JCCompilationUnit) cut).endPositions; context.endPosTable = endPosTable; analyzePackageAnnotations(cut, src, endPosTable); analyzePackageName(cut, src, endPosTable); analyzeImports(cut, src, endPosTable); try { for (Tree td : cut.getTypeDecls()) { // start class if (td instanceof JCTree.JCClassDecl) { analyzeTopLevelClass(context, (JCTree.JCClassDecl) td); } else if (td instanceof JCTree.JCSkip) { // skip } else if (td instanceof JCTree.JCErroneous) { // skip erroneous } else { log.warn("unknown td={} {}", td, td.getClass()); } } } catch (IOException e) { log.catching(e); throw new UncheckedIOException(e); } }
Example 7
Source File: SrcFinder.java From netbeans with Apache License 2.0 | 5 votes |
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; }
Example 8
Source File: ParserTest.java From netbeans with Apache License 2.0 | 5 votes |
private void dumpStructure(JavaSource js, final PrintStream ps, final String text) throws IOException { CancellableTask task = new CancellableTask<CompilationController>() { ElementVisitor scanner; public void cancel() { scanner.cancel(); } public void run(CompilationController parameter) throws Exception { parameter.toPhase(Phase.RESOLVED); CompilationUnitTree cuTree = parameter.getCompilationUnit(); List<? extends Tree> typeDecls = cuTree.getTypeDecls(); TreePath cuPath = new TreePath(cuTree); List<Element> elements = new ArrayList<Element>(typeDecls.size()); for (Tree t : typeDecls) { TreePath p = new TreePath(cuPath, t); Element e = parameter.getTrees().getElement(p); if (e != null) { elements.add(e); } } scanner = new ElementVisitor(parameter, text); for (Element element : elements) { scanner.scan(element, ps); } } }; js.runModificationTask(task).commit(); }
Example 9
Source File: ModuleNames.java From netbeans with Apache License 2.0 | 5 votes |
@CheckForNull public static String parseModuleName( @NonNull final FileObject moduleInfo) { final JavacTaskImpl jt = JavacParser.createJavacTask( new ClasspathInfo.Builder(ClassPath.EMPTY).build(), null, "1.3", //min sl to prevent validateSourceLevel warning null, null, null, null, null, Collections.singletonList(FileObjects.fileObjectFileObject( moduleInfo, moduleInfo.getParent(), null, FileEncodingQuery.getEncoding(moduleInfo)))); final CompilationUnitTree cu = jt.parse().iterator().next(); final List<? extends Tree> typeDecls = cu.getTypeDecls(); if (!typeDecls.isEmpty()) { final Tree typeDecl = typeDecls.get(0); if (typeDecl.getKind() == Tree.Kind.MODULE) { return ((ModuleTree)typeDecl).getName().toString(); } } return null; }
Example 10
Source File: ClassImplementsTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testRemoveFirstTwo() 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 Serializable {\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, 0); 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); }
Example 11
Source File: ClassImplementsTest.java From netbeans with Apache License 2.0 | 5 votes |
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); }
Example 12
Source File: ClassImplementsTest.java From netbeans with Apache License 2.0 | 5 votes |
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); }
Example 13
Source File: RewriteMultipleExpressionsTest.java From netbeans with Apache License 2.0 | 4 votes |
@Test public void testRewriteMultipleExpressions() throws Exception { File testFile = new File(getWorkDir(), "Test.java"); TestUtilities.copyStringToFile(testFile, "\n" + "public class MultipleExpressionsTest {\n" + " public void testMethod() {\n" + " printGreeting();\n" + " printGreeting();\n" + " printGreeting();\n" + " }\n" + " public void printGreeting() {\n" + " System.out.println(\"Hello World!\");\n" + " }\n" + "}\n"); String golden = "\n" + "public class MultipleExpressionsTest {\n" + " public void testMethod() {\n" + " System.out.println(\"Hello World!\");\n" + " System.out.println(\"Hello World!\");\n" + " System.out.println(\"Hello World!\");\n" + " }\n" + " public void printGreeting() {\n" + " System.out.println(\"Hello World!\");\n" + " }\n" + "}\n"; JavaSource src = getJavaSource(testFile); Task<WorkingCopy> task = new Task<WorkingCopy>() { public void run(WorkingCopy workingCopy) throws Exception { workingCopy.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cut = workingCopy.getCompilationUnit(); TreeMaker make = workingCopy.getTreeMaker(); List<? extends Tree> classes = cut.getTypeDecls(); ClassTree clazz = (ClassTree) classes.get(0); List<? extends Tree> trees = clazz.getMembers(); MethodTree testMethod = (MethodTree) trees.get(1); BlockTree body = testMethod.getBody(); MethodTree printMethod = (MethodTree) trees.get(2); BlockTree printBody = printMethod.getBody(); List<StatementTree> statements = new LinkedList<StatementTree>(); statements.add(printBody.getStatements().get(0)); statements.add(printBody.getStatements().get(0)); statements.add(printBody.getStatements().get(0)); BlockTree modified = make.Block(statements, false); workingCopy.rewrite(body, modified); } }; src.runModificationTask(task).commit(); String res = TestUtilities.copyFileToString(testFile); System.out.println(res); assertEquals(golden, res); }
Example 14
Source File: MethodThrowsTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testAddFirstTwo() 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, FileNotFoundException {\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") ); copy = make.addMethodThrows( copy, make.Identifier("FileNotFoundException") ); workingCopy.rewrite(method, copy); } } }; src.runModificationTask(task).commit(); String res = TestUtilities.copyFileToString(testFile); assertEquals(golden, res); }
Example 15
Source File: MethodThrowsTest.java From netbeans with Apache License 2.0 | 4 votes |
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); }
Example 16
Source File: ClassImplementsTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testRemoveAllImplExtends() 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 {\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, 0 ); workingCopy.rewrite(clazz, copy); } } }; src.runModificationTask(task).commit(); String res = TestUtilities.copyFileToString(testFile); //System.err.println(res); assertEquals(golden, res); }
Example 17
Source File: ClassImplementsTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testAddFirstImplExtends() 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 {\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 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); }
Example 18
Source File: ClassImplementsTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testAddFirstTwo() 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 extends Object {\n" + " public void taragui() {\n" + " }\n" + "}\n" ); String golden = "package hierbas.del.litoral;\n\n" + "import java.io.*;\n\n" + "public class Test extends Object implements Collection, 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("Collection") ); copy = make.addClassImplementsClause( copy, make.Identifier("List") ); workingCopy.rewrite(clazz, copy); } } }; src.runModificationTask(task).commit(); String res = TestUtilities.copyFileToString(testFile); //System.err.println(res); assertEquals(golden, res); }
Example 19
Source File: J2eeEntityResourcesGenerator.java From netbeans with Apache License 2.0 | 4 votes |
private String generateEntityManagerFactoryAccess( FileObject fileObject ) throws IOException { final StringBuilder bodyText = new StringBuilder( "{return (EntityManagerFactory) new "); // NOI18N bodyText.append("InitialContext().lookup(\"java:comp/env/");// NOI18N bodyText.append( WebXmlHelper.PERSISTENCE_FACTORY); bodyText.append( "\");"); // NOI18N final String entityManagerMethod = "getEntityManagerFactory";// NOI18N JavaSource javaSource = JavaSource.forFileObject(fileObject); Task<WorkingCopy> task = new Task<WorkingCopy>() { public void run(WorkingCopy workingCopy) throws Exception { workingCopy.toPhase(Phase.RESOLVED); CompilationUnitTree tree = workingCopy.getCompilationUnit(); TreeMaker maker = workingCopy.getTreeMaker(); ExpressionTree exceptionTree = JavaSourceHelper.createTypeTree(workingCopy, "javax.naming.NamingException"); // NOI18N Tree returnTypeTree = JavaSourceHelper.createTypeTree(workingCopy, "javax.persistence.EntityManagerFactory"); // NOI18N ModifiersTree modifiersTree = JavaSourceHelper.createModifiersTree( workingCopy,new Modifier[]{Modifier.PRIVATE} , null, null); MethodTree methodTree = maker.Method(modifiersTree, entityManagerMethod, returnTypeTree, Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), Collections.singletonList( exceptionTree ), bodyText.toString(), null); for (Tree typeDeclaration : tree.getTypeDecls()){ if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDeclaration.getKind())){ ClassTree classTree = (ClassTree) typeDeclaration; ClassTree newTree = maker.addClassMember(classTree, methodTree); workingCopy.rewrite(classTree, newTree); } } } }; javaSource.runModificationTask(task).commit(); return entityManagerMethod; }
Example 20
Source File: ClassImplementsTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testRemoveAll() 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 implements Serializable, List, Collection {\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() {\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, 0); copy = make.removeClassImplementsClause(copy, 0); 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); }