本文整理汇总了Java中com.sun.tools.javac.api.JavacTaskImpl.analyze方法的典型用法代码示例。如果您正苦于以下问题:Java JavacTaskImpl.analyze方法的具体用法?Java JavacTaskImpl.analyze怎么用?Java JavacTaskImpl.analyze使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.javac.api.JavacTaskImpl
的用法示例。
在下文中一共展示了JavacTaskImpl.analyze方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
void run() throws IOException {
JavacTool tool = JavacTool.create();
JavaSource source = new JavaSource("class Test {" +
" I i = STOP -> {};" +
" interface I {" +
" public void test(int i) {}" +
" }" +
"}");
Context context = new Context();
CrashingAttr.preRegister(context);
List<JavaSource> inputs = Arrays.asList(source);
JavacTaskImpl task =
(JavacTaskImpl) tool.getTask(null, null, null, null, null, inputs, context);
try {
task.analyze(null);
throw new AssertionError("Expected exception not seen.");
} catch (StopException ex) {
//ok
}
}
示例2: run
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
void run(Writer output, String version) throws Exception {
List<String> options = Arrays.asList("--release", version, "-classpath", "");
List<ToolBox.JavaSource> files = Arrays.asList(new ToolBox.JavaSource("Test", ""));
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, options, null, files);
task.analyze();
JavaFileManager fm = task.getContext().get(JavaFileManager.class);
for (String pack : packages(fm)) {
PackageElement packEl = task.getElements().getPackageElement(pack);
if (packEl == null) {
throw new AssertionError("Cannot find package: " + pack);
}
new ExhaustiveElementScanner(task, output, p -> true).visit(packEl);
}
}
示例3: testParseError
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
@Test
public void testParseError(Path base) throws Exception {
Path msp = base.resolve("msp");
Path ma = msp.resolve("ma");
tb.writeJavaFiles(ma,
"broken module ma { }",
"package api1; public interface Api { }");
Path out = base.resolve("out");
tb.createDirectories(out);
List<String> opts = List.of("--module-source-path", msp.toString(),
"--add-modules", "ma");
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, opts, jlObjectList, null);
task.analyze();
task.getElements().getModuleElement("java.base");
}
示例4: constructScope
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public static Scope constructScope(CompilationInfo info, String... importedClasses) {
StringBuilder clazz = new StringBuilder();
clazz.append("package $$;\n");
for (String i : importedClasses) {
clazz.append("import ").append(i).append(";\n");
}
clazz.append("public class $").append(inc++).append("{");
clazz.append("private void test() {\n");
clazz.append("}\n");
clazz.append("}\n");
JavacTaskImpl jti = JavaSourceAccessor.getINSTANCE().getJavacTask(info);
Context context = jti.getContext();
JavaCompiler jc = JavaCompiler.instance(context);
Log.instance(context).nerrors = 0;
JavaFileObject jfo = FileObjects.memoryFileObject("$$", "$", new File("/tmp/t.java").toURI(), System.currentTimeMillis(), clazz.toString());
try {
CompilationUnitTree cut = ParserFactory.instance(context).newParser(jfo.getCharContent(true), true, true, true).parseCompilationUnit();
jti.analyze(jti.enter(Collections.singletonList(cut)));
return new ScannerImpl().scan(cut, info);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
return null;
} finally {
}
}
示例5: testCorrectAnonymousIndicesForMethodInvocations
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public void testCorrectAnonymousIndicesForMethodInvocations() throws Exception {
String code = "package test;\n" +
"public class Test {\n" +
" public Test main(Object o) {\n" +
" return new Test().main(new Runnable() {\n" +
" public void run() {\n" +
" throw new UnsupportedOperationException();\n" +
" }\n" +
" }).main(new Iterable() {\n" +
" public java.util.Iterator iterator() {\n" +
" throw new UnsupportedOperationException();\n" +
" }\n" +
" });\n" +
" }\n" +
"}";
JavacTaskImpl ct = Utilities.createJavac(null, Utilities.fileObjectFor(code));
Iterable<? extends CompilationUnitTree> cuts = ct.parse();
Iterable<? extends Element> analyze = ct.analyze();
Symtab symTab = Symtab.instance(ct.getContext());
Modules modules = Modules.instance(ct.getContext());
TypeElement first = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$1"));
TypeElement second = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$2"));
assertEquals("java.lang.Iterable", ((TypeElement) ((DeclaredType) first.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
assertEquals("java.lang.Runnable", ((TypeElement) ((DeclaredType) second.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
}
示例6: testCorrectAnonymousIndicesForMultipleMethods
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public void testCorrectAnonymousIndicesForMultipleMethods() throws IOException {
String code = "package test;\n" +
"public class Test {\n" +
" public Test main1(Object o) {\n" +
" new Runnable() {\n" +
" public void run() {\n" +
" throw new UnsupportedOperationException();\n" +
" }\n" +
" };" +
" }" +
" public Test main2(Object o) {\n" +
" new Iterable() {\n" +
" public java.util.Iterator iterator() {\n" +
" throw new UnsupportedOperationException();\n" +
" }\n" +
" };\n" +
" }\n" +
" public Test main3(Object o) {\n" +
" new java.util.ArrayList() {};\n" +
" }\n" +
"}";
JavacTaskImpl ct = Utilities.createJavac(null, Utilities.fileObjectFor(code));
ct.analyze();
Symtab symTab = Symtab.instance(ct.getContext());
Modules modules = Modules.instance(ct.getContext());
TypeElement first = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$1"));
TypeElement second = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$2"));
TypeElement third = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$3"));
assertEquals("java.lang.Runnable", ((TypeElement) ((DeclaredType) first.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
assertEquals("java.lang.Iterable", ((TypeElement) ((DeclaredType) second.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
assertEquals("java.util.ArrayList", ((TypeElement) ((DeclaredType) third.getSuperclass()).asElement()).getQualifiedName().toString());
}
示例7: testCorrectNameForAnonymous
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public void testCorrectNameForAnonymous() throws IOException {
String code = "package test;\n" +
"public class Test {\n" +
" public Test main1(Object o) {\n" +
" new Runnable() {\n" +
" public void run() {\n" +
" throw new UnsupportedOperationException();\n" +
" }\n" +
" };" +
" new Iterable() {\n" +
" public java.util.Iterator iterator() {\n" +
" new java.util.ArrayList() {};\n" +
" }\n" +
" };\n" +
" }\n" +
"}";
JavacTaskImpl ct = Utilities.createJavac(null, Utilities.fileObjectFor(code));
ct.analyze();
Symtab symTab = Symtab.instance(ct.getContext());
Modules modules = Modules.instance(ct.getContext());
TypeElement first = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$1"));
TypeElement second = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$2"));
TypeElement third = symTab.getClass(modules.getDefaultModule(), (Name)ct.getElements().getName("test.Test$2$1"));
assertEquals("java.lang.Runnable", ((TypeElement) ((DeclaredType) first.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
assertEquals("java.lang.Iterable", ((TypeElement) ((DeclaredType) second.getInterfaces().get(0)).asElement()).getQualifiedName().toString());
assertEquals("java.util.ArrayList", ((TypeElement) ((DeclaredType) third.getSuperclass()).asElement()).getQualifiedName().toString());
}
示例8: testParseEnterAnalyze
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
@Test
public void testParseEnterAnalyze(Path base) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1x");
tb.writeJavaFiles(m1, "module m1x { }",
"package p;",
"package p; class T { }");
Path classes = base.resolve("classes");
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(moduleSrc));
List<String> options = Arrays.asList("-d", classes.toString(), "-Xpkginfo:always");
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, fm, null, options, null, files);
Iterable<? extends CompilationUnitTree> parsed = task.parse();
Iterable<? extends Element> entered = task.enter(parsed);
Iterable<? extends Element> analyzed = task.analyze(entered);
Iterable<? extends JavaFileObject> generatedFiles = task.generate(analyzed);
Set<String> generated = new HashSet<>();
for (JavaFileObject jfo : generatedFiles) {
generated.add(jfo.getName());
}
Set<String> expected = new HashSet<>(
Arrays.asList(Paths.get("testParseEnterAnalyze", "classes", "p", "package-info.class").toString(),
Paths.get("testParseEnterAnalyze", "classes", "module-info.class").toString(),
Paths.get("testParseEnterAnalyze", "classes", "p", "T.class").toString())
);
if (!Objects.equals(expected, generated))
throw new AssertionError("Incorrect generated files: " + generated);
}
}
示例9: testMethodReference
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public void testMethodReference() throws Exception {
final FileObject libFile = FileUtil.toFileObject(TestFileUtils.writeFile(
new File(FileUtil.toFile(src),"Lib.java"), //NOI18N
"public class Lib {\n" + //NOI18N
" public static void foo(){}\n" + //NOI18N
"}")); //NOI18N
final FileObject javaFile = FileUtil.toFileObject(TestFileUtils.writeFile(
new File(FileUtil.toFile(src),"Test.java"), //NOI18N
"public class Test { \n" + //NOI18N
" public static void main(String[] args) {\n" + //NOI18N
" Runnable r = Lib::foo;\n" + //NOI18N
" }\n" + //NOI18N
"}")); //NOI18N
final DiagnosticListener<JavaFileObject> diag = new DiagnosticListener<JavaFileObject>() {
private final Queue<Diagnostic<? extends JavaFileObject>> problems = new ArrayDeque<Diagnostic<? extends JavaFileObject>>();
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
problems.offer(diagnostic);
}
};
TransactionContext.beginStandardTransaction(src.toURL(), true, false, true);
try {
final ClasspathInfo cpInfo = ClasspathInfoAccessor.getINSTANCE().create(
src,
null,
true,
true,
false,
true);
final JavaFileObject jfo = FileObjects.sourceFileObject(javaFile, src);
final JavacTaskImpl jt = JavacParser.createJavacTask(
cpInfo,
diag,
SourceLevelQuery.getSourceLevel(src), //NOI18N
SourceLevelQuery.Profile.DEFAULT,
null, null, null, null, Arrays.asList(jfo));
final Iterable<? extends CompilationUnitTree> trees = jt.parse();
jt.enter();
jt.analyze();
final SourceAnalyzerFactory.SimpleAnalyzer sa = SourceAnalyzerFactory.createSimpleAnalyzer();
List<Pair<Pair<BinaryName, String>, Object[]>> data = sa.analyseUnit(trees.iterator().next(), jt);
assertEquals(1, data.size());
assertTrue(((Collection)data.iterator().next().second()[0]).contains(
DocumentUtil.encodeUsage("Lib", EnumSet.<ClassIndexImpl.UsageType>of( //NOI18N
ClassIndexImpl.UsageType.METHOD_REFERENCE,
ClassIndexImpl.UsageType.TYPE_REFERENCE))));
} finally {
TransactionContext.get().rollBack();
}
}
示例10: testConstructorReference
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public void testConstructorReference() throws Exception {
final FileObject libFile = FileUtil.toFileObject(TestFileUtils.writeFile(
new File(FileUtil.toFile(src),"Lib.java"), //NOI18N
"public class Lib {\n" + //NOI18N
"}")); //NOI18N
final FileObject javaFile = FileUtil.toFileObject(TestFileUtils.writeFile(
new File(FileUtil.toFile(src),"Test.java"), //NOI18N
"public class Test { \n" + //NOI18N
" public static void main(String[] args) {\n" + //NOI18N
" Runnable r = Lib::new;\n" + //NOI18N
" }\n" + //NOI18N
"}")); //NOI18N
final DiagnosticListener<JavaFileObject> diag = new DiagnosticListener<JavaFileObject>() {
private final Queue<Diagnostic<? extends JavaFileObject>> problems = new ArrayDeque<Diagnostic<? extends JavaFileObject>>();
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
problems.offer(diagnostic);
}
};
TransactionContext.beginStandardTransaction(src.toURL(), true, false, true);
try {
final ClasspathInfo cpInfo = ClasspathInfoAccessor.getINSTANCE().create(
src,
null,
true,
true,
false,
true);
final JavaFileObject jfo = FileObjects.sourceFileObject(javaFile, src);
final JavacTaskImpl jt = JavacParser.createJavacTask(
cpInfo,
diag,
SourceLevelQuery.getSourceLevel(src), //NOI18N
SourceLevelQuery.Profile.DEFAULT,
null, null, null, null, Arrays.asList(jfo));
final Iterable<? extends CompilationUnitTree> trees = jt.parse();
jt.enter();
jt.analyze();
final SourceAnalyzerFactory.SimpleAnalyzer sa = SourceAnalyzerFactory.createSimpleAnalyzer();
List<Pair<Pair<BinaryName, String>, Object[]>> data = sa.analyseUnit(trees.iterator().next(), jt);
assertEquals(1, data.size());
assertTrue(((Collection)data.iterator().next().second()[0]).contains(
DocumentUtil.encodeUsage("Lib", EnumSet.<ClassIndexImpl.UsageType>of( //NOI18N
ClassIndexImpl.UsageType.METHOD_REFERENCE,
ClassIndexImpl.UsageType.TYPE_REFERENCE))));
} finally {
TransactionContext.get().rollBack();
}
}
示例11: testBrokenReference
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public void testBrokenReference() throws Exception {
final FileObject javaFile = FileUtil.toFileObject(TestFileUtils.writeFile(
new File(FileUtil.toFile(src),"Test.java"), //NOI18N
"public class Test { \n" + //NOI18N
" public static void main(String[] args) {\n" + //NOI18N
" Runnable r = Lib::foo;\n" + //NOI18N
" }\n" + //NOI18N
"}")); //NOI18N
final DiagnosticListener<JavaFileObject> diag = new DiagnosticListener<JavaFileObject>() {
private final Queue<Diagnostic<? extends JavaFileObject>> problems = new ArrayDeque<Diagnostic<? extends JavaFileObject>>();
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
problems.offer(diagnostic);
}
};
TransactionContext.beginStandardTransaction(src.toURL(), true, false, true);
try {
final ClasspathInfo cpInfo = ClasspathInfoAccessor.getINSTANCE().create(
src,
null,
true,
true,
false,
true);
final JavaFileObject jfo = FileObjects.sourceFileObject(javaFile, src);
final JavacTaskImpl jt = JavacParser.createJavacTask(
cpInfo,
diag,
SourceLevelQuery.getSourceLevel(src), //NOI18N
SourceLevelQuery.Profile.DEFAULT,
null, null, null, null, Arrays.asList(jfo));
final Iterable<? extends CompilationUnitTree> trees = jt.parse();
jt.enter();
jt.analyze();
final SourceAnalyzerFactory.SimpleAnalyzer sa = SourceAnalyzerFactory.createSimpleAnalyzer();
List<Pair<Pair<BinaryName, String>, Object[]>> data = sa.analyseUnit(trees.iterator().next(), jt);
assertEquals(1, data.size());
assertTrue(((Collection)data.iterator().next().second()[0]).contains(
DocumentUtil.encodeUsage("Lib", EnumSet.<ClassIndexImpl.UsageType>of( //NOI18N
ClassIndexImpl.UsageType.TYPE_REFERENCE))));
} finally {
TransactionContext.get().rollBack();
}
}