本文整理汇总了Java中com.sun.source.util.JavacTask类的典型用法代码示例。如果您正苦于以下问题:Java JavacTask类的具体用法?Java JavacTask怎么用?Java JavacTask使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JavacTask类属于com.sun.source.util包,在下文中一共展示了JavacTask类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testNoAnnotationProcessing
import com.sun.source.util.JavacTask; //导入依赖的package包/类
static void testNoAnnotationProcessing(JavacFileManager fm, List<JavaFileObject> files) throws Throwable {
Context context = new Context();
String[] args = { "-d", "." };
JavacTool tool = JavacTool.create();
JavacTask task = tool.getTask(null, fm, null, List.from(args), null, files, context);
// no need in this simple case to call task.prepareCompiler(false)
JavaCompiler compiler = JavaCompiler.instance(context);
compiler.compile(files);
try {
compiler.compile(files);
throw new Error("Error: AssertionError not thrown after second call of compile");
} catch (AssertionError e) {
System.err.println("Exception from compiler (expected): " + e);
}
}
示例2: getTypeElementByBinaryName
import com.sun.source.util.JavacTask; //导入依赖的package包/类
public static TypeElement getTypeElementByBinaryName(JavacTask task, ModuleElement mod, String name) {
Context ctx = ((JavacTaskImpl) task).getContext();
Names names = Names.instance(ctx);
Symtab syms = Symtab.instance(ctx);
final Name wrappedName = names.fromString(name);
ClassSymbol clazz = syms.enterClass((ModuleSymbol) mod, wrappedName);
try {
clazz.complete();
if (clazz.kind == Kind.TYP &&
clazz.flatName() == wrappedName) {
return clazz;
}
} catch (CompletionFailure cf) {
}
return null;
}
示例3: main
import com.sun.source.util.JavacTask; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
PrintWriter out = new PrintWriter(System.out, true);
JavacTool tool = JavacTool.create();
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
File testSrc = new File(System.getProperty("test.src"));
Iterable<? extends JavaFileObject> f =
fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "AnnotatedArrayOrder.java")));
JavacTask task = tool.getTask(out, fm, null, null, null, f);
Iterable<? extends CompilationUnitTree> trees = task.parse();
out.flush();
Scanner s = new Scanner();
for (CompilationUnitTree t: trees)
s.scan(t, null);
}
}
示例4: parseArbitrarySource
import com.sun.source.util.JavacTask; //导入依赖的package包/类
public static CompilationUnitTree parseArbitrarySource(JavacTask task, JavaFileObject file) throws IOException {
JavacTaskImpl taskImpl = (JavacTaskImpl) task;
com.sun.tools.javac.util.Context context = taskImpl.getContext();
Log log = Log.instance(context);
JavaFileObject prevSource = log.useSource(file);
try {
ParserFactory fac = ParserFactory.instance(context);
JCCompilationUnit cut = fac.newParser(file.getCharContent(true), true, true, true).parseCompilationUnit();
cut.sourcefile = file;
return cut;
} finally {
log.useSource(prevSource);
}
}
示例5: main
import com.sun.source.util.JavacTask; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
PrintWriter out = new PrintWriter(System.out, true);
JavacTool tool = JavacTool.create();
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
File testSrc = new File(System.getProperty("test.src"));
Iterable<? extends JavaFileObject> f =
fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "ArrayCreationTree.java")));
JavacTask task = tool.getTask(out, fm, null, null, null, f);
Iterable<? extends CompilationUnitTree> trees = task.parse();
out.flush();
Scanner s = new Scanner();
for (CompilationUnitTree t: trees)
s.scan(t, null);
}
}
示例6: testConstructorSignatureFromElement
import com.sun.source.util.JavacTask; //导入依赖的package包/类
public void testConstructorSignatureFromElement () throws Exception {
InputStream in = this.prepareData(TEST_CLASS);
try {
JavacTask jt = prepareJavac ();
Elements elements = jt.getElements();
TypeElement be = elements.getTypeElement(TEST_CLASS);
ClassFile cf = new ClassFile (in, true);
String className = cf.getName().getInternalName().replace('/','.'); //NOI18N
List<? extends Element> members = be.getEnclosedElements();
for (Element e : members) {
if (e.getKind() == ElementKind.CONSTRUCTOR) {
String[] msig = ClassFileUtil.createExecutableDescriptor((ExecutableElement) e);
assertEquals (className,msig[0]);
assertEquals (e.getSimpleName().toString(),msig[1]);
Method m = cf.getMethod (e.getSimpleName().toString(),msig[2]);
assertNotNull (m);
}
}
} finally {
in.close ();
}
}
示例7: parse
import com.sun.source.util.JavacTask; //导入依赖的package包/类
public static void parse(Path moduleInfoPath, ModuleClassVisitor moduleClassVisitor) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try(StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(moduleInfoPath);
CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
JavacTask javacTask = (JavacTask)task;
Iterable<? extends CompilationUnitTree> units= javacTask.parse();
CompilationUnitTree unit = units.iterator().next();
ModuleHandler moduleHandler = new ModuleHandler(moduleClassVisitor);
TreeVisitor<?,?> visitor = (TreeVisitor<?,?>)Proxy.newProxyInstance(TreeVisitor.class.getClassLoader(), new Class<?>[]{ TreeVisitor.class},
(proxy, method, args) -> {
ModuleHandler.METHOD_MAP
.getOrDefault(method.getName(), (handler, node, v) -> {
throw new AssertionError("invalid node " + node.getClass());
})
.visit(moduleHandler, (Tree)args[0], (TreeVisitor<?,?>)proxy);
return null;
});
unit.accept(visitor, null);
}
}
示例8: main
import com.sun.source.util.JavacTask; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
Context context = new Context();
MyMessages.preRegister(context);
JavacTool tool = JavacTool.create();
JavacTask task = tool.getTask(null, null, null, null, null,
List.of(new MyFileObject()),
context);
task.parse();
for (Element e : task.analyze()) {
if (!e.getEnclosingElement().toString().equals("compiler.misc.unnamed.package"))
throw new AssertionError(e.getEnclosingElement());
System.out.println("OK: " + e.getEnclosingElement());
return;
}
throw new AssertionError("No top-level classes!");
}
示例9: testTreePathForModuleDecl
import com.sun.source.util.JavacTask; //导入依赖的package包/类
@Test
public void testTreePathForModuleDecl(Path base) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
Path src = base.resolve("src");
tb.writeJavaFiles(src, "/** Test module */ module m1x {}");
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(src));
JavacTask task = (JavacTask) compiler.getTask(null, fm, null, null, null, files);
task.analyze();
JavacTrees trees = JavacTrees.instance(task);
ModuleElement mdle = (ModuleElement) task.getElements().getModuleElement("m1x");
TreePath path = trees.getPath(mdle);
assertNotNull("path", path);
ModuleElement mdle1 = (ModuleElement) trees.getElement(path);
assertNotNull("mdle1", mdle1);
DocCommentTree docCommentTree = trees.getDocCommentTree(mdle);
assertNotNull("docCommentTree", docCommentTree);
}
}
示例10: main
import com.sun.source.util.JavacTask; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
File testSrc = new File(System.getProperty("test.src"));
File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null,
fileManager.getJavaFileObjects(thisSrc));
CompilationUnitTree tree = task.parse().iterator().next();
int count = 0;
for (ImportTree importTree : tree.getImports()) {
System.out.println(importTree);
count++;
}
int expected = 7;
if (count != expected)
throw new Exception("unexpected number of imports found: " + count + ", expected: " + expected);
}
}
示例11: run
import com.sun.source.util.JavacTask; //导入依赖的package包/类
void run() throws Exception {
JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
File classesDir = new File(System.getProperty("user.dir"), "classes");
classesDir.mkdirs();
JavaSource[] sources = new JavaSource[]{
new JavaSource("TestOneIgnorableChar", "AA\\u0000BB"),
new JavaSource("TestMultipleIgnorableChar", "AA\\u0000\\u0000\\u0000BB")};
JavacTask ct = (JavacTask)comp.getTask(null, null, null,
Arrays.asList("-d", classesDir.getPath()),
null, Arrays.asList(sources));
try {
if (!ct.call()) {
throw new AssertionError("Error thrown when compiling test cases");
}
} catch (Throwable ex) {
throw new AssertionError("Error thrown when compiling test cases");
}
check(classesDir,
"TestOneIgnorableChar.class",
"TestOneIgnorableChar$AABB.class",
"TestMultipleIgnorableChar.class",
"TestMultipleIgnorableChar$AABB.class");
if (errors > 0)
throw new AssertionError("There are some errors in the test check the error output");
}
示例12: clear
import com.sun.source.util.JavacTask; //导入依赖的package包/类
void clear() {
drop(Arguments.argsKey);
drop(DiagnosticListener.class);
drop(Log.outKey);
drop(Log.errKey);
drop(JavaFileManager.class);
drop(JavacTask.class);
if (ht.get(Log.logKey) instanceof ReusableLog) {
//log already inited - not first round
((ReusableLog)Log.instance(this)).clear();
Enter.instance(this).newRound();
((ReusableJavaCompiler)ReusableJavaCompiler.instance(this)).clear();
Types.instance(this).newRound();
Check.instance(this).newRound();
Modules.instance(this).newRound();
Annotate.instance(this).newRound();
CompileStates.instance(this).clear();
MultiTaskListener.instance(this).clear();
//find if any of the roots have redefined java.* classes
Symtab syms = Symtab.instance(this);
pollutionScanner.scan(roots, syms);
roots.clear();
}
}
示例13: run
import com.sun.source.util.JavacTask; //导入依赖的package包/类
void run() throws Exception {
Context context = new Context();
JavacFileManager.preRegister(context);
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource()));
Iterable<? extends CompilationUnitTree> elements = ct.parse();
ct.analyze();
Assert.check(elements.iterator().hasNext());
JCTree topLevel = (JCTree)elements.iterator().next();
new TreeScanner() {
@Override
public void visitReference(JCMemberReference tree) {
Assert.check(tree.getOverloadKind() != null);
}
}.scan(topLevel);
}
示例14: process
import com.sun.source.util.JavacTask; //导入依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// System.err.println("TestProcessor.process " + roundEnv);
JavacTask task = JavacTask.instance(processingEnv);
if (++round == 1) {
switch (ak) {
case ADD_IN_PROCESSOR:
task.addTaskListener(listener);
break;
case ADD_IN_LISTENER:
addInListener(task, TaskEvent.Kind.ANALYZE, listener);
break;
}
} else if (roundEnv.processingOver()) {
switch (rk) {
case REMOVE_IN_PROCESSOR:
task.removeTaskListener(listener);
break;
case REMOVE_IN_LISTENER:
removeInListener(task, TaskEvent.Kind.GENERATE, listener);
break;
}
}
return true;
}
示例15: compile
import com.sun.source.util.JavacTask; //导入依赖的package包/类
private File compile(List<File> classpaths, List<JavaFileObject> files, boolean generate) throws IOException {
JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fm = systemJavaCompiler.getStandardFileManager(null, null, null)) {
if (classpaths.size() > 0)
fm.setLocation(StandardLocation.CLASS_PATH, classpaths);
JavacTask ct = (JavacTask) systemJavaCompiler.getTask(null, fm, diags, compileOptions, null, files);
if (generate) {
File destDir = new File(root, Integer.toString(counter.incrementAndGet()));
// @@@ Assert that this directory didn't exist, or start counter at max+1
destDir.mkdirs();
fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
ct.generate();
return destDir;
}
else {
ct.analyze();
return nullDir;
}
}
}