Using META-INF/net.java.html.js.classes to find out whether runtime patching is needed or not. The annotation processor for @JavaScriptBody generate the file, the maven class post processor empties it. If such file is around, the bootstrap creates own file, otherwise it assumes patching has been done and is not needed in runtime. preprocess
authorJaroslav Tulach <jaroslav.tulach@netbeans.org>
Thu, 30 Jan 2014 14:59:33 +0100
branchpreprocess
changeset 509186de7f12a7c
parent 508 40282fb8fabd
child 510 1db285d9d38b
Using META-INF/net.java.html.js.classes to find out whether runtime patching is needed or not. The annotation processor for @JavaScriptBody generate the file, the maven class post processor empties it. If such file is around, the bootstrap creates own file, otherwise it assumes patching has been done and is not needed in runtime.
boot/src/main/java/net/java/html/boot/BrowserBuilder.java
boot/src/main/java/org/netbeans/html/boot/impl/FnUtils.java
boot/src/main/java/org/netbeans/html/boot/impl/JavaScriptProcesor.java
html4j-maven-plugin/src/main/java/org/apidesign/html/mojo/ProcessJsAnnotationsMojo.java
     1.1 --- a/boot/src/main/java/net/java/html/boot/BrowserBuilder.java	Thu Jan 30 12:11:06 2014 +0100
     1.2 +++ b/boot/src/main/java/net/java/html/boot/BrowserBuilder.java	Thu Jan 30 14:59:33 2014 +0100
     1.3 @@ -54,10 +54,10 @@
     1.4  import java.util.logging.Level;
     1.5  import java.util.logging.Logger;
     1.6  import net.java.html.js.JavaScriptBody;
     1.7 -import org.netbeans.html.boot.impl.FnUtils;
     1.8  import org.apidesign.html.boot.spi.Fn;
     1.9  import org.netbeans.html.boot.impl.FindResources;
    1.10  import org.netbeans.html.boot.impl.FnContext;
    1.11 +import org.netbeans.html.boot.impl.FnUtils;
    1.12  
    1.13  /** Use this builder to launch your Java/HTML based application. Typical
    1.14   * usage in a main method of your application looks like this: 
    1.15 @@ -226,12 +226,12 @@
    1.16          }
    1.17          
    1.18          final ClassLoader loader;
    1.19 -//        if (FnUtils.isJavaScriptCapable(clazz.getClassLoader())) {
    1.20 +        if (FnUtils.isJavaScriptCapable(clazz.getClassLoader())) {
    1.21              loader = clazz.getClassLoader();
    1.22 -//        } else {
    1.23 -//            FImpl impl = new FImpl(clazz.getClassLoader());
    1.24 -//            loader = FnUtils.newLoader(impl, dfnr, clazz.getClassLoader().getParent());
    1.25 -//        }
    1.26 +        } else {
    1.27 +            FImpl impl = new FImpl(clazz.getClassLoader());
    1.28 +            loader = FnUtils.newLoader(impl, dfnr, clazz.getClassLoader().getParent());
    1.29 +        }
    1.30  
    1.31          final Fn.Presenter currentP = dfnr;
    1.32          class OnPageLoad implements Runnable {
     2.1 --- a/boot/src/main/java/org/netbeans/html/boot/impl/FnUtils.java	Thu Jan 30 12:11:06 2014 +0100
     2.2 +++ b/boot/src/main/java/org/netbeans/html/boot/impl/FnUtils.java	Thu Jan 30 14:59:33 2014 +0100
     2.3 @@ -113,6 +113,9 @@
     2.4          if (l instanceof JsClassLoader) {
     2.5              return true;
     2.6          }
     2.7 +        if (l.getResource("META-INF/net.java.html.js.classes") != null) {
     2.8 +            return false;
     2.9 +        }
    2.10          Class<?> clazz;
    2.11          Closeable c = Fn.activate(new TrueFn());
    2.12          try {
     3.1 --- a/boot/src/main/java/org/netbeans/html/boot/impl/JavaScriptProcesor.java	Thu Jan 30 12:11:06 2014 +0100
     3.2 +++ b/boot/src/main/java/org/netbeans/html/boot/impl/JavaScriptProcesor.java	Thu Jan 30 14:59:33 2014 +0100
     3.3 @@ -66,6 +66,7 @@
     3.4  import javax.lang.model.element.ElementKind;
     3.5  import javax.lang.model.element.ExecutableElement;
     3.6  import javax.lang.model.element.Modifier;
     3.7 +import javax.lang.model.element.Name;
     3.8  import javax.lang.model.element.PackageElement;
     3.9  import javax.lang.model.element.TypeElement;
    3.10  import javax.lang.model.element.VariableElement;
    3.11 @@ -280,11 +281,20 @@
    3.12      }
    3.13      
    3.14      private void generateJavaScriptBodyList(Map<String,Set<TypeElement>> bodies) {
    3.15 -        for (Map.Entry<String, Set<TypeElement>> entry : bodies.entrySet()) {
    3.16 -            String pkg = entry.getKey();
    3.17 -            Set<TypeElement> classes = entry.getValue();
    3.18 -            
    3.19 -            try {
    3.20 +        if (bodies.isEmpty()) {
    3.21 +            return;
    3.22 +        }
    3.23 +        try {
    3.24 +            FileObject all = processingEnv.getFiler().createResource(
    3.25 +                StandardLocation.CLASS_OUTPUT, "", "META-INF/net.java.html.js.classes"                
    3.26 +            );
    3.27 +            PrintWriter wAll = new PrintWriter(new OutputStreamWriter(
    3.28 +                all.openOutputStream(), "UTF-8"
    3.29 +            ));
    3.30 +            for (Map.Entry<String, Set<TypeElement>> entry : bodies.entrySet()) {
    3.31 +                String pkg = entry.getKey();
    3.32 +                Set<TypeElement> classes = entry.getValue();
    3.33 +
    3.34                  FileObject out = processingEnv.getFiler().createResource(
    3.35                      StandardLocation.CLASS_OUTPUT, pkg, "net.java.html.js.classes",
    3.36                      classes.iterator().next()
    3.37 @@ -293,16 +303,21 @@
    3.38                  try {
    3.39                      PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
    3.40                      for (TypeElement type : classes) {
    3.41 -                        w.println(processingEnv.getElementUtils().getBinaryName(type));
    3.42 +                        final Name bn = processingEnv.getElementUtils().getBinaryName(type);
    3.43 +                        w.println(bn);
    3.44 +                        wAll.println(bn);
    3.45                      }
    3.46                      w.flush();
    3.47                      w.close();
    3.48 +                } catch (IOException x) {
    3.49 +                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to write to " + entry.getKey() + ": " + x.toString());
    3.50                  } finally {
    3.51                      os.close();
    3.52                  }
    3.53 -            } catch (IOException x) {
    3.54 -                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to write to " + entry.getKey() + ": " + x.toString());
    3.55              }
    3.56 +            wAll.close();
    3.57 +        } catch (IOException x) {
    3.58 +            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to write to " + "META-INF/net.java.html.js.classes: " + x.toString());
    3.59          }
    3.60      }
    3.61      
     4.1 --- a/html4j-maven-plugin/src/main/java/org/apidesign/html/mojo/ProcessJsAnnotationsMojo.java	Thu Jan 30 12:11:06 2014 +0100
     4.2 +++ b/html4j-maven-plugin/src/main/java/org/apidesign/html/mojo/ProcessJsAnnotationsMojo.java	Thu Jan 30 14:59:33 2014 +0100
     4.3 @@ -107,13 +107,14 @@
     4.4          }
     4.5          URLClassLoader l = new URLClassLoader(arr.toArray(new URL[arr.size()]));
     4.6          try {
     4.7 -            processClasses(l, classes);
     4.8 +            File master = new File(new File(classes, "META-INF"), "net.java.html.js.classes");
     4.9 +            processClasses(l, master, classes);
    4.10          } catch (IOException ex) {
    4.11              throw new MojoExecutionException("Problem converting JavaScriptXXX annotations", ex);
    4.12          }
    4.13      }
    4.14      
    4.15 -    private void processClasses(ClassLoader l, File f) throws IOException, MojoExecutionException {
    4.16 +    private void processClasses(ClassLoader l, File master, File f) throws IOException, MojoExecutionException {
    4.17          if (!f.exists()) {
    4.18              return;
    4.19          }
    4.20 @@ -123,7 +124,7 @@
    4.21              if (arr != null) {
    4.22                  for (File file : arr) {
    4.23                      if (classes || file.isDirectory()) {
    4.24 -                        processClasses(l, file);
    4.25 +                        processClasses(l, master, file);
    4.26                      }
    4.27                  }
    4.28              }
    4.29 @@ -152,6 +153,7 @@
    4.30                  return;
    4.31              }
    4.32              filterClass(new File(f.getParentFile(), "net.java.html.js.classes"), f.getName());
    4.33 +            filterClass(master, f.getName());
    4.34          } catch (Exception ex) {
    4.35              throw new MojoExecutionException("Can't process " + f, ex);
    4.36          }