本文整理汇总了Java中com.sun.tools.javac.api.JavacTaskImpl.enter方法的典型用法代码示例。如果您正苦于以下问题:Java JavacTaskImpl.enter方法的具体用法?Java JavacTaskImpl.enter怎么用?Java JavacTaskImpl.enter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.javac.api.JavacTaskImpl
的用法示例。
在下文中一共展示了JavacTaskImpl.enter方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findSource
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
private Pair<JavacTask, CompilationUnitTree> findSource(String moduleName,
String binaryName) throws IOException {
JavaFileObject jfo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH,
binaryName,
JavaFileObject.Kind.SOURCE);
if (jfo == null)
return null;
List<JavaFileObject> jfos = Arrays.asList(jfo);
JavaFileManager patchFM = moduleName != null
? new PatchModuleFileManager(baseFileManager, jfo, moduleName)
: baseFileManager;
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, patchFM, d -> {}, null, null, jfos);
Iterable<? extends CompilationUnitTree> cuts = task.parse();
task.enter();
return Pair.of(task, cuts.iterator().next());
}
示例2: testBrokenModuleInfoModuleSourcePath
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
@Test
public void testBrokenModuleInfoModuleSourcePath(Path base) throws Exception {
Path msp = base.resolve("msp");
Path ma = msp.resolve("ma");
tb.writeJavaFiles(ma,
"module ma { requires not.available; exports api1; }",
"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.enter();
task.getElements().getModuleElement("java.base");
}
示例3: testSystem
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
@Test
public void testSystem(Path base) throws Exception {
Path jdkCompiler = base.resolve("jdk.compiler");
tb.writeJavaFiles(jdkCompiler,
"module jdk.compiler { requires not.available; }");
Path out = base.resolve("out");
tb.createDirectories(out);
List<String> opts = List.of("--patch-module", "jdk.compiler=" + jdkCompiler.toString(),
"--add-modules", "jdk.compiler");
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, opts, jlObjectList, null);
task.enter();
task.getElements().getModuleElement("java.base");
}
示例4: main
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public static void main(String... args) throws IOException {
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
String srcdir = System.getProperty("test.src");
File file = new File(srcdir, args[0]);
List<String> options = Arrays.asList(
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED"
);
JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, options, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
Elements elements = task.getElements();
for (Element clazz : task.enter(task.parse())) {
String doc = elements.getDocComment(clazz);
if (doc == null)
throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
}
}
}
示例5: testGetTypeDeclaration
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public void testGetTypeDeclaration() throws Exception {
ClasspathInfo ci = ClasspathInfo.create( bootPath, classPath, null);
JavacTaskImpl jTask = JavacParser.createJavacTask(ci, (DiagnosticListener) null, (String) null, null, null, null, null, null, Collections.emptyList());
jTask.enter();
List<String> notFound = new LinkedList<String>();
JarFile jf = new JarFile( rtJar );
for( Enumeration entries = jf.entries(); entries.hasMoreElements(); ) {
JarEntry je = (JarEntry)entries.nextElement();
String jeName = je.getName();
if ( !je.isDirectory() && jeName.endsWith( ".class" ) ) {
String typeName = jeName.substring( 0, jeName.length() - ".class".length() );
typeName = typeName.replace( "/", "." ); //.replace( "$", "." );
TypeElement te = ElementUtils.getTypeElementByBinaryName(jTask, typeName );
// assertNotNull( "Declaration for " + typeName + " should not be null.", td );
if ( te == null ) {
if (!typeName.endsWith("package-info")) {
notFound.add( typeName );
}
}
}
}
assertTrue( "Should be empty " + notFound, notFound.isEmpty() );
}
示例6: 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);
}
}
示例7: basicTest
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
static void basicTest(String... args) throws IOException {
String srcdir = System.getProperty("test.src");
File file = new File(srcdir, args[0]);
JavacTaskImpl task = getTask(file);
for (Element clazz : task.enter(task.parse()))
System.out.println(clazz.getSimpleName());
}
示例8: preBuildArgs
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
/**
* Pre builds argument names for {@link javax.swing.JComponent} to speed up first
* call of code completion on swing classes. Has no semantic impact only improves performance,
* so it's can be safely disabled.
* @param archiveFile the archive
* @param archiveUrl URL of an archive
*/
private static void preBuildArgs (
@NonNull final String fqn,
@NonNull final URL... archiveUrls) {
class DevNullDiagnosticListener implements DiagnosticListener<JavaFileObject> {
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "Diagnostic reported during prebuilding args: {0}", diagnostic.toString()); //NOI18N
}
}
}
ClasspathInfo cpInfo = ClasspathInfoAccessor.getINSTANCE().create(
ClassPathSupport.createClassPath(archiveUrls),
ClassPathSupport.createClassPath(archiveUrls),
ClassPath.EMPTY,
ClassPath.EMPTY,
ClassPath.EMPTY,
ClassPath.EMPTY,
ClassPath.EMPTY,
null,
true,
true,
false,
false,
false,
null);
final JavacTaskImpl jt = JavacParser.createJavacTask(cpInfo, new DevNullDiagnosticListener(), null, null, null, null, null, null, Collections.emptyList());
//Force JTImpl.prepareCompiler to get JTImpl into Context
jt.enter();
TypeElement jc = ElementUtils.getTypeElementByBinaryName(jt, fqn);
if (jc != null) {
List<ExecutableElement> methods = ElementFilter.methodsIn(jt.getElements().getAllMembers(jc));
for (ExecutableElement method : methods) {
List<? extends VariableElement> params = method.getParameters();
if (!params.isEmpty()) {
params.get(0).getSimpleName();
}
}
}
}
示例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();
}
}
示例12: main
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public static void main(String... args) throws IOException {
if (args.length < 1) {
help();
return ;
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
List<String> options = Arrays.asList("-source", "9",
"-target", "9",
"-proc:only",
"--system", "none",
"--module-source-path", args[0],
"--add-modules", Arrays.stream(args)
.skip(1)
.collect(Collectors.joining(",")));
List<String> jlObjectList = Arrays.asList("java.lang.Object");
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, options, jlObjectList, null);
task.enter();
Elements elements = task.getElements();
List<String> todo = new LinkedList<>();
Arrays.stream(args).skip(1).forEach(todo::add);
Set<String> allModules = new HashSet<>();
while (!todo.isEmpty()) {
String current = todo.remove(0);
if (!allModules.add(current))
continue;
ModuleSymbol mod = (ModuleSymbol) elements.getModuleElement(current);
if (mod == null) {
throw new IllegalStateException("Missing: " + current);
}
//use the internal structure to avoid unnecesarily completing the symbol using the UsesProvidesVisitor:
for (RequiresDirective rd : mod.requires) {
if (rd.isTransitive()) {
todo.add(rd.getDependency().getQualifiedName().toString());
}
}
}
allModules.add("java.base");
allModules.add("jdk.unsupported");
allModules.stream()
.sorted()
.forEach(System.out::println);
}