# HG changeset patch # User Jaroslav Tulach # Date 1358286497 -3600 # Node ID aa69b138762439a84d58f28ebbb050a7512806de # Parent ccc3fd318cb1debdb8c49fcc061685df1e1340ad Trying to compile Java source via the javax.tools.ToolProvider.getSystemJavaCompiler diff -r ccc3fd318cb1 -r aa69b1387624 launcher/nb-configuration.xml --- a/launcher/nb-configuration.xml Tue Jan 15 16:56:18 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,18 +0,0 @@ - - - - - - app - - diff -r ccc3fd318cb1 -r aa69b1387624 launcher/pom.xml --- a/launcher/pom.xml Tue Jan 15 16:56:18 2013 +0100 +++ b/launcher/pom.xml Tue Jan 15 22:48:17 2013 +0100 @@ -30,12 +30,6 @@ - junit - junit - 3.8.1 - test - - org.glassfish.grizzly grizzly-http-server 2.2.19 @@ -50,5 +44,16 @@ json 20090211 + + org.testng + testng + test + + + junit + junit + + + diff -r ccc3fd318cb1 -r aa69b1387624 launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java Tue Jan 15 22:48:17 2013 +0100 @@ -0,0 +1,163 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.apidesign.bck2brwsr.dew; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.tools.Diagnostic; +import javax.tools.DiagnosticListener; +import javax.tools.FileObject; +import javax.tools.JavaCompiler; +import javax.tools.JavaCompiler.CompilationTask; +import javax.tools.JavaFileManager; +import javax.tools.JavaFileObject; +import javax.tools.JavaFileObject.Kind; +import javax.tools.StandardJavaFileManager; +import javax.tools.StandardLocation; + +/** + * + * @author Jaroslav Tulach + */ +final class Compile implements JavaFileManager, DiagnosticListener { + private final JFO jfo; + private final String pkg; + private StandardJavaFileManager delegate; + private List> errors = new ArrayList<>(); + + public Compile(String pkg, JFO jfo) { + this.pkg = pkg; + this.jfo = jfo; + } + + public static Map compile(String html, String java) throws IOException { + JavaCompiler jc = javax.tools.ToolProvider.getSystemJavaCompiler(); + String pkg = findPkg(java); + String cls = findCls(java); + + JFO jfo = new JFO(java, pkg.replace('.', '/') + '/' + cls + ".java"); + final Compile cmp = new Compile(pkg, jfo); + cmp.delegate = jc.getStandardFileManager(cmp, Locale.ENGLISH, Charset.forName("UTF-8")); + + Set toCmp = Collections.singleton(pkg + '.' + cls); + Set unit = Collections.singleton(jfo); + CompilationTask task = jc.getTask(null, cmp, cmp, null, null, unit); + if (task.call() != true) { + throw new IOException("Compilation failed: " + cmp.errors); + } + return Collections.emptyMap(); + } + + @Override + public ClassLoader getClassLoader(Location location) { + return null;//Compile.class.getClassLoader(); + } + + @Override + public Iterable list(Location location, String packageName, Set kinds, boolean recurse) throws IOException { + if (location == StandardLocation.PLATFORM_CLASS_PATH) { + return delegate.list(location, packageName, kinds, recurse); + } + if (location == StandardLocation.CLASS_PATH) { + return delegate.list(location, packageName, kinds, recurse); + } + if (location == StandardLocation.SOURCE_PATH) { + if (packageName.equals(pkg)) { + return Collections.singleton(jfo); + } + return Collections.emptyList(); + } + throw new UnsupportedOperationException("Loc: " + location + " pkg: " + packageName + " kinds: " + kinds + " rec: " + recurse); + } + + @Override + public String inferBinaryName(Location location, JavaFileObject file) { + if (file == jfo) { + return pkg + "." + file.getName(); + } + return delegate.inferBinaryName(location, file); + } + + @Override + public boolean isSameFile(FileObject a, FileObject b) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean handleOption(String current, Iterator remaining) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean hasLocation(Location location) { + return true; + } + + @Override + public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void flush() throws IOException { + } + + @Override + public void close() throws IOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public int isSupportedOption(String option) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void report(Diagnostic diagnostic) { + errors.add(diagnostic); + } + private static String findPkg(String java) throws IOException { + Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE); + Matcher m = p.matcher(java); + if (!m.find()) { + throw new IOException("Can't find package declaration in the java file"); + } + String pkg = m.group(1); + return pkg; + } + private static String findCls(String java) throws IOException { + Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE); + Matcher m = p.matcher(java); + if (!m.find()) { + throw new IOException("Can't find package declaration in the java file"); + } + String cls = m.group(1); + return cls; + } +} diff -r ccc3fd318cb1 -r aa69b1387624 launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java Tue Jan 15 16:56:18 2013 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java Tue Jan 15 22:48:17 2013 +0100 @@ -24,7 +24,8 @@ * @author phrebejk */ public class Dew extends HttpHandler { - private static String html = "Nazdar!"; + private String html = "Nazdar!"; + private String java = "class C {\n}\n"; @Override public void service(Request request, Response response) throws Exception { @@ -34,7 +35,9 @@ JSONTokener tok = new JSONTokener(new InputStreamReader(is)); JSONObject obj = new JSONObject(tok); html = obj.getString("html"); + java = obj.getString("java"); LOG.info(html); + LOG.info(java); response.getOutputStream().write("[]".getBytes()); response.setStatus(HttpStatus.OK_200); diff -r ccc3fd318cb1 -r aa69b1387624 launcher/src/main/java/org/apidesign/bck2brwsr/dew/JFO.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/dew/JFO.java Tue Jan 15 22:48:17 2013 +0100 @@ -0,0 +1,96 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.apidesign.bck2brwsr.dew; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.StringReader; +import java.io.Writer; +import java.net.URI; +import javax.lang.model.element.Modifier; +import javax.lang.model.element.NestingKind; +import javax.tools.JavaFileObject; + +/** + * + * @author Jaroslav Tulach + */ +final class JFO implements JavaFileObject { + private final String text; + private final String name; + + public JFO(String text, String name) { + this.text = text; + this.name = name; + } + + @Override + public Kind getKind() { + return Kind.SOURCE; + } + + @Override + public boolean isNameCompatible(String simpleName, Kind kind) { + return false; + } + + @Override + public NestingKind getNestingKind() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Modifier getAccessLevel() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public URI toUri() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getName() { + return name; + } + + @Override + public InputStream openInputStream() throws IOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public OutputStream openOutputStream() throws IOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Reader openReader(boolean ignoreEncodingErrors) throws IOException { + return new StringReader(text); + } + + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { + return text; + } + + @Override + public Writer openWriter() throws IOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public long getLastModified() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean delete() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + +} diff -r ccc3fd318cb1 -r aa69b1387624 launcher/src/test/java/org/apidesign/bck2brwsr/dew/CompileTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/test/java/org/apidesign/bck2brwsr/dew/CompileTest.java Tue Jan 15 22:48:17 2013 +0100 @@ -0,0 +1,28 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.apidesign.bck2brwsr.dew; + +import java.io.IOException; +import java.util.Map; +import static org.testng.Assert.*; +import org.testng.annotations.Test; + +/** + * + * @author Jaroslav Tulach + */ +public class CompileTest { + @Test public void testCompile() throws IOException { + String html = "" + + " " + + ""; + String java = "package x.y.z;" + + "class X {" + + "}"; + Map result = Compile.compile(html, java); + + assertNotNull(result.get("x/y/z/X.class"), "Class X is compiled: " + result); + } +}