Polishing the compilation APIs dew
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 16 Jan 2013 11:07:28 +0100
branchdew
changeset 4649823859d253a
parent 463 3641fd0663d3
child 465 443eb0f21a0a
Polishing the compilation APIs
launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java
launcher/src/main/java/org/apidesign/bck2brwsr/dew/JFO.java
launcher/src/test/java/org/apidesign/bck2brwsr/dew/CompileTest.java
     1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java	Wed Jan 16 10:52:06 2013 +0100
     1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java	Wed Jan 16 11:07:28 2013 +0100
     1.3 @@ -1,7 +1,3 @@
     1.4 -/*
     1.5 - * To change this template, choose Tools | Templates
     1.6 - * and open the template in the editor.
     1.7 - */
     1.8  package org.apidesign.bck2brwsr.dew;
     1.9  
    1.10  import java.io.ByteArrayInputStream;
    1.11 @@ -11,26 +7,17 @@
    1.12  import java.io.OutputStream;
    1.13  import java.net.URI;
    1.14  import java.net.URISyntaxException;
    1.15 -import java.nio.charset.Charset;
    1.16  import java.util.ArrayList;
    1.17  import java.util.Arrays;
    1.18 -import java.util.Collections;
    1.19  import java.util.HashMap;
    1.20 -import java.util.Iterator;
    1.21  import java.util.List;
    1.22 -import java.util.Locale;
    1.23  import java.util.Map;
    1.24 -import java.util.Set;
    1.25 -import java.util.logging.Level;
    1.26 -import java.util.logging.Logger;
    1.27  import java.util.regex.Matcher;
    1.28  import java.util.regex.Pattern;
    1.29  import javax.tools.Diagnostic;
    1.30  import javax.tools.DiagnosticListener;
    1.31  import javax.tools.FileObject;
    1.32  import javax.tools.ForwardingJavaFileManager;
    1.33 -import javax.tools.JavaCompiler;
    1.34 -import javax.tools.JavaCompiler.CompilationTask;
    1.35  import javax.tools.JavaFileManager;
    1.36  import javax.tools.JavaFileObject;
    1.37  import javax.tools.JavaFileObject.Kind;
    1.38 @@ -43,51 +30,45 @@
    1.39   *
    1.40   * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.41   */
    1.42 -final class Compile implements JavaFileManager, DiagnosticListener<JavaFileObject> {
    1.43 -    private final JFO jfo;
    1.44 +final class Compile implements DiagnosticListener<JavaFileObject> {
    1.45 +    private final List<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<>();
    1.46 +    private final Map<String, byte[]> classes;
    1.47      private final String pkg;
    1.48 -    private StandardJavaFileManager delegate;
    1.49 -    private List<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<>();
    1.50 +    private final String cls;
    1.51  
    1.52 -    public Compile(String pkg, JFO jfo) {
    1.53 -        this.pkg = pkg;
    1.54 -        this.jfo = jfo;
    1.55 +    private Compile(String html, String code) throws IOException {
    1.56 +        this.pkg = findPkg(code);
    1.57 +        this.cls = findCls(code);
    1.58 +        classes = compile(html, code);
    1.59 +    }
    1.60 +
    1.61 +    /** Performs compilation of given HTML page and associated Java code
    1.62 +     */
    1.63 +    public static Compile create(String html, String code) throws IOException {
    1.64 +        return new Compile(html, code);
    1.65      }
    1.66      
    1.67 -    /*
    1.68 -    public static Map<String,byte[]> compile(String html, String java) throws IOException {
    1.69 -        JavaCompiler jc = javax.tools.ToolProvider.getSystemJavaCompiler();
    1.70 -        String pkg = findPkg(java);
    1.71 -        String cls = findCls(java);
    1.72 -        
    1.73 -        JFO jfo = new JFO(java, pkg.replace('.', '/') + '/' + cls + ".java");
    1.74 -        final Compile cmp = new Compile(pkg, jfo);
    1.75 -        cmp.delegate = jc.getStandardFileManager(cmp, Locale.ENGLISH, Charset.forName("UTF-8"));
    1.76 -        
    1.77 -        Set<String> toCmp = Collections.singleton(pkg + '.' + cls);
    1.78 -        Set<JFO> unit = Collections.singleton(jfo);
    1.79 -        CompilationTask task = jc.getTask(null, cmp, cmp, null, null, unit);
    1.80 -        if (task.call() != true) {
    1.81 -            throw new IOException("Compilation failed: " + cmp.errors);
    1.82 +    /** Checks for given class among compiled resources */
    1.83 +    public byte[] get(String res) {
    1.84 +        return classes.get(res);
    1.85 +    }
    1.86 +    
    1.87 +    /** Obtains errors created during compilation.
    1.88 +     */
    1.89 +    public List<Diagnostic<? extends JavaFileObject>> getErrors() {
    1.90 +        List<Diagnostic<? extends JavaFileObject>> err = new ArrayList<>();
    1.91 +        for (Diagnostic<? extends JavaFileObject> diagnostic : errors) {
    1.92 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
    1.93 +                err.add(diagnostic);
    1.94 +            }
    1.95          }
    1.96 -        return Collections.emptyMap();
    1.97 +        return err;
    1.98      }
    1.99 -    */
   1.100 -    public static Map<String, byte[]> compile(final String html, final String code) throws IOException {
   1.101 -        final String pkg = findPkg(code);
   1.102 -//        String cls = findCls(code);
   1.103 -        
   1.104 -        DiagnosticListener<JavaFileObject> devNull = new DiagnosticListener<JavaFileObject>() {
   1.105 -            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.106 -                System.err.println("diagnostic=" + diagnostic);
   1.107 -            }
   1.108 -        };
   1.109 -        StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(devNull, null, null);
   1.110 +    
   1.111 +    private Map<String, byte[]> compile(final String html, final String code) throws IOException {
   1.112 +        StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null);
   1.113  
   1.114 -//        sjfm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, toFiles(boot));
   1.115 -//        sjfm.setLocation(StandardLocation.CLASS_PATH, toFiles(compile));
   1.116 -
   1.117 -        final Map<String, ByteArrayOutputStream> class2BAOS = new HashMap<String, ByteArrayOutputStream>();
   1.118 +        final Map<String, ByteArrayOutputStream> class2BAOS = new HashMap<>();
   1.119  
   1.120          JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) {
   1.121              @Override
   1.122 @@ -147,28 +128,10 @@
   1.123                  
   1.124                  throw new IllegalStateException();
   1.125              }
   1.126 -//            @Override
   1.127 -//            public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException {
   1.128 -//                if (location == StandardLocation.PLATFORM_CLASS_PATH) {
   1.129 -//                    return super.list(location, packageName, kinds, recurse);
   1.130 -//                }
   1.131 -//                if (location == StandardLocation.CLASS_PATH) {
   1.132 -//                    return super.list(location, packageName, kinds, recurse);
   1.133 -//                }
   1.134 -//                if (location == StandardLocation.SOURCE_PATH) {
   1.135 -//                    System.out.println("src path for " + packageName + " kinds: " + kinds);
   1.136 -//                    if (packageName.equals(pkg) && kinds.contains(Kind.OTHER)) {
   1.137 -//                        return Collections.<JavaFileObject>singleton(htmlFile);
   1.138 -//                    }
   1.139 -//                    return Collections.emptyList();
   1.140 -//                }
   1.141 -//                throw new UnsupportedOperationException("Loc: " + location + " pkg: " + packageName + " kinds: " + kinds + " rec: " + recurse);
   1.142 -//            }
   1.143  
   1.144              @Override
   1.145              public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
   1.146                  if (location == StandardLocation.SOURCE_PATH) {
   1.147 -//                    System.out.println("src path for " + packageName + " kinds: " + kinds);
   1.148                      if (packageName.equals(pkg)) {
   1.149                          return htmlFile;
   1.150                      }
   1.151 @@ -179,9 +142,9 @@
   1.152              
   1.153          };
   1.154  
   1.155 -        ToolProvider.getSystemJavaCompiler().getTask(null, jfm, devNull, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call();
   1.156 +        ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call();
   1.157  
   1.158 -        Map<String, byte[]> result = new HashMap<String, byte[]>();
   1.159 +        Map<String, byte[]> result = new HashMap<>();
   1.160  
   1.161          for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) {
   1.162              result.put(e.getKey(), e.getValue().toByteArray());
   1.163 @@ -190,84 +153,6 @@
   1.164          return result;
   1.165      }
   1.166  
   1.167 -    @Override
   1.168 -    public ClassLoader getClassLoader(Location location) {
   1.169 -        return null;//Compile.class.getClassLoader();
   1.170 -    }
   1.171 -
   1.172 -    @Override
   1.173 -    public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException {
   1.174 -        if (location == StandardLocation.PLATFORM_CLASS_PATH) {
   1.175 -            return delegate.list(location, packageName, kinds, recurse);
   1.176 -        }
   1.177 -        if (location == StandardLocation.CLASS_PATH) {
   1.178 -            return delegate.list(location, packageName, kinds, recurse);
   1.179 -        }
   1.180 -        if (location == StandardLocation.SOURCE_PATH) {
   1.181 -            if (packageName.equals(pkg)) {
   1.182 -                return Collections.<JavaFileObject>singleton(jfo);
   1.183 -            }
   1.184 -            return Collections.emptyList();
   1.185 -        }
   1.186 -        throw new UnsupportedOperationException("Loc: " + location + " pkg: " + packageName + " kinds: " + kinds + " rec: " + recurse);
   1.187 -    }
   1.188 -
   1.189 -    @Override
   1.190 -    public String inferBinaryName(Location location, JavaFileObject file) {
   1.191 -        if (file == jfo) {
   1.192 -            return pkg + "." + file.getName();
   1.193 -        }
   1.194 -        return delegate.inferBinaryName(location, file);
   1.195 -    }
   1.196 -
   1.197 -    @Override
   1.198 -    public boolean isSameFile(FileObject a, FileObject b) {
   1.199 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.200 -    }
   1.201 -
   1.202 -    @Override
   1.203 -    public boolean handleOption(String current, Iterator<String> remaining) {
   1.204 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.205 -    }
   1.206 -
   1.207 -    @Override
   1.208 -    public boolean hasLocation(Location location) {
   1.209 -        return true;
   1.210 -    }
   1.211 -
   1.212 -    @Override
   1.213 -    public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException {
   1.214 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.215 -    }
   1.216 -
   1.217 -    @Override
   1.218 -    public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
   1.219 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.220 -    }
   1.221 -
   1.222 -    @Override
   1.223 -    public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
   1.224 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.225 -    }
   1.226 -
   1.227 -    @Override
   1.228 -    public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException {
   1.229 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.230 -    }
   1.231 -
   1.232 -    @Override
   1.233 -    public void flush() throws IOException {
   1.234 -    }
   1.235 -
   1.236 -    @Override
   1.237 -    public void close() throws IOException {
   1.238 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.239 -    }
   1.240 -
   1.241 -    @Override
   1.242 -    public int isSupportedOption(String option) {
   1.243 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.244 -    }
   1.245  
   1.246      @Override
   1.247      public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
     2.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/dew/JFO.java	Wed Jan 16 10:52:06 2013 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,96 +0,0 @@
     2.4 -/*
     2.5 - * To change this template, choose Tools | Templates
     2.6 - * and open the template in the editor.
     2.7 - */
     2.8 -package org.apidesign.bck2brwsr.dew;
     2.9 -
    2.10 -import java.io.IOException;
    2.11 -import java.io.InputStream;
    2.12 -import java.io.OutputStream;
    2.13 -import java.io.Reader;
    2.14 -import java.io.StringReader;
    2.15 -import java.io.Writer;
    2.16 -import java.net.URI;
    2.17 -import javax.lang.model.element.Modifier;
    2.18 -import javax.lang.model.element.NestingKind;
    2.19 -import javax.tools.JavaFileObject;
    2.20 -
    2.21 -/**
    2.22 - *
    2.23 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.24 - */
    2.25 -final class JFO implements JavaFileObject {
    2.26 -    private final String text;
    2.27 -    private final String name;
    2.28 -
    2.29 -    public JFO(String text, String name) {
    2.30 -        this.text = text;
    2.31 -        this.name = name;
    2.32 -    }
    2.33 -
    2.34 -    @Override
    2.35 -    public Kind getKind() {
    2.36 -        return Kind.SOURCE;
    2.37 -    }
    2.38 -
    2.39 -    @Override
    2.40 -    public boolean isNameCompatible(String simpleName, Kind kind) {
    2.41 -        return false;
    2.42 -    }
    2.43 -
    2.44 -    @Override
    2.45 -    public NestingKind getNestingKind() {
    2.46 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.47 -    }
    2.48 -
    2.49 -    @Override
    2.50 -    public Modifier getAccessLevel() {
    2.51 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.52 -    }
    2.53 -
    2.54 -    @Override
    2.55 -    public URI toUri() {
    2.56 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.57 -    }
    2.58 -
    2.59 -    @Override
    2.60 -    public String getName() {
    2.61 -        return name;
    2.62 -    }
    2.63 -
    2.64 -    @Override
    2.65 -    public InputStream openInputStream() throws IOException {
    2.66 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.67 -    }
    2.68 -
    2.69 -    @Override
    2.70 -    public OutputStream openOutputStream() throws IOException {
    2.71 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.72 -    }
    2.73 -
    2.74 -    @Override
    2.75 -    public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
    2.76 -        return new StringReader(text);
    2.77 -    }
    2.78 -
    2.79 -    @Override
    2.80 -    public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    2.81 -        return text;
    2.82 -    }
    2.83 -
    2.84 -    @Override
    2.85 -    public Writer openWriter() throws IOException {
    2.86 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.87 -    }
    2.88 -
    2.89 -    @Override
    2.90 -    public long getLastModified() {
    2.91 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.92 -    }
    2.93 -
    2.94 -    @Override
    2.95 -    public boolean delete() {
    2.96 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.97 -    }
    2.98 -    
    2.99 -}
     3.1 --- a/launcher/src/test/java/org/apidesign/bck2brwsr/dew/CompileTest.java	Wed Jan 16 10:52:06 2013 +0100
     3.2 +++ b/launcher/src/test/java/org/apidesign/bck2brwsr/dew/CompileTest.java	Wed Jan 16 11:07:28 2013 +0100
     3.3 @@ -1,11 +1,6 @@
     3.4 -/*
     3.5 - * To change this template, choose Tools | Templates
     3.6 - * and open the template in the editor.
     3.7 - */
     3.8  package org.apidesign.bck2brwsr.dew;
     3.9  
    3.10  import java.io.IOException;
    3.11 -import java.util.Map;
    3.12  import static org.testng.Assert.*;
    3.13  import org.testng.annotations.Test;
    3.14  
    3.15 @@ -24,7 +19,7 @@
    3.16              + "class X { "
    3.17              + "   @OnClick(id=\"btn\") static void clcs() {}"
    3.18              + "}";
    3.19 -        Map<String,byte[]> result = Compile.compile(html, java);
    3.20 +        Compile result = Compile.create(html, java);
    3.21  
    3.22          assertNotNull(result.get("x/y/z/X.class"), "Class X is compiled: " + result);
    3.23          assertNotNull(result.get("x/y/z/Index.class"), "Class Index is compiled: " + result);