# HG changeset patch # User Jaroslav Tulach # Date 1400708544 -7200 # Node ID 89b6b369c13dc74161869f21a6568ec1c43e9dcf # Parent 32499c3c9cfa637087fef8a5b43a75b499b9aa9a Support for exporting whole packages. addRootClasses defined in terms of addClasses and addExported. diff -r 32499c3c9cfa -r 89b6b369c13d launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java --- a/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java Wed May 21 21:55:17 2014 +0200 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java Wed May 21 23:42:24 2014 +0200 @@ -49,21 +49,8 @@ throws IOException { List arr = new ArrayList<>(); List classes = new ArrayList<>(); - Set exported = new HashSet(); Set keep = new HashSet(testClasses); - listJAR(jar, classes, arr, exported, keep); - List root = new ArrayList<>(); - for (String c : classes) { - if (keep.contains(c)) { - root.add(c); - continue; - } - int slash = c.lastIndexOf('/'); - String pkg = c.substring(0, slash + 1); - if (exported.contains(pkg)) { - root.add(c); - } - } + listJAR(jar, classes, arr, keep); StringWriter w = new StringWriter(); try { @@ -77,7 +64,7 @@ Bck2Brwsr.newCompiler() .addClasses(classes.toArray(new String[0])) - .addRootClasses(root.toArray(new String[0])) + .addExported(keep.toArray(new String[0])) .addResources(arr.toArray(new String[0])) .library(true) .resources(new JarRes()) @@ -138,7 +125,7 @@ private static void listJAR( JarFile j, List classes, - List resources, Set exported, Set keep + List resources, Set keep ) throws IOException { Enumeration en = j.entries(); while (en.hasMoreElements()) { @@ -172,10 +159,10 @@ } } String exp = j.getManifest().getMainAttributes().getValue("Export-Package"); - if (exp != null && exported != null) { + if (exp != null && keep != null) { for (String def : exp.split(",")) { for (String sep : def.split(";")) { - exported.add(sep.replace('.', '/') + "/"); + keep.add(sep.replace('.', '/') + "/"); break; } } @@ -209,12 +196,12 @@ { URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0); JarURLConnection juc = (JarURLConnection)u.openConnection(); - listJAR(juc.getJarFile(), classes, arr, null, null); + listJAR(juc.getJarFile(), classes, arr, null); } { URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0); JarURLConnection juc = (JarURLConnection)u.openConnection(); - listJAR(juc.getJarFile(), classes, arr, null, null); + listJAR(juc.getJarFile(), classes, arr, null); } Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0])) diff -r 32499c3c9cfa -r 89b6b369c13d rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Wed May 21 21:55:17 2014 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Wed May 21 23:42:24 2014 +0200 @@ -19,7 +19,6 @@ import java.io.IOException; import java.io.InputStream; -import org.apidesign.bck2brwsr.core.Exported; /** Build your own virtual machine! Use methods in this class to generate * a skeleton JVM in JavaScript that contains pre-compiled classes of your @@ -54,15 +53,15 @@ */ public final class Bck2Brwsr { private final ObfuscationLevel level; - private final StringArray rootcls; + private final StringArray exported; private final StringArray classes; private final StringArray resources; private final Resources res; private final boolean extension; - private Bck2Brwsr(ObfuscationLevel level, StringArray rootcls, StringArray classes, StringArray resources, Resources res, boolean extension) { + private Bck2Brwsr(ObfuscationLevel level, StringArray exported, StringArray classes, StringArray resources, Resources res, boolean extension) { this.level = level; - this.rootcls = rootcls; + this.exported = exported; this.classes = classes; this.resources = resources; this.res = res; @@ -105,6 +104,27 @@ public static Bck2Brwsr newCompiler() { return new Bck2Brwsr(ObfuscationLevel.NONE, new StringArray(), new StringArray(), new StringArray(), null, false); } + + /** Adds exported classes or packages. If the string ends + * with slash, it is considered a name of package. If it does not, + * it is a name of a class (without .class suffix). + * The exported classes are prevented from being obfuscated. + * All public classes in exported packages are prevented from + * being obfuscated. By listing the packages or classes in this + * method, these classes are not guaranteed to be included in + * the generated script. Use {@link #addClasses} to include + * the classes. + * + * @param exported names of classes and packages to treat as exported + * @return new instances of the Bck2Brwsr compiler which inherits + * all values from this except list of exported classes + */ + public Bck2Brwsr addExported(String... exported) { + return new Bck2Brwsr( + level, this.exported.addAndNew(exported), + classes, resources, res, extension + ); + } /** Adds additional classes * to the list of those that should be included in the generated @@ -113,7 +133,7 @@ * generated virtual machine code accessible using their fully * qualified name. This brings the same behavior as if the * classes were added by {@link #addClasses(java.lang.String...) } and - * were annotated with {@link Exported} annotation. + * exported via {@link #addExported(java.lang.String...)}. * * @param classes the classes to add to the compilation * @return new instance of the Bck2Brwsr compiler which inherits @@ -122,10 +142,8 @@ public Bck2Brwsr addRootClasses(String... classes) { if (classes.length == 0) { return this; - } else { - return new Bck2Brwsr(level, rootcls.addAndNew(classes), this.classes, resources, res, - extension); - } + } + return addExported(classes).addClasses(classes); } /** Adds additional classes @@ -143,7 +161,8 @@ if (classes.length == 0) { return this; } else { - return new Bck2Brwsr(level, rootcls, this.classes.addAndNew(classes), resources, res, + return new Bck2Brwsr(level, exported, + this.classes.addAndNew(classes), resources, res, extension); } } @@ -163,7 +182,7 @@ if (resources.length == 0) { return this; } else { - return new Bck2Brwsr(level, rootcls, this.classes, + return new Bck2Brwsr(level, exported, this.classes, this.resources.addAndNew(resources), res, extension ); } @@ -178,7 +197,7 @@ * @since 0.5 */ public Bck2Brwsr obfuscation(ObfuscationLevel level) { - return new Bck2Brwsr(level, rootcls, classes, resources, res, extension); + return new Bck2Brwsr(level, exported, classes, resources, res, extension); } /** A way to change the provider of additional resources (classes) for the @@ -190,7 +209,7 @@ * @since 0.5 */ public Bck2Brwsr resources(Resources res) { - return new Bck2Brwsr(level, rootcls, classes, resources, res, extension); + return new Bck2Brwsr(level, exported, classes, resources, res, extension); } /** Should one generate a library? By default the system generates @@ -205,7 +224,7 @@ * @since 0.9 */ public Bck2Brwsr library(boolean library) { - return new Bck2Brwsr(level, rootcls, classes, resources, res, library); + return new Bck2Brwsr(level, exported, classes, resources, res, library); } /** A way to change the provider of additional resources (classes) for the @@ -264,16 +283,16 @@ return res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader(), false); } - String[] allClasses() { - return classes.addAndNew(rootcls.toArray()).toArray(); - } StringArray allResources() { return resources; } - - StringArray rootClasses() { - return rootcls; + StringArray classes() { + return classes; + } + + StringArray exported() { + return exported; } boolean isExtension() { diff -r 32499c3c9cfa -r 89b6b369c13d rt/vm/src/main/java/org/apidesign/vm4brwsr/ExportedSymbols.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ExportedSymbols.java Wed May 21 21:55:17 2014 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ExportedSymbols.java Wed May 21 23:42:24 2014 +0200 @@ -115,6 +115,9 @@ } private boolean resolveIsMarkedAsExportedPackage(String pkgName) { + if (exported.contains(pkgName + '/')) { + return true; + } try { final InputStream is = resources.get(pkgName + "/package-info.class"); diff -r 32499c3c9cfa -r 89b6b369c13d rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Wed May 21 21:55:17 2014 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Wed May 21 23:42:24 2014 +0200 @@ -67,12 +67,12 @@ static void compile(Appendable out, Bck2Brwsr config ) throws IOException { - String[] both = config.allClasses(); + String[] both = config.classes().toArray(); VM vm = config.isExtension() ? - new Extension(out, config.getResources(), both, config.rootClasses()) + new Extension(out, config.getResources(), both, config.exported()) : - new Standalone(out, config.getResources(), config.rootClasses()); + new Standalone(out, config.getResources(), config.exported()); final StringArray fixedNames = new StringArray();