本文整理汇总了Java中com.sun.tools.javac.main.JavaCompiler.compile方法的典型用法代码示例。如果您正苦于以下问题:Java JavaCompiler.compile方法的具体用法?Java JavaCompiler.compile怎么用?Java JavaCompiler.compile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.javac.main.JavaCompiler
的用法示例。
在下文中一共展示了JavaCompiler.compile方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testNoAnnotationProcessing
import com.sun.tools.javac.main.JavaCompiler; //导入方法依赖的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: testAnnotationProcessing
import com.sun.tools.javac.main.JavaCompiler; //导入方法依赖的package包/类
static void testAnnotationProcessing(JavacFileManager fm, List<JavaFileObject> files) throws Throwable {
Context context = new Context();
String[] args = {
"-XprintRounds",
"-processorpath", testClasses,
"-processor", self,
"-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);
}
}
示例3: test
import com.sun.tools.javac.main.JavaCompiler; //导入方法依赖的package包/类
static void test(JavacFileManager fm, JavaFileObject f, String... args) throws Throwable {
Context context = new Context();
fm.setContext(context);
Main compilerMain = new Main("javac", new PrintWriter(System.err, true));
compilerMain.setOptions(Options.instance(context));
compilerMain.filenames = new LinkedHashSet<File>();
compilerMain.processArgs(args);
JavaCompiler c = JavaCompiler.instance(context);
c.compile(List.of(f));
if (c.errorCount() != 0)
throw new AssertionError("compilation failed");
long msec = c.elapsed_msec;
if (msec < 0 || msec > 5 * 60 * 1000) // allow test 5 mins to execute, should be more than enough!
throw new AssertionError("elapsed time is suspect: " + msec);
}
示例4: testNoAnnotationProcessing
import com.sun.tools.javac.main.JavaCompiler; //导入方法依赖的package包/类
static void testNoAnnotationProcessing(JavacFileManager fm, JavaFileObject f) throws Throwable {
Context context = new Context();
fm.setContext(context);
Main compilerMain = new Main("javac", new PrintWriter(System.err, true));
compilerMain.setOptions(Options.instance(context));
compilerMain.filenames = new LinkedHashSet<File>();
compilerMain.processArgs(new String[] { "-d", "." });
JavaCompiler compiler = JavaCompiler.instance(context);
compiler.compile(List.of(f));
try {
compiler.compile(List.of(f));
throw new Error("Error: AssertionError not thrown after second call of compile");
} catch (AssertionError e) {
System.err.println("Exception from compiler (expected): " + e);
}
}
示例5: testAnnotationProcessing
import com.sun.tools.javac.main.JavaCompiler; //导入方法依赖的package包/类
static void testAnnotationProcessing(JavacFileManager fm, JavaFileObject f) throws Throwable {
Context context = new Context();
fm.setContext(context);
Main compilerMain = new Main("javac", new PrintWriter(System.err, true));
compilerMain.setOptions(Options.instance(context));
compilerMain.filenames = new LinkedHashSet<File>();
compilerMain.processArgs(new String[] {
"-XprintRounds",
"-processorpath", testClasses,
"-processor", self,
"-d", "."});
JavaCompiler compiler = JavaCompiler.instance(context);
compiler.initProcessAnnotations(null);
JavaCompiler compiler2 = compiler.processAnnotations(compiler.enterTrees(compiler.parseFiles(List.of(f))));
try {
compiler2.compile(List.of(f));
throw new Error("Error: AssertionError not thrown after second call of compile");
} catch (AssertionError e) {
System.err.println("Exception from compiler (expected): " + e);
}
}
示例6: test
import com.sun.tools.javac.main.JavaCompiler; //导入方法依赖的package包/类
static void test(JavacFileManager fm, JavaFileObject f, String... args) throws Throwable {
Context context = new Context();
fm.setContext(context);
Main compilerMain = new Main("javac", new PrintWriter(System.err, true));
compilerMain.setOptions(Options.instance(context));
compilerMain.filenames = new ListBuffer<File>();
compilerMain.processArgs(args);
JavaCompiler c = JavaCompiler.instance(context);
c.compile(List.of(f));
if (c.errorCount() != 0)
throw new AssertionError("compilation failed");
long msec = c.elapsed_msec;
if (msec < 0 || msec > 5 * 60 * 1000) // allow test 5 mins to execute, should be more than enough!
throw new AssertionError("elapsed time is suspect: " + msec);
}
示例7: testNoAnnotationProcessing
import com.sun.tools.javac.main.JavaCompiler; //导入方法依赖的package包/类
static void testNoAnnotationProcessing(JavacFileManager fm, JavaFileObject f) throws Throwable {
Context context = new Context();
fm.setContext(context);
Main compilerMain = new Main("javac", new PrintWriter(System.err, true));
compilerMain.setOptions(Options.instance(context));
compilerMain.filenames = new ListBuffer<File>();
compilerMain.processArgs(new String[] { "-d", "." });
JavaCompiler compiler = JavaCompiler.instance(context);
compiler.compile(List.of(f));
try {
compiler.compile(List.of(f));
throw new Error("Error: AssertionError not thrown after second call of compile");
} catch (AssertionError e) {
System.err.println("Exception from compiler (expected): " + e);
}
}
示例8: testAnnotationProcessing
import com.sun.tools.javac.main.JavaCompiler; //导入方法依赖的package包/类
static void testAnnotationProcessing(JavacFileManager fm, JavaFileObject f) throws Throwable {
Context context = new Context();
fm.setContext(context);
Main compilerMain = new Main("javac", new PrintWriter(System.err, true));
compilerMain.setOptions(Options.instance(context));
compilerMain.filenames = new ListBuffer<File>();
compilerMain.processArgs(new String[] {
"-XprintRounds",
"-processorpath", testClasses,
"-processor", self,
"-d", "."});
JavaCompiler compiler = JavaCompiler.instance(context);
compiler.initProcessAnnotations(null);
JavaCompiler compiler2 = compiler.processAnnotations(compiler.enterTrees(compiler.parseFiles(List.of(f))));
try {
compiler2.compile(List.of(f));
throw new Error("Error: AssertionError not thrown after second call of compile");
} catch (AssertionError e) {
System.err.println("Exception from compiler (expected): " + e);
}
}
示例9: clearPendingUnits
import com.sun.tools.javac.main.JavaCompiler; //导入方法依赖的package包/类
protected void clearPendingUnits(BasicJavacTask task) {
List<CompilationUnitTaskList> missing = new ArrayList<>();
cups.values()
.stream()
.filter(pcu -> pcu.getUnit() == null)
.forEach(missing::add);
if (!missing.isEmpty()) {
Context ctx = new Context(task.getContext());
ctx.put(JavacService.class, service);
MultiTaskListener tasks = MultiTaskListener.instance(ctx);
tasks.add(getTaskListener(task));
JavacProcessingEnvironment env = JavacProcessingEnvironment.instance(ctx);
try (
JavacFileManager jfm = new JavacFileManager(ctx, false, Charset.forName("UTF-8"))
) {
List<JavaFileObject> missingFiles = new ArrayList<>();
missing.forEach(pcu -> {
String name = pcu.getName();
TypeElement element = task.getElements().getTypeElement(name);
if (element == null) {
X_Log.info(getClass(), "No element found for ", name);
} else {
final javax.lang.model.element.Name qualified = element.getQualifiedName();
Name binary = env.getElementUtils().getBinaryName(element);
String relativeName = binary.toString().split("[$]")[0];// TODO check for enclosing types instead of this nasty hack
JavaFileObject javaFile;
try {
javaFile = jfm.getJavaFileForInput(StandardLocation.SOURCE_OUTPUT, relativeName, JavaFileObject.Kind.SOURCE);
if (javaFile == null) {
javaFile = jfm.getJavaFileForInput(StandardLocation.SOURCE_PATH, relativeName, JavaFileObject.Kind.SOURCE);
if (javaFile == null) {
javaFile = jfm.getJavaFileForInput(StandardLocation.CLASS_PATH, relativeName, JavaFileObject.Kind.SOURCE);
if (javaFile == null) {
X_Log.warn(getClass(), "Unable to find file ", relativeName, " to recompile...");
}
}
}
if (javaFile != null){
missingFiles.add(javaFile);
}
} catch (Throwable e) {
X_Log.warn(getClass(), "Unable to compile source for "+binary, e);
throw X_Debug.rethrow(e);
}
// pcu.setUnit(cu);
// pcu.finish();
}
});
JavaCompiler compiler = JavaCompiler.instance(ctx);
final com.sun.tools.javac.util.List<JavaFileObject> javaFiles = com.sun.tools.javac.util.List.from(missingFiles.toArray(new JavaFileObject[missingFiles.size()]));
compiler.compile(javaFiles);
} catch (Throwable throwable) {
throw X_Debug.rethrow(throwable);
}
}
doFinish();
}