1.1 --- a/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java Wed Mar 13 11:00:08 2013 +0100
1.2 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java Wed Mar 13 16:17:47 2013 +0100
1.3 @@ -35,6 +35,7 @@
1.4 import org.apache.maven.plugins.annotations.Parameter;
1.5 import org.apache.maven.project.MavenProject;
1.6 import org.apidesign.vm4brwsr.Bck2Brwsr;
1.7 +import org.apidesign.vm4brwsr.ObfuscationLevel;
1.8
1.9 /** Compiles classes into JavaScript. */
1.10 @Mojo(name="j2js", defaultPhase=LifecyclePhase.PROCESS_CLASSES)
1.11 @@ -48,11 +49,12 @@
1.12 package under the classes directory */
1.13 @Parameter
1.14 private File javascript;
1.15 -
1.16 +
1.17 @Parameter(defaultValue="${project}")
1.18 private MavenProject prj;
1.19 -
1.20 -
1.21 +
1.22 + @Parameter(defaultValue="NONE")
1.23 + private ObfuscationLevel obfuscation;
1.24
1.25 @Override
1.26 public void execute() throws MojoExecutionException {
1.27 @@ -74,7 +76,7 @@
1.28 try {
1.29 URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
1.30 FileWriter w = new FileWriter(javascript);
1.31 - Bck2Brwsr.generate(w, url, arr.toArray(new String[0]));
1.32 + Bck2Brwsr.generate(w, obfuscation, url, arr.toArray(new String[0]));
1.33 w.close();
1.34 } catch (IOException ex) {
1.35 throw new MojoExecutionException("Can't compile", ex);
2.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Wed Mar 13 11:00:08 2013 +0100
2.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Wed Mar 13 16:17:47 2013 +0100
2.3 @@ -56,35 +56,69 @@
2.4 public final class Bck2Brwsr {
2.5 private Bck2Brwsr() {
2.6 }
2.7 -
2.8 +
2.9 /** Generates virtual machine from bytes served by a <code>resources</code>
2.10 * provider.
2.11 - *
2.12 + *
2.13 * @param out the output to write the generated JavaScript to
2.14 * @param resources provider of class files to use
2.15 * @param classes additional classes to include in the generated script
2.16 * @throws IOException I/O exception can be thrown when something goes wrong
2.17 */
2.18 public static void generate(Appendable out, Resources resources, String... classes) throws IOException {
2.19 + generate(out, ObfuscationLevel.NONE, resources, classes);
2.20 + }
2.21 +
2.22 + /** Generates virtual machine from bytes served by a class loader.
2.23 + *
2.24 + * @param out the output to write the generated JavaScript to
2.25 + * @param loader class loader to load needed classes from
2.26 + * @param classes additional classes to include in the generated script
2.27 + * @throws IOException I/O exception can be thrown when something goes wrong
2.28 + */
2.29 + public static void generate(Appendable out, ClassLoader loader, String... classes) throws IOException {
2.30 + generate(out, ObfuscationLevel.NONE, loader, classes);
2.31 + }
2.32 +
2.33 + /** Generates virtual machine from bytes served by a <code>resources</code>
2.34 + * provider.
2.35 + *
2.36 + * @param out the output to write the generated JavaScript to
2.37 + * @param obfuscationLevel the obfuscation level for the generated
2.38 + * JavaScript
2.39 + * @param resources provider of class files to use
2.40 + * @param classes additional classes to include in the generated script
2.41 + * @throws IOException I/O exception can be thrown when something goes wrong
2.42 + */
2.43 + public static void generate(Appendable out, ObfuscationLevel obfuscationLevel, Resources resources, String... classes) throws IOException {
2.44 StringArray arr = StringArray.asList(classes);
2.45 arr.add(VM.class.getName().replace('.', '/'));
2.46 - try {
2.47 - ClosureWrapper.produceTo(out, resources, arr);
2.48 - } catch (IOException ex) {
2.49 - throw ex;
2.50 - } catch (Throwable ex) {
2.51 - VM.compile(resources, out, arr);
2.52 +
2.53 + if (obfuscationLevel != ObfuscationLevel.NONE) {
2.54 + try {
2.55 + ClosureWrapper.produceTo(out, obfuscationLevel, resources, arr);
2.56 + return;
2.57 + } catch (IOException ex) {
2.58 + throw ex;
2.59 + } catch (Throwable ex) {
2.60 + out.append("/* Failed to obfuscate: " + ex.getMessage()
2.61 + + " */\n");
2.62 + }
2.63 }
2.64 +
2.65 + VM.compile(resources, out, arr);
2.66 }
2.67
2.68 /** Generates virtual machine from bytes served by a class loader.
2.69 *
2.70 * @param out the output to write the generated JavaScript to
2.71 + * @param obfuscationLevel the obfuscation level for the generated
2.72 + * JavaScript
2.73 * @param loader class loader to load needed classes from
2.74 * @param classes additional classes to include in the generated script
2.75 * @throws IOException I/O exception can be thrown when something goes wrong
2.76 */
2.77 - public static void generate(Appendable out, final ClassLoader loader, String... classes) throws IOException {
2.78 + public static void generate(Appendable out, ObfuscationLevel obfuscationLevel, final ClassLoader loader, String... classes) throws IOException {
2.79 class R implements Resources {
2.80 @Override
2.81 public InputStream get(String name) throws IOException {
2.82 @@ -99,7 +133,7 @@
2.83 return u.openStream();
2.84 }
2.85 }
2.86 - generate(out, new R(), classes);
2.87 + generate(out, obfuscationLevel, new R(), classes);
2.88 }
2.89
2.90 /** Provider of resources (classes and other files). The
3.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java Wed Mar 13 11:00:08 2013 +0100
3.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java Wed Mar 13 16:17:47 2013 +0100
3.3 @@ -37,9 +37,10 @@
3.4 private String code;
3.5 private final Bck2Brwsr.Resources res;
3.6 private final StringArray classes;
3.7 - private ClosureWrapper(Appendable out, Bck2Brwsr.Resources res, StringArray classes) {
3.8 + private ClosureWrapper(Appendable out, ObfuscationLevel obfuscationLevel,
3.9 + Bck2Brwsr.Resources res, StringArray classes) {
3.10 super(
3.11 - ARGS,
3.12 + generateArguments(obfuscationLevel),
3.13 new PrintStream(new APS(out)), System.err
3.14 );
3.15 this.res = res;
3.16 @@ -67,7 +68,7 @@
3.17 }
3.18 return code;
3.19 }
3.20 -
3.21 +
3.22 private static final class APS extends OutputStream {
3.23 private final Appendable out;
3.24
3.25 @@ -79,8 +80,18 @@
3.26 out.append((char)b);
3.27 }
3.28 }
3.29 - static int produceTo(Appendable w, Bck2Brwsr.Resources resources, StringArray arr) throws IOException {
3.30 - ClosureWrapper cw = new ClosureWrapper(w, resources, arr);
3.31 +
3.32 + private static String[] generateArguments(
3.33 + ObfuscationLevel obfuscationLevel) {
3.34 + String[] finalArgs = ARGS.clone();
3.35 + finalArgs[1] = obfuscationLevel.toString();
3.36 +
3.37 + return finalArgs;
3.38 + }
3.39 +
3.40 + static int produceTo(Appendable w, ObfuscationLevel obfuscationLevel, Bck2Brwsr.Resources resources, StringArray arr) throws IOException {
3.41 + ClosureWrapper cw = new ClosureWrapper(w, obfuscationLevel, resources,
3.42 + arr);
3.43 try {
3.44 return cw.doRun();
3.45 } catch (FlagUsageException ex) {