Support for exporting whole packages. addRootClasses defined in terms of addClasses and addExported. closure
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 21 May 2014 23:42:24 +0200
branchclosure
changeset 158389b6b369c13d
parent 1582 32499c3c9cfa
child 1584 7b6295731c30
Support for exporting whole packages. addRootClasses defined in terms of addClasses and addExported.
launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/ExportedSymbols.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java
     1.1 --- a/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java	Wed May 21 21:55:17 2014 +0200
     1.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java	Wed May 21 23:42:24 2014 +0200
     1.3 @@ -49,21 +49,8 @@
     1.4      throws IOException {
     1.5          List<String> arr = new ArrayList<>();
     1.6          List<String> classes = new ArrayList<>();
     1.7 -        Set<String> exported = new HashSet<String>();
     1.8          Set<String> keep = new HashSet<String>(testClasses);
     1.9 -        listJAR(jar, classes, arr, exported, keep);
    1.10 -        List<String> root = new ArrayList<>();
    1.11 -        for (String c : classes) {
    1.12 -            if (keep.contains(c)) {
    1.13 -                root.add(c);
    1.14 -                continue;
    1.15 -            }
    1.16 -            int slash = c.lastIndexOf('/');
    1.17 -            String pkg = c.substring(0, slash + 1);
    1.18 -            if (exported.contains(pkg)) {
    1.19 -                root.add(c);
    1.20 -            }
    1.21 -        }
    1.22 +        listJAR(jar, classes, arr, keep);
    1.23          
    1.24          StringWriter w = new StringWriter();
    1.25          try {
    1.26 @@ -77,7 +64,7 @@
    1.27              
    1.28              Bck2Brwsr.newCompiler()
    1.29                  .addClasses(classes.toArray(new String[0]))
    1.30 -                .addRootClasses(root.toArray(new String[0]))
    1.31 +                .addExported(keep.toArray(new String[0]))
    1.32                  .addResources(arr.toArray(new String[0]))
    1.33                  .library(true)
    1.34                  .resources(new JarRes())
    1.35 @@ -138,7 +125,7 @@
    1.36      
    1.37      private static void listJAR(
    1.38          JarFile j, List<String> classes,
    1.39 -        List<String> resources, Set<String> exported, Set<String> keep
    1.40 +        List<String> resources, Set<String> keep
    1.41      ) throws IOException {
    1.42          Enumeration<JarEntry> en = j.entries();
    1.43          while (en.hasMoreElements()) {
    1.44 @@ -172,10 +159,10 @@
    1.45              }
    1.46          }
    1.47          String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
    1.48 -        if (exp != null && exported != null) {
    1.49 +        if (exp != null && keep != null) {
    1.50              for (String def : exp.split(",")) {
    1.51                  for (String sep : def.split(";")) {
    1.52 -                    exported.add(sep.replace('.', '/') + "/");
    1.53 +                    keep.add(sep.replace('.', '/') + "/");
    1.54                      break;
    1.55                  }
    1.56              }
    1.57 @@ -209,12 +196,12 @@
    1.58          {
    1.59              URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
    1.60              JarURLConnection juc = (JarURLConnection)u.openConnection();
    1.61 -            listJAR(juc.getJarFile(), classes, arr, null, null);
    1.62 +            listJAR(juc.getJarFile(), classes, arr, null);
    1.63          }
    1.64          {
    1.65              URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
    1.66              JarURLConnection juc = (JarURLConnection)u.openConnection();
    1.67 -            listJAR(juc.getJarFile(), classes, arr, null, null);
    1.68 +            listJAR(juc.getJarFile(), classes, arr, null);
    1.69          }
    1.70  
    1.71          Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
     2.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java	Wed May 21 21:55:17 2014 +0200
     2.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java	Wed May 21 23:42:24 2014 +0200
     2.3 @@ -19,7 +19,6 @@
     2.4  
     2.5  import java.io.IOException;
     2.6  import java.io.InputStream;
     2.7 -import org.apidesign.bck2brwsr.core.Exported;
     2.8  
     2.9  /** Build your own virtual machine! Use methods in this class to generate
    2.10   * a skeleton JVM in JavaScript that contains pre-compiled classes of your
    2.11 @@ -54,15 +53,15 @@
    2.12   */
    2.13  public final class Bck2Brwsr {
    2.14      private final ObfuscationLevel level;
    2.15 -    private final StringArray rootcls;
    2.16 +    private final StringArray exported;
    2.17      private final StringArray classes;
    2.18      private final StringArray resources;
    2.19      private final Resources res;
    2.20      private final boolean extension;
    2.21  
    2.22 -    private Bck2Brwsr(ObfuscationLevel level, StringArray rootcls, StringArray classes, StringArray resources, Resources res, boolean extension) {
    2.23 +    private Bck2Brwsr(ObfuscationLevel level, StringArray exported, StringArray classes, StringArray resources, Resources res, boolean extension) {
    2.24          this.level = level;
    2.25 -        this.rootcls = rootcls;
    2.26 +        this.exported = exported;
    2.27          this.classes = classes;
    2.28          this.resources = resources;
    2.29          this.res = res;
    2.30 @@ -105,6 +104,27 @@
    2.31      public static Bck2Brwsr newCompiler() {
    2.32          return new Bck2Brwsr(ObfuscationLevel.NONE, new StringArray(), new StringArray(), new StringArray(), null, false);
    2.33      }
    2.34 +    
    2.35 +    /** Adds exported classes or packages. If the string ends 
    2.36 +     * with slash, it is considered a name of package. If it does not,
    2.37 +     * it is a name of a class (without <code>.class</code> suffix).
    2.38 +     * The exported classes are prevented from being obfuscated. 
    2.39 +     * All public classes in exported packages are prevented from
    2.40 +     * being obfuscated. By listing the packages or classes in this 
    2.41 +     * method, these classes are not guaranteed to be included in
    2.42 +     * the generated script. Use {@link #addClasses} to include
    2.43 +     * the classes.
    2.44 +     * 
    2.45 +     * @param exported names of classes and packages to treat as exported
    2.46 +     * @return new instances of the Bck2Brwsr compiler which inherits
    2.47 +     *   all values from <code>this</code> except list of exported classes
    2.48 +     */
    2.49 +    public Bck2Brwsr addExported(String... exported) {
    2.50 +        return new Bck2Brwsr(
    2.51 +            level, this.exported.addAndNew(exported), 
    2.52 +            classes, resources, res, extension
    2.53 +        );
    2.54 +    }
    2.55  
    2.56      /** Adds additional classes 
    2.57       * to the list of those that should be included in the generated
    2.58 @@ -113,7 +133,7 @@
    2.59       * generated virtual machine code accessible using their fully 
    2.60       * qualified name. This brings the same behavior as if the
    2.61       * classes were added by {@link #addClasses(java.lang.String...) } and
    2.62 -     * were annotated with {@link Exported} annotation.
    2.63 +     * exported via {@link #addExported(java.lang.String...)}.
    2.64       * 
    2.65       * @param classes the classes to add to the compilation
    2.66       * @return new instance of the Bck2Brwsr compiler which inherits
    2.67 @@ -122,10 +142,8 @@
    2.68      public Bck2Brwsr addRootClasses(String... classes) {
    2.69          if (classes.length == 0) {
    2.70              return this;
    2.71 -        } else {
    2.72 -            return new Bck2Brwsr(level, rootcls.addAndNew(classes), this.classes, resources, res,
    2.73 -                                 extension);
    2.74 -        }
    2.75 +        } 
    2.76 +        return addExported(classes).addClasses(classes);
    2.77      }
    2.78      
    2.79      /** Adds additional classes 
    2.80 @@ -143,7 +161,8 @@
    2.81          if (classes.length == 0) {
    2.82              return this;
    2.83          } else {
    2.84 -            return new Bck2Brwsr(level, rootcls, this.classes.addAndNew(classes), resources, res,
    2.85 +            return new Bck2Brwsr(level, exported, 
    2.86 +                this.classes.addAndNew(classes), resources, res,
    2.87                  extension);
    2.88          }
    2.89      }
    2.90 @@ -163,7 +182,7 @@
    2.91          if (resources.length == 0) {
    2.92              return this;
    2.93          } else {
    2.94 -            return new Bck2Brwsr(level, rootcls, this.classes, 
    2.95 +            return new Bck2Brwsr(level, exported, this.classes, 
    2.96                  this.resources.addAndNew(resources), res, extension
    2.97              );
    2.98          }
    2.99 @@ -178,7 +197,7 @@
   2.100       * @since 0.5
   2.101       */
   2.102      public Bck2Brwsr obfuscation(ObfuscationLevel level) {
   2.103 -        return new Bck2Brwsr(level, rootcls, classes, resources, res, extension);
   2.104 +        return new Bck2Brwsr(level, exported, classes, resources, res, extension);
   2.105      }
   2.106      
   2.107      /** A way to change the provider of additional resources (classes) for the 
   2.108 @@ -190,7 +209,7 @@
   2.109       * @since 0.5
   2.110       */
   2.111      public Bck2Brwsr resources(Resources res) {
   2.112 -        return new Bck2Brwsr(level, rootcls, classes, resources, res, extension);
   2.113 +        return new Bck2Brwsr(level, exported, classes, resources, res, extension);
   2.114      }
   2.115  
   2.116      /** Should one generate a library? By default the system generates
   2.117 @@ -205,7 +224,7 @@
   2.118       * @since 0.9
   2.119       */
   2.120      public Bck2Brwsr library(boolean library) {
   2.121 -        return new Bck2Brwsr(level, rootcls, classes, resources, res, library);
   2.122 +        return new Bck2Brwsr(level, exported, classes, resources, res, library);
   2.123      }
   2.124  
   2.125      /** A way to change the provider of additional resources (classes) for the 
   2.126 @@ -264,16 +283,16 @@
   2.127          return res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader(), false);
   2.128      }
   2.129      
   2.130 -    String[] allClasses() {
   2.131 -        return classes.addAndNew(rootcls.toArray()).toArray();
   2.132 -    }
   2.133      StringArray allResources() {
   2.134          return resources;
   2.135      }
   2.136  
   2.137 -    
   2.138 -    StringArray rootClasses() {
   2.139 -        return rootcls;
   2.140 +    StringArray classes() {
   2.141 +        return classes;
   2.142 +    }
   2.143 +
   2.144 +    StringArray exported() {
   2.145 +        return exported;
   2.146      }
   2.147      
   2.148      boolean isExtension() {
     3.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ExportedSymbols.java	Wed May 21 21:55:17 2014 +0200
     3.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ExportedSymbols.java	Wed May 21 23:42:24 2014 +0200
     3.3 @@ -115,6 +115,9 @@
     3.4      }
     3.5  
     3.6      private boolean resolveIsMarkedAsExportedPackage(String pkgName) {
     3.7 +        if (exported.contains(pkgName + '/')) {
     3.8 +            return true;
     3.9 +        }
    3.10          try {
    3.11              final InputStream is =
    3.12                      resources.get(pkgName + "/package-info.class");
     4.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Wed May 21 21:55:17 2014 +0200
     4.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Wed May 21 23:42:24 2014 +0200
     4.3 @@ -67,12 +67,12 @@
     4.4      static void compile(Appendable out, 
     4.5          Bck2Brwsr config
     4.6      ) throws IOException {
     4.7 -        String[] both = config.allClasses();
     4.8 +        String[] both = config.classes().toArray();
     4.9          
    4.10          VM vm = config.isExtension() ? 
    4.11 -            new Extension(out, config.getResources(), both, config.rootClasses())
    4.12 +            new Extension(out, config.getResources(), both, config.exported())
    4.13              : 
    4.14 -            new Standalone(out, config.getResources(), config.rootClasses());
    4.15 +            new Standalone(out, config.getResources(), config.exported());
    4.16  
    4.17          final StringArray fixedNames = new StringArray();
    4.18