List of usage examples for javax.tools StandardJavaFileManager getJavaFileObjects
Iterable<? extends JavaFileObject> getJavaFileObjects(String... names);
From source file:Main.java
public static void main(String[] args) { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>(); StandardJavaFileManager sjfm = compiler.getStandardFileManager(dc, null, null); Iterable<? extends JavaFileObject> fileObjects = sjfm.getJavaFileObjects(args); compiler.getTask(null, sjfm, dc, null, null, fileObjects).call(); for (Diagnostic d : dc.getDiagnostics()) { System.out.println(d.getMessage(null)); System.out.printf("Line number = %d\n", d.getLineNumber()); System.out.printf("File = %s\n", d.getSource()); }/* ww w . j a va 2 s .c om*/ }
From source file:CompileFiles2.java
public static void main(String[] args) { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> dc; dc = new DiagnosticCollector<JavaFileObject>(); StandardJavaFileManager sjfm; sjfm = compiler.getStandardFileManager(dc, null, null); Iterable<? extends JavaFileObject> fileObjects; fileObjects = sjfm.getJavaFileObjects(args); compiler.getTask(null, sjfm, dc, null, null, fileObjects).call(); for (Diagnostic d : dc.getDiagnostics()) { System.out.println(d.getMessage(null)); System.out.printf("Line number = %d\n", d.getLineNumber()); System.out.printf("File = %s\n", d.getSource()); }/* ww w. j a va 2s . c o m*/ }
From source file:clear.cdb.support.test.AnnotationProcessorTestCompiler.java
private static Iterable<? extends JavaFileObject> getCompilationUnitsOfClasses( StandardJavaFileManager fileManager, String[] classesToCompile) throws IOException { return fileManager.getJavaFileObjects(classesToCompile); }
From source file:com.github.aliakhtar.annoTest.util.Compiler.java
private boolean compile(Processor processor, SourceFile... sourceFiles) throws Exception { Builder<String> builder = ImmutableList.builder(); builder.add("-classpath").add(buildClassPath(outputDir)); builder.add("-d").add(outputDir.getAbsolutePath()); for (Map.Entry<String, String> entry : parameterMap.entrySet()) { builder.add("-A" + entry.getKey() + "=" + entry.getValue()); }/*from ww w . jav a2 s . c o m*/ File[] files = new File[sourceFiles.length]; for (int i = 0; i < sourceFiles.length; i++) { files[i] = writeSourceFile(sourceFiles[i]); } JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> javaFileObjects = fileManager.getJavaFileObjects(files); CompilationTask compilationTask = compiler.getTask(null, null, null, builder.build(), null, javaFileObjects); if (processor != null) compilationTask.setProcessors(Arrays.asList(processor)); Boolean success = compilationTask.call(); fileManager.close(); return success; }
From source file:net.morematerials.manager.HandlerManager.java
private void compile(File file) throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file); CompilationTask task = compiler.getTask(null, fileManager, null, this.compilerOptions, null, compilationUnits);/* www .j a va2 s.c om*/ task.call(); }
From source file:de.monticore.codegen.GeneratorTest.java
/** * Creates an Iterable over all the JavaFileObjects contained in a given * directory.//w ww . jav a 2 s . c o m * * @param sourceCodePath the directory from which JavaFileObjects are to be * retrieved * @param fileManager the StandardJavaFileManager to be used for the * JavaFileObject creation * @return the JavaFileObjects contained in the given directory */ protected Iterable<? extends JavaFileObject> getJavaFileObjects(Path sourceCodePath, StandardJavaFileManager fileManager) { Collection<File> files = FileUtils.listFiles(sourceCodePath.toFile(), new String[] { "java" }, true); return fileManager.getJavaFileObjects(files.toArray(new File[files.size()])); }
From source file:gruifo.output.jsni.BaseJsTest.java
private boolean compile(final String file) { final File fileToCompile = getJavaSourceFile(file); final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); assertNotNull("No compiler installed", compiler); final List<String> options = new ArrayList<>(); options.add("-source"); options.add("1.6"); options.add("-Xlint:-options"); options.add("-classpath"); options.add(getClass().getProtectionDomain().getCodeSource().getLocation().getFile()); final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); final JavaCompiler.CompilationTask task = compiler.getTask(/*default System.err*/ null, /*std file manager*/ null, /*std DiagnosticListener */ null, /*compiler options*/ options, /*no annotation*/ null, fileManager.getJavaFileObjects(fileToCompile)); return task.call(); }
From source file:com.legstar.protobuf.cobol.ProtoCobol.java
/** * Compile a java source file.//from w ww . jav a2 s. c o m * * @param javaOut where, on the file system, the generated java class will * be produced * @param javaSourceFile the java source file * @return the java class loaded * @throws ProtoCobolException if compilation fails */ public Class<?> runJavaCompiler(File javaOut, File javaSourceFile) throws ProtoCobolException { logger.info("About to compile " + javaSourceFile.getAbsolutePath()); JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); if (javaCompiler == null) { throw new ProtoCobolException("You need to have the JDK tools.jar on the classpath"); } StandardJavaFileManager manager = javaCompiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> units = manager.getJavaFileObjects(javaSourceFile.getAbsolutePath()); String[] opts = new String[] { "-d", javaOut.getAbsolutePath() }; CompilationTask task = javaCompiler.getTask(null, manager, null, Arrays.asList(opts), null, units); boolean status = task.call(); if (status) { logger.info("Compilation successful"); } else { throw new ProtoCobolException("Compilation failed for " + javaSourceFile.getAbsolutePath()); } return loadClass(javaOut, getRelativeClassName(javaOut, javaSourceFile)); }
From source file:com.sillelien.dollar.DocTestingVisitor.java
@Override public void visit(@NotNull VerbatimNode node) { if ("java".equals(node.getType())) { try {// w w w .ja va 2 s. c o m String name = "DocTemp" + System.currentTimeMillis(); File javaFile = new File("/tmp/" + name + ".java"); File clazzFile = new File("/tmp/" + name + ".class"); clazzFile.getParentFile().mkdirs(); FileUtils.write(javaFile, "import com.sillelien.dollar.api.*;\n" + "import static com.sillelien.dollar.api.DollarStatic.*;\n" + "public class " + name + " implements java.lang.Runnable{\n" + " public void run() {\n" + " " + node.getText() + "\n" + " }\n" + "}"); final JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); final StandardJavaFileManager jfm = javac.getStandardFileManager(null, null, null); JavaCompiler.CompilationTask task; DiagnosticListener<JavaFileObject> diagnosticListener = new DiagnosticListener<JavaFileObject>() { @Override public void report(Diagnostic diagnostic) { System.out.println(diagnostic); throw new RuntimeException(diagnostic.getMessage(Locale.getDefault())); } }; try (FileOutputStream fileOutputStream = FileUtils.openOutputStream(clazzFile)) { task = javac.getTask(new OutputStreamWriter(fileOutputStream), jfm, diagnosticListener, null, null, jfm.getJavaFileObjects(javaFile)); } task.call(); try { // Convert File to a URL URL url = clazzFile.getParentFile().toURL(); URL[] urls = new URL[] { url }; ClassLoader cl = new URLClassLoader(urls); Class cls = cl.loadClass(name); ((Runnable) cls.newInstance()).run(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } System.out.println("Parsed: " + node.getText()); } catch (IOException e) { throw new RuntimeException(e); } } }
From source file:org.apache.hadoop.hbase.util.ClassLoaderTestHelper.java
/** * Create a test jar for testing purpose for a given class * name with specified code string.// ww w .j a v a 2s.c om * * @param testDir the folder under which to store the test class * @param className the test class name * @param code the optional test class code, which can be null. * If null, an empty class will be used * @param folder the folder under which to store the generated jar * @return the test jar file generated */ public static File buildJar(String testDir, String className, String code, String folder) throws Exception { String javaCode = code != null ? code : "public class " + className + " {}"; Path srcDir = new Path(testDir, "src"); File srcDirPath = new File(srcDir.toString()); srcDirPath.mkdirs(); File sourceCodeFile = new File(srcDir.toString(), className + ".java"); BufferedWriter bw = new BufferedWriter(new FileWriter(sourceCodeFile)); bw.write(javaCode); bw.close(); // compile it by JavaCompiler JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); ArrayList<String> srcFileNames = new ArrayList<String>(); srcFileNames.add(sourceCodeFile.toString()); StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> cu = fm.getJavaFileObjects(sourceCodeFile); List<String> options = new ArrayList<String>(); options.add("-classpath"); // only add hbase classes to classpath. This is a little bit tricky: assume // the classpath is {hbaseSrc}/target/classes. String currentDir = new File(".").getAbsolutePath(); String classpath = currentDir + File.separator + "target" + File.separator + "classes" + System.getProperty("path.separator") + System.getProperty("java.class.path") + System.getProperty("path.separator") + System.getProperty("surefire.test.class.path"); options.add(classpath); LOG.debug("Setting classpath to: " + classpath); JavaCompiler.CompilationTask task = compiler.getTask(null, fm, null, options, null, cu); assertTrue("Compile file " + sourceCodeFile + " failed.", task.call()); // build a jar file by the classes files String jarFileName = className + ".jar"; File jarFile = new File(folder, jarFileName); jarFile.getParentFile().mkdirs(); if (!createJarArchive(jarFile, new File[] { new File(srcDir.toString(), className + ".class") })) { assertTrue("Build jar file failed.", false); } return jarFile; }