After Lahvac's tweaks the compilation seems to run OK dew
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 16 Jan 2013 10:52:06 +0100
branchdew
changeset 4633641fd0663d3
parent 462 aa69b1387624
child 464 9823859d253a
After Lahvac's tweaks the compilation seems to run OK
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java
launcher/pom.xml
launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java
launcher/src/test/java/org/apidesign/bck2brwsr/dew/CompileTest.java
     1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Tue Jan 15 22:48:17 2013 +0100
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Wed Jan 16 10:52:06 2013 +0100
     1.3 @@ -62,6 +62,9 @@
     1.4      public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
     1.5          for (Element e : roundEnv.getElementsAnnotatedWith(Page.class)) {
     1.6              Page p = e.getAnnotation(Page.class);
     1.7 +            if (p == null) {
     1.8 +                continue;
     1.9 +            }
    1.10              PackageElement pe = (PackageElement)e.getEnclosingElement();
    1.11              String pkg = pe.getQualifiedName().toString();
    1.12              
     2.1 --- a/launcher/pom.xml	Tue Jan 15 22:48:17 2013 +0100
     2.2 +++ b/launcher/pom.xml	Wed Jan 16 10:52:06 2013 +0100
     2.3 @@ -55,5 +55,10 @@
     2.4          </exclusion>
     2.5        </exclusions>
     2.6      </dependency>
     2.7 +    <dependency>
     2.8 +      <groupId>${project.groupId}</groupId>
     2.9 +      <artifactId>javaquery.api</artifactId>
    2.10 +      <version>${project.version}</version>
    2.11 +    </dependency>
    2.12    </dependencies>
    2.13  </project>
     3.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java	Tue Jan 15 22:48:17 2013 +0100
     3.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java	Wed Jan 16 10:52:06 2013 +0100
     3.3 @@ -4,27 +4,40 @@
     3.4   */
     3.5  package org.apidesign.bck2brwsr.dew;
     3.6  
     3.7 +import java.io.ByteArrayInputStream;
     3.8 +import java.io.ByteArrayOutputStream;
     3.9  import java.io.IOException;
    3.10 +import java.io.InputStream;
    3.11 +import java.io.OutputStream;
    3.12 +import java.net.URI;
    3.13 +import java.net.URISyntaxException;
    3.14  import java.nio.charset.Charset;
    3.15  import java.util.ArrayList;
    3.16 +import java.util.Arrays;
    3.17  import java.util.Collections;
    3.18 +import java.util.HashMap;
    3.19  import java.util.Iterator;
    3.20  import java.util.List;
    3.21  import java.util.Locale;
    3.22  import java.util.Map;
    3.23  import java.util.Set;
    3.24 +import java.util.logging.Level;
    3.25 +import java.util.logging.Logger;
    3.26  import java.util.regex.Matcher;
    3.27  import java.util.regex.Pattern;
    3.28  import javax.tools.Diagnostic;
    3.29  import javax.tools.DiagnosticListener;
    3.30  import javax.tools.FileObject;
    3.31 +import javax.tools.ForwardingJavaFileManager;
    3.32  import javax.tools.JavaCompiler;
    3.33  import javax.tools.JavaCompiler.CompilationTask;
    3.34  import javax.tools.JavaFileManager;
    3.35  import javax.tools.JavaFileObject;
    3.36  import javax.tools.JavaFileObject.Kind;
    3.37 +import javax.tools.SimpleJavaFileObject;
    3.38  import javax.tools.StandardJavaFileManager;
    3.39  import javax.tools.StandardLocation;
    3.40 +import javax.tools.ToolProvider;
    3.41  
    3.42  /**
    3.43   *
    3.44 @@ -41,6 +54,7 @@
    3.45          this.jfo = jfo;
    3.46      }
    3.47      
    3.48 +    /*
    3.49      public static Map<String,byte[]> compile(String html, String java) throws IOException {
    3.50          JavaCompiler jc = javax.tools.ToolProvider.getSystemJavaCompiler();
    3.51          String pkg = findPkg(java);
    3.52 @@ -58,6 +72,123 @@
    3.53          }
    3.54          return Collections.emptyMap();
    3.55      }
    3.56 +    */
    3.57 +    public static Map<String, byte[]> compile(final String html, final String code) throws IOException {
    3.58 +        final String pkg = findPkg(code);
    3.59 +//        String cls = findCls(code);
    3.60 +        
    3.61 +        DiagnosticListener<JavaFileObject> devNull = new DiagnosticListener<JavaFileObject>() {
    3.62 +            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    3.63 +                System.err.println("diagnostic=" + diagnostic);
    3.64 +            }
    3.65 +        };
    3.66 +        StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(devNull, null, null);
    3.67 +
    3.68 +//        sjfm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, toFiles(boot));
    3.69 +//        sjfm.setLocation(StandardLocation.CLASS_PATH, toFiles(compile));
    3.70 +
    3.71 +        final Map<String, ByteArrayOutputStream> class2BAOS = new HashMap<String, ByteArrayOutputStream>();
    3.72 +
    3.73 +        JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) {
    3.74 +            @Override
    3.75 +            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    3.76 +                return code;
    3.77 +            }
    3.78 +        };
    3.79 +        final JavaFileObject htmlFile = new SimpleJavaFileObject(URI.create("mem://mem2"), Kind.OTHER) {
    3.80 +            @Override
    3.81 +            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    3.82 +                return html;
    3.83 +            }
    3.84 +
    3.85 +            @Override
    3.86 +            public InputStream openInputStream() throws IOException {
    3.87 +                return new ByteArrayInputStream(html.getBytes());
    3.88 +            }
    3.89 +        };
    3.90 +        
    3.91 +        final URI scratch;
    3.92 +        try {
    3.93 +            scratch = new URI("mem://mem3");
    3.94 +        } catch (URISyntaxException ex) {
    3.95 +            throw new IOException(ex);
    3.96 +        }
    3.97 +        
    3.98 +        JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(sjfm) {
    3.99 +            @Override
   3.100 +            public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
   3.101 +                if (kind  == Kind.CLASS) {
   3.102 +                    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
   3.103 +
   3.104 +                    class2BAOS.put(className.replace('.', '/') + ".class", buffer);
   3.105 +                    return new SimpleJavaFileObject(sibling.toUri(), kind) {
   3.106 +                        @Override
   3.107 +                        public OutputStream openOutputStream() throws IOException {
   3.108 +                            return buffer;
   3.109 +                        }
   3.110 +                    };
   3.111 +                }
   3.112 +                
   3.113 +                if (kind == Kind.SOURCE) {
   3.114 +                    return new SimpleJavaFileObject(scratch/*sibling.toUri()*/, kind) {
   3.115 +                        private final ByteArrayOutputStream data = new ByteArrayOutputStream();
   3.116 +                        @Override
   3.117 +                        public OutputStream openOutputStream() throws IOException {
   3.118 +                            return data;
   3.119 +                        }
   3.120 +
   3.121 +                        @Override
   3.122 +                        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   3.123 +                            data.close();
   3.124 +                            return new String(data.toByteArray());
   3.125 +                        }
   3.126 +                    };
   3.127 +                }
   3.128 +                
   3.129 +                throw new IllegalStateException();
   3.130 +            }
   3.131 +//            @Override
   3.132 +//            public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException {
   3.133 +//                if (location == StandardLocation.PLATFORM_CLASS_PATH) {
   3.134 +//                    return super.list(location, packageName, kinds, recurse);
   3.135 +//                }
   3.136 +//                if (location == StandardLocation.CLASS_PATH) {
   3.137 +//                    return super.list(location, packageName, kinds, recurse);
   3.138 +//                }
   3.139 +//                if (location == StandardLocation.SOURCE_PATH) {
   3.140 +//                    System.out.println("src path for " + packageName + " kinds: " + kinds);
   3.141 +//                    if (packageName.equals(pkg) && kinds.contains(Kind.OTHER)) {
   3.142 +//                        return Collections.<JavaFileObject>singleton(htmlFile);
   3.143 +//                    }
   3.144 +//                    return Collections.emptyList();
   3.145 +//                }
   3.146 +//                throw new UnsupportedOperationException("Loc: " + location + " pkg: " + packageName + " kinds: " + kinds + " rec: " + recurse);
   3.147 +//            }
   3.148 +
   3.149 +            @Override
   3.150 +            public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
   3.151 +                if (location == StandardLocation.SOURCE_PATH) {
   3.152 +//                    System.out.println("src path for " + packageName + " kinds: " + kinds);
   3.153 +                    if (packageName.equals(pkg)) {
   3.154 +                        return htmlFile;
   3.155 +                    }
   3.156 +                }
   3.157 +                
   3.158 +                return null;
   3.159 +            }
   3.160 +            
   3.161 +        };
   3.162 +
   3.163 +        ToolProvider.getSystemJavaCompiler().getTask(null, jfm, devNull, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call();
   3.164 +
   3.165 +        Map<String, byte[]> result = new HashMap<String, byte[]>();
   3.166 +
   3.167 +        for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) {
   3.168 +            result.put(e.getKey(), e.getValue().toByteArray());
   3.169 +        }
   3.170 +
   3.171 +        return result;
   3.172 +    }
   3.173  
   3.174      @Override
   3.175      public ClassLoader getClassLoader(Location location) {
     4.1 --- a/launcher/src/test/java/org/apidesign/bck2brwsr/dew/CompileTest.java	Tue Jan 15 22:48:17 2013 +0100
     4.2 +++ b/launcher/src/test/java/org/apidesign/bck2brwsr/dew/CompileTest.java	Wed Jan 16 10:52:06 2013 +0100
     4.3 @@ -19,10 +19,14 @@
     4.4                  + " <button id='btn'>Hello!</button>"
     4.5                  + "</body></html>";
     4.6          String java = "package x.y.z;"
     4.7 -                + "class X {"
     4.8 -                + "}";
     4.9 +                + "import org.apidesign.bck2brwsr.htmlpage.api.*;"
    4.10 +            + "@Page(xhtml=\"index.html\", className=\"Index\")"
    4.11 +            + "class X { "
    4.12 +            + "   @OnClick(id=\"btn\") static void clcs() {}"
    4.13 +            + "}";
    4.14          Map<String,byte[]> result = Compile.compile(html, java);
    4.15  
    4.16          assertNotNull(result.get("x/y/z/X.class"), "Class X is compiled: " + result);
    4.17 +        assertNotNull(result.get("x/y/z/Index.class"), "Class Index is compiled: " + result);
    4.18      }
    4.19  }