本文整理汇总了Java中javax.tools.JavaCompiler.CompilationTask.call方法的典型用法代码示例。如果您正苦于以下问题:Java CompilationTask.call方法的具体用法?Java CompilationTask.call怎么用?Java CompilationTask.call使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.tools.JavaCompiler.CompilationTask
的用法示例。
在下文中一共展示了CompilationTask.call方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compileFiles
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
public static void compileFiles(File projectRoot, List<String> javaFiles) {
DiagnosticCollector diagnosticCollector = new DiagnosticCollector();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, Locale.ENGLISH, Charset.forName("utf-8"));
Iterable<? extends JavaFileObject> javaFileObjects = fileManager.getJavaFileObjects(javaFiles.toArray(new String[0]));
File outputFolder = new File(projectRoot, "bin");
if (!outputFolder.exists()) {
outputFolder.mkdir();
}
String[] options = new String[] { "-d", outputFolder.getAbsolutePath() , "-g", "-proc:none"};
final StringWriter output = new StringWriter();
CompilationTask task = compiler.getTask(output, fileManager, diagnosticCollector, Arrays.asList(options), null, javaFileObjects);
boolean result = task.call();
if (!result) {
throw new IllegalArgumentException(
"Compilation failed:\n" + output);
}
List list = diagnosticCollector.getDiagnostics();
for (Object object : list) {
Diagnostic d = (Diagnostic) object;
System.out.println(d.getMessage(Locale.ENGLISH));
}
}
示例2: run
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
int run(JavaCompiler comp, StandardJavaFileManager fm) throws IOException {
System.err.println("test: ck:" + ck + " gk:" + gk + " sk:" + sk);
File testDir = new File(ck + "-" + gk + "-" + sk);
testDir.mkdirs();
fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(testDir));
JavaSource js = new JavaSource();
System.err.println(js.getCharContent(false));
CompilationTask t = comp.getTask(null, fm, null, null, null, Arrays.asList(js));
if (!t.call())
throw new Error("compilation failed");
File testClass = new File(testDir, "Test.class");
String out = javap(testClass);
// Extract class sig from first line of Java source
String expect = js.source.replaceAll("(?s)^(.* Test[^{]+?) *\\{.*", "$1");
// Extract class sig from line from javap output
String found = out.replaceAll("(?s).*\n(.* Test[^{]+?) *\\{.*", "$1");
checkEqual("class signature", expect, found);
return errors;
}
示例3: main
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
// Get a compiler tool
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
String srcdir = System.getProperty("test.src");
File source = new File(srcdir, "T6378728.java");
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
CompilationTask task =
compiler.getTask(null,
new ExceptionalFileManager(fm),
null,
Arrays.asList("-proc:only"),
null,
fm.getJavaFileObjectsFromFiles(Arrays.asList(source)));
if (!task.call())
throw new RuntimeException("Unexpected compilation failure");
}
}
示例4: testCompositeSourcePath
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
private void testCompositeSourcePath(JavaCompiler javac) throws Exception {
System.err.println("testCompositeSearchPath");
File tmpdir = new File("testCompositeSourcePath");
File srcdir = new File(tmpdir, "src");
File rsrcdir = new File(tmpdir, "rsrc");
File destdir = new File(tmpdir, "dest");
write(srcdir, "pkg/X.java", "package pkg; class X {}");
write(rsrcdir, "resources/file.txt", "hello");
destdir.mkdirs();
CompilationTask task = javac.getTask(null, null, null,
Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()),
Collections.singleton("pkg.X"), null);
task.setProcessors(Collections.singleton(new AnnoProc()));
boolean result = task.call();
System.err.println("javac result with composite source path: " + result);
expect(result, true);
}
示例5: testMissingResource
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
private void testMissingResource(JavaCompiler javac) throws Exception {
System.err.println("testMissingResource");
File tmpdir = new File("testMissingResource");
File srcdir = new File(tmpdir, "src");
File destdir = new File(tmpdir, "dest");
write(srcdir, "pkg/X.java", "package pkg; class X {}");
destdir.mkdirs();
CompilationTask task = javac.getTask(null, null, null,
Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
Collections.singleton("pkg.X"), null);
task.setProcessors(Collections.singleton(new AnnoProc()));
boolean result = task.call();
System.err.println("javac result when missing resource: " + result);
expect(result, false);
if (errors > 0)
throw new Exception(errors + " errors occurred");
}
示例6: main
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
public static void main(String... args) throws Exception {
String testSrc = System.getProperty("test.src");
String testClasses = System.getProperty("test.classes");
JavaCompiler c = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) {
String thisName = TreePosRoundsTest.class.getName();
File thisFile = new File(testSrc, thisName + ".java");
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(thisFile);
List<String> options = Arrays.asList(
"-proc:only",
"-processor", thisName,
"-processorpath", testClasses);
CompilationTask t = c.getTask(null, fm, null, options, null, files);
boolean ok = t.call();
if (!ok)
throw new Exception("processing failed");
}
}
示例7: createFile
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
File createFile(Dir dir) throws IOException {
File src = new File(dir.file, classId + ".java");
try (FileWriter w = new FileWriter(src)) {
w.append("public class " + classId + " {}");
}
// If we're after the ".java" representation, we're done...
if(dir == Dir.SOURCE_PATH)
return src;
// ...otherwise compile into a ".class".
CompilationTask task = comp.getTask(null, fm, null, null, null,
fm.getJavaFileObjects(src));
File dest = new File(dir.file, classId + ".class");
if(!task.call() || !dest.exists())
throw new RuntimeException("Compilation failure.");
src.delete();
return dest;
}
示例8: main
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
public static void main(String[] args) {
// Get a compiler tool
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
String srcdir = System.getProperty("test.src");
File source = new File(srcdir, "T6378728.java");
StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
CompilationTask task =
compiler.getTask(null,
new ExceptionalFileManager(fm),
null,
Arrays.asList("-proc:only"),
null,
fm.getJavaFileObjectsFromFiles(Arrays.asList(source)));
if (!task.call())
throw new RuntimeException("Unexpected compilation failure");
}
示例9: compile
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
public synchronized Class compile(String packageName, String className, final CharSequence javaSource)
throws JdkCompileException,
ClassCastException {
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
JavaFileManagerImpl javaFileManager = buildFileManager(classLoader, classLoader.getParent(), diagnostics);
JavaFileObjectImpl source = new JavaFileObjectImpl(className, javaSource);
javaFileManager.putFileForInput(StandardLocation.SOURCE_PATH, packageName, className + EXTENSION, source);
CompilationTask task = compiler.getTask(null,
javaFileManager,
diagnostics,
options,
null,
Arrays.asList(source));
final Boolean result = task.call();
if (result == null || !result.booleanValue()) {
throw new JdkCompileException("Compilation failed.", diagnostics);
}
try {
return (Class<T>) classLoader.loadClass(packageName + "." + className);
} catch (Throwable e) {
throw new JdkCompileException(e, diagnostics);
}
}
示例10: compile
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
static byte[] compile(File file) {
try {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
File tempDir = getTempDir();
tempDir.mkdirs();
List<String> options = Arrays.asList("-proc:none", "-d", tempDir.getAbsolutePath());
StringWriter captureWarnings = new StringWriter();
final StringBuilder compilerErrors = new StringBuilder();
DiagnosticListener<JavaFileObject> diagnostics = new DiagnosticListener<JavaFileObject>() {
@Override public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
compilerErrors.append(diagnostic.toString()).append("\n");
}
};
CompilationTask task = compiler.getTask(captureWarnings, null, diagnostics, options, null, Collections.singleton(new ContentBasedJavaFileObject(file.getPath(), readFileAsString(file))));
Boolean taskResult = task.call();
assertTrue("Compilation task didn't succeed: \n<Warnings and Errors>\n" + compilerErrors.toString() + "\n</Warnings and Errors>", taskResult);
return PostCompilerApp.readFile(new File(tempDir, file.getName().replaceAll("\\.java$", ".class")));
} catch (Exception e) {
throw Lombok.sneakyThrow(e);
}
}
示例11: callTask
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
void callTask(List<String> options, List<JavaFileObject> files) {
out.print("compile: ");
if (options != null) {
for (String o: options) {
if (o.length() > 64) {
o = o.substring(0, 32) + "..." + o.substring(o.length() - 32);
}
out.print(" " + o);
}
}
for (JavaFileObject f: files)
out.print(" " + f.getName());
out.println();
CompilationTask t = compiler.getTask(out, fileManager, null, options, null, files);
boolean ok = t.call();
if (!ok)
error("compilation failed");
}
示例12: compile
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
public File compile(Config config) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
fileManager.setLocation(StandardLocation.SOURCE_PATH, Collections.singleton(tmpSrcDir));
File tmpClassDir = File.createTempFile("jpf-testing", "classes");
tmpClassDir.delete();
tmpClassDir.mkdir();
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(tmpClassDir));
File[] cp = config.getPathArray("classpath");
fileManager.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(cp));
CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjectsFromFiles(sourceFiles));
if(!task.call())
throw new RuntimeException("Compilation failed");
return tmpClassDir;
}
示例13: compile
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
public Class<?> compile(String sourceCode) throws DynamicClassCompilerException {
if (isCached(sourceCode)) {
return fromCache(sourceCode);
}
String name = findName(sourceCode);
String pkg = findPackage(sourceCode);
String fullQualifiedName = pkg + '.' + name;
JavaInMemoryFileManager fileManager = new JavaInMemoryFileManager(loader, compiler.getStandardFileManager(null, null, null));
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, asList((JavaFileObject) new JavaSourceFileObject(name, sourceCode)));
boolean success = task.call();
if (!success) {
throw new DynamicClassCompilerException("compile failed with messages", collectMessages(diagnostics.getDiagnostics()));
}
try {
Class<?> clazz = fileManager.getClassLoader(null).loadClass(fullQualifiedName);
cache(sourceCode, clazz);
return clazz;
} catch (ClassNotFoundException e) {
throw new DynamicClassCompilerException("class " + fullQualifiedName + " cannot be loaded: " + e.getMessage());
}
}
示例14: compileJavaFile
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
private static boolean compileJavaFile(String javaBinDirName, File javaFile) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
return false; //fail
}
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(
diagnostics, Locale.getDefault(), Charset.forName("UTF-8"));
Iterable<? extends JavaFileObject> compilationUnits = fileManager
.getJavaFileObjectsFromFiles(Collections
.singletonList(javaFile));
List<String> optionList;
optionList = new ArrayList<String>();
optionList.addAll(Arrays.asList("-d", javaBinDirName));
CompilationTask task = compiler.getTask(null, fileManager, diagnostics,
optionList, null, compilationUnits);
boolean compiled = task.call();
try {
fileManager.close();
} catch (IOException e) {
return false;
}
return compiled;
}
示例15: testSingleSourceDir
import javax.tools.JavaCompiler.CompilationTask; //导入方法依赖的package包/类
private void testSingleSourceDir(JavaCompiler javac) throws Exception {
System.err.println("testSingleSourceDir");
File tmpdir = new File("testSingleSourceDir");
File srcdir = new File(tmpdir, "src");
File destdir = new File(tmpdir, "dest");
write(srcdir, "pkg/X.java", "package pkg; class X {}");
write(srcdir, "resources/file.txt", "hello");
destdir.mkdirs();
CompilationTask task = javac.getTask(null, null, null,
Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
Collections.singleton("pkg.X"), null);
task.setProcessors(Collections.singleton(new AnnoProc()));
boolean result = task.call();
System.err.println("javac result with single source dir: " + result);
expect(result, true);
}