Trying to compile Java source via the javax.tools.ToolProvider.getSystemJavaCompiler
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
5 package org.apidesign.bck2brwsr.dew;
7 import java.io.IOException;
8 import java.nio.charset.Charset;
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Locale;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18 import javax.tools.Diagnostic;
19 import javax.tools.DiagnosticListener;
20 import javax.tools.FileObject;
21 import javax.tools.JavaCompiler;
22 import javax.tools.JavaCompiler.CompilationTask;
23 import javax.tools.JavaFileManager;
24 import javax.tools.JavaFileObject;
25 import javax.tools.JavaFileObject.Kind;
26 import javax.tools.StandardJavaFileManager;
27 import javax.tools.StandardLocation;
31 * @author Jaroslav Tulach <jtulach@netbeans.org>
33 final class Compile implements JavaFileManager, DiagnosticListener<JavaFileObject> {
34 private final JFO jfo;
35 private final String pkg;
36 private StandardJavaFileManager delegate;
37 private List<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<>();
39 public Compile(String pkg, JFO jfo) {
44 public static Map<String,byte[]> compile(String html, String java) throws IOException {
45 JavaCompiler jc = javax.tools.ToolProvider.getSystemJavaCompiler();
46 String pkg = findPkg(java);
47 String cls = findCls(java);
49 JFO jfo = new JFO(java, pkg.replace('.', '/') + '/' + cls + ".java");
50 final Compile cmp = new Compile(pkg, jfo);
51 cmp.delegate = jc.getStandardFileManager(cmp, Locale.ENGLISH, Charset.forName("UTF-8"));
53 Set<String> toCmp = Collections.singleton(pkg + '.' + cls);
54 Set<JFO> unit = Collections.singleton(jfo);
55 CompilationTask task = jc.getTask(null, cmp, cmp, null, null, unit);
56 if (task.call() != true) {
57 throw new IOException("Compilation failed: " + cmp.errors);
59 return Collections.emptyMap();
63 public ClassLoader getClassLoader(Location location) {
64 return null;//Compile.class.getClassLoader();
68 public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException {
69 if (location == StandardLocation.PLATFORM_CLASS_PATH) {
70 return delegate.list(location, packageName, kinds, recurse);
72 if (location == StandardLocation.CLASS_PATH) {
73 return delegate.list(location, packageName, kinds, recurse);
75 if (location == StandardLocation.SOURCE_PATH) {
76 if (packageName.equals(pkg)) {
77 return Collections.<JavaFileObject>singleton(jfo);
79 return Collections.emptyList();
81 throw new UnsupportedOperationException("Loc: " + location + " pkg: " + packageName + " kinds: " + kinds + " rec: " + recurse);
85 public String inferBinaryName(Location location, JavaFileObject file) {
87 return pkg + "." + file.getName();
89 return delegate.inferBinaryName(location, file);
93 public boolean isSameFile(FileObject a, FileObject b) {
94 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
98 public boolean handleOption(String current, Iterator<String> remaining) {
99 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
103 public boolean hasLocation(Location location) {
108 public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException {
109 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
113 public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
114 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
118 public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
119 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
123 public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException {
124 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
128 public void flush() throws IOException {
132 public void close() throws IOException {
133 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
137 public int isSupportedOption(String option) {
138 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
142 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
143 errors.add(diagnostic);
145 private static String findPkg(String java) throws IOException {
146 Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE);
147 Matcher m = p.matcher(java);
149 throw new IOException("Can't find package declaration in the java file");
151 String pkg = m.group(1);
154 private static String findCls(String java) throws IOException {
155 Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
156 Matcher m = p.matcher(java);
158 throw new IOException("Can't find package declaration in the java file");
160 String cls = m.group(1);