# HG changeset patch # User Jaroslav Tulach # Date 1358330848 -3600 # Node ID 9823859d253af68de28680164fd84a195fdf2939 # Parent 3641fd0663d3e2e8566fd68c1ce4b64d03414f36 Polishing the compilation APIs diff -r 3641fd0663d3 -r 9823859d253a launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java Wed Jan 16 10:52:06 2013 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java Wed Jan 16 11:07:28 2013 +0100 @@ -1,7 +1,3 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ package org.apidesign.bck2brwsr.dew; import java.io.ByteArrayInputStream; @@ -11,26 +7,17 @@ import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; import java.util.List; -import java.util.Locale; import java.util.Map; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; 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.ForwardingJavaFileManager; -import javax.tools.JavaCompiler; -import javax.tools.JavaCompiler.CompilationTask; import javax.tools.JavaFileManager; import javax.tools.JavaFileObject; import javax.tools.JavaFileObject.Kind; @@ -43,51 +30,45 @@ * * @author Jaroslav Tulach */ -final class Compile implements JavaFileManager, DiagnosticListener { - private final JFO jfo; +final class Compile implements DiagnosticListener { + private final List> errors = new ArrayList<>(); + private final Map classes; private final String pkg; - private StandardJavaFileManager delegate; - private List> errors = new ArrayList<>(); + private final String cls; - public Compile(String pkg, JFO jfo) { - this.pkg = pkg; - this.jfo = jfo; + private Compile(String html, String code) throws IOException { + this.pkg = findPkg(code); + this.cls = findCls(code); + classes = compile(html, code); + } + + /** Performs compilation of given HTML page and associated Java code + */ + public static Compile create(String html, String code) throws IOException { + return new Compile(html, code); } - /* - 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); + /** Checks for given class among compiled resources */ + public byte[] get(String res) { + return classes.get(res); + } + + /** Obtains errors created during compilation. + */ + public List> getErrors() { + List> err = new ArrayList<>(); + for (Diagnostic diagnostic : errors) { + if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { + err.add(diagnostic); + } } - return Collections.emptyMap(); + return err; } - */ - public static Map compile(final String html, final String code) throws IOException { - final String pkg = findPkg(code); -// String cls = findCls(code); - - DiagnosticListener devNull = new DiagnosticListener() { - public void report(Diagnostic diagnostic) { - System.err.println("diagnostic=" + diagnostic); - } - }; - StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(devNull, null, null); + + private Map compile(final String html, final String code) throws IOException { + StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null); -// sjfm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, toFiles(boot)); -// sjfm.setLocation(StandardLocation.CLASS_PATH, toFiles(compile)); - - final Map class2BAOS = new HashMap(); + final Map class2BAOS = new HashMap<>(); JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) { @Override @@ -147,28 +128,10 @@ throw new IllegalStateException(); } -// @Override -// public Iterable list(Location location, String packageName, Set kinds, boolean recurse) throws IOException { -// if (location == StandardLocation.PLATFORM_CLASS_PATH) { -// return super.list(location, packageName, kinds, recurse); -// } -// if (location == StandardLocation.CLASS_PATH) { -// return super.list(location, packageName, kinds, recurse); -// } -// if (location == StandardLocation.SOURCE_PATH) { -// System.out.println("src path for " + packageName + " kinds: " + kinds); -// if (packageName.equals(pkg) && kinds.contains(Kind.OTHER)) { -// return Collections.singleton(htmlFile); -// } -// return Collections.emptyList(); -// } -// throw new UnsupportedOperationException("Loc: " + location + " pkg: " + packageName + " kinds: " + kinds + " rec: " + recurse); -// } @Override public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException { if (location == StandardLocation.SOURCE_PATH) { -// System.out.println("src path for " + packageName + " kinds: " + kinds); if (packageName.equals(pkg)) { return htmlFile; } @@ -179,9 +142,9 @@ }; - ToolProvider.getSystemJavaCompiler().getTask(null, jfm, devNull, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call(); + ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call(); - Map result = new HashMap(); + Map result = new HashMap<>(); for (Map.Entry e : class2BAOS.entrySet()) { result.put(e.getKey(), e.getValue().toByteArray()); @@ -190,84 +153,6 @@ return result; } - @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) { diff -r 3641fd0663d3 -r 9823859d253a launcher/src/main/java/org/apidesign/bck2brwsr/dew/JFO.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/dew/JFO.java Wed Jan 16 10:52:06 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,96 +0,0 @@ -/* - * 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 3641fd0663d3 -r 9823859d253a launcher/src/test/java/org/apidesign/bck2brwsr/dew/CompileTest.java --- a/launcher/src/test/java/org/apidesign/bck2brwsr/dew/CompileTest.java Wed Jan 16 10:52:06 2013 +0100 +++ b/launcher/src/test/java/org/apidesign/bck2brwsr/dew/CompileTest.java Wed Jan 16 11:07:28 2013 +0100 @@ -1,11 +1,6 @@ -/* - * 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; @@ -24,7 +19,7 @@ + "class X { " + " @OnClick(id=\"btn\") static void clcs() {}" + "}"; - Map result = Compile.compile(html, java); + Compile result = Compile.create(html, java); assertNotNull(result.get("x/y/z/X.class"), "Class X is compiled: " + result); assertNotNull(result.get("x/y/z/Index.class"), "Class Index is compiled: " + result);