Java Code Examples for com.sun.source.util.Trees#instance()
The following examples show how to use
com.sun.source.util.Trees#instance() .
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: Processor.java From EasyMPermission with MIT License | 6 votes |
/** {@inheritDoc} */ @Override public void init(ProcessingEnvironment procEnv) { super.init(procEnv); if (System.getProperty("lombok.disable") != null) { lombokDisabled = true; return; } this.processingEnv = (JavacProcessingEnvironment) procEnv; placePostCompileAndDontMakeForceRoundDummiesHook(); transformer = new JavacTransformer(procEnv.getMessager()); trees = Trees.instance(procEnv); SortedSet<Long> p = transformer.getPriorities(); if (p.isEmpty()) { this.priorityLevels = new long[] {0L}; this.priorityLevelsRequiringResolutionReset = new HashSet<Long>(); } else { this.priorityLevels = new long[p.size()]; int i = 0; for (Long prio : p) this.priorityLevels[i++] = prio; this.priorityLevelsRequiringResolutionReset = transformer.getPrioritiesRequiringResolutionReset(); } }
Example 2
Source File: JavacParserTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
@Test void testStartPositionForMethodWithoutModifiers() throws IOException { String code = "package t; class Test { <T> void t() {} }"; JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, null, Arrays.asList(new MyFileObject(code))); CompilationUnitTree cut = ct.parse().iterator().next(); ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); MethodTree mt = (MethodTree) clazz.getMembers().get(0); Trees t = Trees.instance(ct); int start = (int) t.getSourcePositions().getStartPosition(cut, mt); int end = (int) t.getSourcePositions().getEndPosition(cut, mt); assertEquals("testStartPositionForMethodWithoutModifiers", "<T> void t() {}", code.substring(start, end)); }
Example 3
Source File: ModelChecker.java From hottub with GNU General Public License v2.0 | 5 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { if (roundEnv.processingOver()) return true; Trees trees = Trees.instance(processingEnv); TypeElement testAnno = elements.getTypeElement("Check"); for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) { TreePath p = trees.getPath(elem); new MulticatchParamTester(trees).scan(p, null); } return true; }
Example 4
Source File: ScopeTest.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
private static void verifyLambdaScopeCorrect(final String packageClause) throws Exception { JavacTool tool = JavacTool.create(); JavaFileObject source = new SimpleJavaFileObject(URI.create("mem://Test.java"), Kind.SOURCE) { @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { return packageClause + SOURCE_CODE; } @Override public boolean isNameCompatible(String simpleName, Kind kind) { return true; } }; Iterable<? extends JavaFileObject> fos = Collections.singletonList(source); JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, fos); final Types types = JavacTypes.instance(((JavacTaskImpl) task).getContext()); final Trees trees = Trees.instance(task); CompilationUnitTree cu = task.parse().iterator().next(); task.analyze(); new TreePathScanner<Void, Void>() { @Override public Void visitMemberSelect(MemberSelectTree node, Void p) { if (node.getIdentifier().contentEquals("correct")) { TypeMirror xType = trees.getTypeMirror(new TreePath(getCurrentPath(), node.getExpression())); Scope scope = trees.getScope(getCurrentPath()); for (Element l : scope.getLocalElements()) { if (!l.getSimpleName().contentEquals("x")) continue; if (!types.isSameType(xType, l.asType())) { throw new IllegalStateException("Incorrect variable type in scope: " + l.asType() + "; should be: " + xType); } } } return super.visitMemberSelect(node, p); } }.scan(cu, null); }
Example 5
Source File: JavacParserTest.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
void testPositionBrokenSource126732b() throws IOException { String[] commands = new String[]{ "break", "break A", "continue ", "continue A",}; for (String command : commands) { String code = "package test;\n" + "public class Test {\n" + " public static void test() {\n" + " while (true) {\n" + " " + command + " {\n" + " new Runnable() {\n" + " };\n" + " }\n" + " }\n" + "}"; JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, null, Arrays.asList(new MyFileObject(code))); CompilationUnitTree cut = ct.parse().iterator().next(); ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); MethodTree method = (MethodTree) clazz.getMembers().get(0); List<? extends StatementTree> statements = ((BlockTree) ((WhileLoopTree) method.getBody().getStatements().get(0)).getStatement()).getStatements(); StatementTree ret = statements.get(0); StatementTree block = statements.get(1); Trees t = Trees.instance(ct); int len = code.indexOf(command + " {") + (command + " ").length(); assertEquals(command, len, t.getSourcePositions().getEndPosition(cut, ret)); assertEquals(command, len, t.getSourcePositions().getStartPosition(cut, block)); } }
Example 6
Source File: ApNavigator.java From hottub with GNU General Public License v2.0 | 5 votes |
private Location getLocation(Element element) { Trees trees = Trees.instance(env); return getLocation( ((TypeElement) element.getEnclosingElement()).getQualifiedName() + "." + element.getSimpleName(), trees.getPath(element) ); }
Example 7
Source File: ScopeTest.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private static void verifyLambdaScopeCorrect(final String packageClause) throws Exception { JavacTool tool = JavacTool.create(); JavaFileObject source = new SimpleJavaFileObject(URI.create("mem://Test.java"), Kind.SOURCE) { @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { return packageClause + SOURCE_CODE; } @Override public boolean isNameCompatible(String simpleName, Kind kind) { return true; } }; Iterable<? extends JavaFileObject> fos = Collections.singletonList(source); JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, fos); final Types types = JavacTypes.instance(((JavacTaskImpl) task).getContext()); final Trees trees = Trees.instance(task); CompilationUnitTree cu = task.parse().iterator().next(); task.analyze(); new TreePathScanner<Void, Void>() { @Override public Void visitMemberSelect(MemberSelectTree node, Void p) { if (node.getIdentifier().contentEquals("correct")) { TypeMirror xType = trees.getTypeMirror(new TreePath(getCurrentPath(), node.getExpression())); Scope scope = trees.getScope(getCurrentPath()); for (Element l : scope.getLocalElements()) { if (!l.getSimpleName().contentEquals("x")) continue; if (!types.isSameType(xType, l.asType())) { throw new IllegalStateException("Incorrect variable type in scope: " + l.asType() + "; should be: " + xType); } } } return super.visitMemberSelect(node, p); } }.scan(cu, null); }
Example 8
Source File: AbstractCodingRulesAnalyzer.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
public void init(JavacTask task, String... args) { BasicJavacTask impl = (BasicJavacTask)task; Context context = impl.getContext(); log = Log.instance(context); trees = Trees.instance(task); messages = new Messages(); task.addTaskListener(new PostAnalyzeTaskListener()); }
Example 9
Source File: TestGetTree.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) { final Trees trees = Trees.instance(processingEnv); for (TypeElement e : typesIn(roundEnvironment.getRootElements())) { ClassTree node = trees.getTree(e); System.out.println(node.toString()); } return true; }
Example 10
Source File: AbstractCodingRulesAnalyzer.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
public void init(JavacTask task, String... args) { BasicJavacTask impl = (BasicJavacTask)task; Context context = impl.getContext(); log = Log.instance(context); trees = Trees.instance(task); messages = new Messages(); task.addTaskListener(new PostAnalyzeTaskListener()); }
Example 11
Source File: JavacParserTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
void testPositionBrokenSource126732a() throws IOException { String[] commands = new String[]{ "return Runnable()", "do { } while (true)", "throw UnsupportedOperationException()", "assert true", "1 + 1",}; for (String command : commands) { String code = "package test;\n" + "public class Test {\n" + " public static void test() {\n" + " " + command + " {\n" + " new Runnable() {\n" + " };\n" + " }\n" + "}"; JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null, null, Arrays.asList(new MyFileObject(code))); CompilationUnitTree cut = ct.parse().iterator().next(); ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); MethodTree method = (MethodTree) clazz.getMembers().get(0); List<? extends StatementTree> statements = method.getBody().getStatements(); StatementTree ret = statements.get(0); StatementTree block = statements.get(1); Trees t = Trees.instance(ct); int len = code.indexOf(command + " {") + (command + " ").length(); assertEquals(command, len, t.getSourcePositions().getEndPosition(cut, ret)); assertEquals(command, len, t.getSourcePositions().getStartPosition(cut, block)); } }
Example 12
Source File: JavacParserTest.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
void testPositionBrokenSource126732b() throws IOException { String[] commands = new String[]{ "break", "break A", "continue ", "continue A",}; for (String command : commands) { String code = "package test;\n" + "public class Test {\n" + " public static void test() {\n" + " while (true) {\n" + " " + command + " {\n" + " new Runnable() {\n" + " };\n" + " }\n" + " }\n" + "}"; JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, null, Arrays.asList(new MyFileObject(code))); CompilationUnitTree cut = ct.parse().iterator().next(); ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); MethodTree method = (MethodTree) clazz.getMembers().get(0); List<? extends StatementTree> statements = ((BlockTree) ((WhileLoopTree) method.getBody().getStatements().get(0)).getStatement()).getStatements(); StatementTree ret = statements.get(0); StatementTree block = statements.get(1); Trees t = Trees.instance(ct); int len = code.indexOf(command + " {") + (command + " ").length(); assertEquals(command, len, t.getSourcePositions().getEndPosition(cut, ret)); assertEquals(command, len, t.getSourcePositions().getStartPosition(cut, block)); } }
Example 13
Source File: AbstractCodingRulesAnalyzer.java From hottub with GNU General Public License v2.0 | 5 votes |
public void init(JavacTask task, String... args) { BasicJavacTask impl = (BasicJavacTask)task; Context context = impl.getContext(); log = Log.instance(context); trees = Trees.instance(task); messages = new Messages(); task.addTaskListener(new PostAnalyzeTaskListener()); }
Example 14
Source File: T6402077.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
public static void main(String... args) throws IOException { class MyFileObject extends SimpleJavaFileObject { MyFileObject() { super(URI.create("myfo:///Test.java"), SOURCE); } @Override public String getCharContent(boolean ignoreEncodingErrors) { // 0 1 2 // 0123456789012345678901234 return "class Test { Test() { } }"; } } JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject()); JavacTask task = (JavacTask)javac.getTask(null, null, null, null, null, compilationUnits); Trees trees = Trees.instance(task); CompilationUnitTree toplevel = task.parse().iterator().next(); Tree tree = ((ClassTree)toplevel.getTypeDecls().get(0)).getMembers().get(0); long pos = trees.getSourcePositions().getStartPosition(toplevel, tree); if (pos != 13) throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree, pos)); pos = trees.getSourcePositions().getEndPosition(toplevel, tree); if (pos != 23) throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree, pos)); }
Example 15
Source File: ApNavigator.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public Location getClassLocation(TypeElement typeElement) { Trees trees = Trees.instance(env); return getLocation(typeElement.getQualifiedName().toString(), trees.getPath(typeElement)); }
Example 16
Source File: T6361619.java From hottub with GNU General Public License v2.0 | 4 votes |
public void finished(TaskEvent e) { System.err.println("Finished: " + e); Trees t = Trees.instance(task); }
Example 17
Source File: ShowTypePlugin.java From hottub with GNU General Public License v2.0 | 4 votes |
ShowTypeTreeVisitor(JavacTask task, Pattern pattern) { trees = Trees.instance(task); this.pattern = pattern; }
Example 18
Source File: ApNavigator.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
public Location getClassLocation(TypeElement typeElement) { Trees trees = Trees.instance(env); return getLocation(typeElement.getQualifiedName().toString(), trees.getPath(typeElement)); }
Example 19
Source File: JavacParserTest.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
private void performPositionsSanityTest(String code) throws IOException { final List<Diagnostic<? extends JavaFileObject>> errors = new LinkedList<Diagnostic<? extends JavaFileObject>>(); JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, new DiagnosticListener<JavaFileObject>() { public void report(Diagnostic<? extends JavaFileObject> diagnostic) { errors.add(diagnostic); } }, null, null, Arrays.asList(new MyFileObject(code))); final CompilationUnitTree cut = ct.parse().iterator().next(); final Trees trees = Trees.instance(ct); new TreeScanner<Void, Void>() { private long parentStart = 0; private long parentEnd = Integer.MAX_VALUE; @Override public Void scan(Tree node, Void p) { if (node == null) { return null; } long start = trees.getSourcePositions().getStartPosition(cut, node); if (start == (-1)) { return null; // synthetic tree } assertTrue(node.toString() + ":" + start + "/" + parentStart, parentStart <= start); long prevParentStart = parentStart; parentStart = start; long end = trees.getSourcePositions().getEndPosition(cut, node); assertTrue(node.toString() + ":" + end + "/" + parentEnd, end <= parentEnd); long prevParentEnd = parentEnd; parentEnd = end; super.scan(node, p); parentStart = prevParentStart; parentEnd = prevParentEnd; return null; } private void assertTrue(String message, boolean b) { if (!b) fail(message); } }.scan(cut, null); }
Example 20
Source File: TestContext.java From hottub with GNU General Public License v2.0 | 4 votes |
@Override public void init(ProcessingEnvironment pEnv) { super.init(pEnv); treeUtils = Trees.instance(processingEnv); }