# HG changeset patch # User Jaroslav Tulach # Date 1363959970 -3600 # Node ID 2bcbe348dbec84e6255b477aec12130a3296c592 # Parent 95c6ffa9d8e7e03f7968ea5901b1268345fa0fa6 Using cummulative factory with the hope to prevent enormous increase in overloaded 'generate' methods diff -r 95c6ffa9d8e7 -r 2bcbe348dbec rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java --- a/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java Fri Mar 22 10:52:51 2013 +0100 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java Fri Mar 22 14:46:10 2013 +0100 @@ -88,7 +88,11 @@ try { URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts()); FileWriter w = new FileWriter(javascript); - Bck2Brwsr.generate(w, obfuscation, url, arr.toArray(new String[0])); + Bck2Brwsr.newCompiler(). + obfuscation(obfuscation). + resources(url). + addRootClasses(arr.toArray(new String[0])). + generate(w); w.close(); } catch (IOException ex) { throw new MojoExecutionException("Can't compile", ex); diff -r 95c6ffa9d8e7 -r 2bcbe348dbec rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Fri Mar 22 10:52:51 2013 +0100 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Fri Mar 22 14:46:10 2013 +0100 @@ -54,10 +54,17 @@ * @author Jaroslav Tulach */ public final class Bck2Brwsr { - private Bck2Brwsr() { + private final ObfuscationLevel level; + private final StringArray classes; + private final Resources res; + + private Bck2Brwsr(ObfuscationLevel level, StringArray classes, Resources resources) { + this.level = level; + this.classes = classes; + this.res = resources; } - - /** Generates virtual machine from bytes served by a resources + + /** Helper method to generate virtual machine from bytes served by a resources * provider. * * @param out the output to write the generated JavaScript to @@ -66,10 +73,10 @@ * @throws IOException I/O exception can be thrown when something goes wrong */ public static void generate(Appendable out, Resources resources, String... classes) throws IOException { - generate(out, ObfuscationLevel.NONE, resources, classes); + newCompiler().resources(resources).addRootClasses(classes).generate(out); } - /** Generates virtual machine from bytes served by a class loader. + /** Helper method to generate virtual machine from bytes served by a class loader. * * @param out the output to write the generated JavaScript to * @param loader class loader to load needed classes from @@ -77,27 +84,87 @@ * @throws IOException I/O exception can be thrown when something goes wrong */ public static void generate(Appendable out, ClassLoader loader, String... classes) throws IOException { - generate(out, ObfuscationLevel.NONE, loader, classes); + newCompiler().resources(loader).addRootClasses(classes).generate(out); + } + + /** Creates new instance of Bck2Brwsr compiler which is ready to generate + * empty Bck2Brwsr virtual machine. The instance can be further + * configured by calling chain of methods. For example: + *
+     * {@link #createCompiler()}.{@link #resources(org.apidesign.vm4brwsr.Bck2Brwsr.Resources) resources(loader)}.{@link #addRootClasses(java.lang.String[]) addRootClasses("your/Clazz")}.{@link #generate(java.lang.Appendable) generate(out)};
+     * 
+ * + * @return new instance of the Bck2Brwsr compiler + * @since 0.5 + */ + public static Bck2Brwsr newCompiler() { + StringArray arr = StringArray.asList(VM.class.getName().replace('.', '/')); + return new Bck2Brwsr(ObfuscationLevel.NONE, arr, null); } - /** Generates virtual machine from bytes served by a resources - * provider. + /** Creates new instance of the Bck2Brwsr compiler which inherits + * all values from this instance and adds additional classes + * to the list of those that should be compiled by the {@link #generate(java.lang.Appendable)} + * method. + * + * @param classes the classes to add to the compilation + * @return new instance of the compiler + */ + public Bck2Brwsr addRootClasses(String... classes) { + if (classes.length == 0) { + return this; + } else { + return new Bck2Brwsr(level, this.classes.addAndNew(classes), res); + } + } + + /** Changes the obfuscation level for the compiler by creating new instance + * which inherits all values from this and adjust the level + * of obfuscation. + * + * @param level the new level of obfuscation + * @return new instance of the compiler with changed level of obfuscation + * @since 0.5 + */ + public Bck2Brwsr obfuscation(ObfuscationLevel level) { + return new Bck2Brwsr(level, classes, res); + } + + /** A way to change the provider of additional resources (classes) for the + * compiler. + * + * @param res the implementation of resources provider + * @return new instance of the compiler with all values remaining the same, just + * with different resources provider + * @since 0.5 + */ + public Bck2Brwsr resources(Resources res) { + return new Bck2Brwsr(level, classes, res); + } + + /** A way to change the provider of additional resources (classes) for the + * compiler by specifying classloader to use for loading them. + * + * @param loader class loader to load the resources from + * @return new instance of the compiler with all values being the same, just + * different resources provider + * @since 0.5 + */ + public Bck2Brwsr resources(final ClassLoader loader) { + return resources(new LdrRsrcs(loader)); + } + + /** Generates virtual machine based on previous configuration of the + * compiler. * * @param out the output to write the generated JavaScript to - * @param obfuscationLevel the obfuscation level for the generated - * JavaScript - * @param resources provider of class files to use - * @param classes additional classes to include in the generated script - * @throws IOException I/O exception can be thrown when something goes wrong * @since 0.5 */ - public static void generate(Appendable out, ObfuscationLevel obfuscationLevel, Resources resources, String... classes) throws IOException { - StringArray arr = StringArray.asList(classes); - arr.add(VM.class.getName().replace('.', '/')); - - if (obfuscationLevel != ObfuscationLevel.NONE) { + public void generate(Appendable out) throws IOException { + Resources r = res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader()); + if (level != ObfuscationLevel.NONE) { try { - ClosureWrapper.produceTo(out, obfuscationLevel, resources, arr); + ClosureWrapper.produceTo(out, level, r, classes); return; } catch (IOException ex) { throw ex; @@ -107,35 +174,7 @@ } } - VM.compile(resources, out, arr); - } - - /** Generates virtual machine from bytes served by a class loader. - * - * @param out the output to write the generated JavaScript to - * @param obfuscationLevel the obfuscation level for the generated - * JavaScript - * @param loader class loader to load needed classes from - * @param classes additional classes to include in the generated script - * @throws IOException I/O exception can be thrown when something goes wrong - * @since 0.5 - */ - public static void generate(Appendable out, ObfuscationLevel obfuscationLevel, final ClassLoader loader, String... classes) throws IOException { - class R implements Resources { - @Override - public InputStream get(String name) throws IOException { - Enumeration en = loader.getResources(name); - URL u = null; - while (en.hasMoreElements()) { - u = en.nextElement(); - } - if (u == null) { - throw new IOException("Can't find " + name); - } - return u.openStream(); - } - } - generate(out, obfuscationLevel, new R(), classes); + VM.compile(r, out, classes); } /** Provider of resources (classes and other files). The diff -r 95c6ffa9d8e7 -r 2bcbe348dbec rt/vm/src/main/java/org/apidesign/vm4brwsr/LdrRsrcs.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/LdrRsrcs.java Fri Mar 22 14:46:10 2013 +0100 @@ -0,0 +1,48 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Enumeration; + +/** Implementation of Resources that delegates to some class loader. + * + * @author Jaroslav Tulach + */ +final class LdrRsrcs implements Bck2Brwsr.Resources { + private final ClassLoader loader; + + LdrRsrcs(ClassLoader loader) { + this.loader = loader; + } + + @Override + public InputStream get(String name) throws IOException { + Enumeration en = loader.getResources(name); + URL u = null; + while (en.hasMoreElements()) { + u = en.nextElement(); + } + if (u == null) { + throw new IOException("Can't find " + name); + } + return u.openStream(); + } +} diff -r 95c6ffa9d8e7 -r 2bcbe348dbec rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java Fri Mar 22 10:52:51 2013 +0100 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java Fri Mar 22 14:46:10 2013 +0100 @@ -43,6 +43,25 @@ } arr[arr.length - 1] = s; } + + StringArray addAndNew(String... values) { + int j; + String[] tmp; + if (arr == null) { + tmp = new String[values.length]; + j = 0; + } else { + tmp = new String[arr.length + values.length]; + for (int i = 0; i < arr.length; i++) { + tmp[i] = arr[i]; + } + j = arr.length; + } + for (int i = 0; i < values.length;) { + tmp[j++] = values[i++]; + } + return new StringArray(tmp); + } public String[] toArray() { return arr == null ? new String[0] : arr; @@ -93,5 +112,4 @@ } return -1; } - }