List of usage examples for javax.tools JavaFileManager close
@Override void close() throws IOException;
From source file:org.glowroot.common2.repo.util.Compilations.java
public static Class<?> compile(String source) throws Exception { JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>(); IsolatedClassLoader isolatedClassLoader = new IsolatedClassLoader(); StandardJavaFileManager standardFileManager = javaCompiler.getStandardFileManager(diagnosticCollector, Locale.ENGLISH, UTF_8); standardFileManager.setLocation(StandardLocation.CLASS_PATH, getCompilationClassPath()); JavaFileManager fileManager = new IsolatedJavaFileManager(standardFileManager, isolatedClassLoader); try {// www . ja va2 s . c om List<JavaFileObject> compilationUnits = Lists.newArrayList(); String className = getPublicClassName(source); int index = className.lastIndexOf('.'); String simpleName; if (index == -1) { simpleName = className; } else { simpleName = className.substring(index + 1); } compilationUnits.add(new SourceJavaFileObject(simpleName, source)); JavaCompiler.CompilationTask task = javaCompiler.getTask(null, fileManager, diagnosticCollector, null, null, compilationUnits); task.call(); List<Diagnostic<? extends JavaFileObject>> diagnostics = diagnosticCollector.getDiagnostics(); if (!diagnostics.isEmpty()) { List<String> compilationErrors = Lists.newArrayList(); for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) { compilationErrors.add(checkNotNull(diagnostic.toString())); } throw new CompilationException(compilationErrors); } if (className.isEmpty()) { throw new CompilationException(ImmutableList.of("Class must be public")); } return isolatedClassLoader.loadClass(className); } finally { fileManager.close(); } }
From source file:org.auroraide.server.jci.compilers.Jsr199JavaCompiler.java
public CompilationResult compile(final String[] pResourcePaths, final ResourceReader pReader, final ResourceStore pStore, final ClassLoader classLoader, JavaCompilerSettings settings) { final Collection<JavaFileObject> units = new ArrayList<JavaFileObject>(); for (int i = 0; i < pResourcePaths.length; i++) { final String sourcePath = pResourcePaths[i]; log.debug("compiling " + sourcePath); final String source = pResourcePaths[i]; if (pReader.isAvailable(source)) units.add(new CompilationUnit(sourcePath, pReader)); }/*from ww w.j a va 2 s . c o m*/ final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); final JavaFileManager fileManager = new JciJavaFileManager(units, pStore); final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); // final JavaFileManager fileManager1 = // compiler.getStandardFileManager(null,null,null); System.getProperties(); compiler.getTask(null, fileManager, diagnostics, options, null, units).call(); try { fileManager.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } final List<Diagnostic<? extends JavaFileObject>> jsrProblems = diagnostics.getDiagnostics(); final CompilationProblem[] problems = new CompilationProblem[jsrProblems.size()]; int i = 0; for (final Diagnostic<? extends JavaFileObject> jsrProblem : jsrProblems) { problems[i++] = new Jsr199CompilationProblem(jsrProblem); if (problemHandler != null) { problemHandler.handle(problems[i - 1]); } } return new CompilationResult(problems); }