# HG changeset patch # User Lubomir Nerad # Date 1363187867 -3600 # Node ID d95117153304ecfb2059484afad6e782b6c0dc2c # Parent 677c9bcf507fdf3b6caf2483ef421380daa9d557 Allow to set obfuscation level for j2js diff -r 677c9bcf507f -r d95117153304 rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java --- a/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java Wed Mar 13 11:00:08 2013 +0100 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java Wed Mar 13 16:17:47 2013 +0100 @@ -35,6 +35,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; import org.apidesign.vm4brwsr.Bck2Brwsr; +import org.apidesign.vm4brwsr.ObfuscationLevel; /** Compiles classes into JavaScript. */ @Mojo(name="j2js", defaultPhase=LifecyclePhase.PROCESS_CLASSES) @@ -48,11 +49,12 @@ package under the classes directory */ @Parameter private File javascript; - + @Parameter(defaultValue="${project}") private MavenProject prj; - - + + @Parameter(defaultValue="NONE") + private ObfuscationLevel obfuscation; @Override public void execute() throws MojoExecutionException { @@ -74,7 +76,7 @@ try { URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts()); FileWriter w = new FileWriter(javascript); - Bck2Brwsr.generate(w, url, arr.toArray(new String[0])); + Bck2Brwsr.generate(w, obfuscation, url, arr.toArray(new String[0])); w.close(); } catch (IOException ex) { throw new MojoExecutionException("Can't compile", ex); diff -r 677c9bcf507f -r d95117153304 rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Wed Mar 13 11:00:08 2013 +0100 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Wed Mar 13 16:17:47 2013 +0100 @@ -56,35 +56,69 @@ public final class Bck2Brwsr { private Bck2Brwsr() { } - + /** Generates virtual machine from bytes served by a resources * provider. - * + * * @param out the output to write the generated JavaScript to * @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 */ public static void generate(Appendable out, Resources resources, String... classes) throws IOException { + generate(out, ObfuscationLevel.NONE, resources, classes); + } + + /** Generates 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 + * @param classes additional classes to include in the generated script + * @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); + } + + /** Generates virtual machine from bytes served by a resources + * provider. + * + * @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 + */ + 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('.', '/')); - try { - ClosureWrapper.produceTo(out, resources, arr); - } catch (IOException ex) { - throw ex; - } catch (Throwable ex) { - VM.compile(resources, out, arr); + + if (obfuscationLevel != ObfuscationLevel.NONE) { + try { + ClosureWrapper.produceTo(out, obfuscationLevel, resources, arr); + return; + } catch (IOException ex) { + throw ex; + } catch (Throwable ex) { + out.append("/* Failed to obfuscate: " + ex.getMessage() + + " */\n"); + } } + + 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 */ - public static void generate(Appendable out, final ClassLoader loader, String... classes) throws IOException { + 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 { @@ -99,7 +133,7 @@ return u.openStream(); } } - generate(out, new R(), classes); + generate(out, obfuscationLevel, new R(), classes); } /** Provider of resources (classes and other files). The diff -r 677c9bcf507f -r d95117153304 rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java Wed Mar 13 11:00:08 2013 +0100 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java Wed Mar 13 16:17:47 2013 +0100 @@ -37,9 +37,10 @@ private String code; private final Bck2Brwsr.Resources res; private final StringArray classes; - private ClosureWrapper(Appendable out, Bck2Brwsr.Resources res, StringArray classes) { + private ClosureWrapper(Appendable out, ObfuscationLevel obfuscationLevel, + Bck2Brwsr.Resources res, StringArray classes) { super( - ARGS, + generateArguments(obfuscationLevel), new PrintStream(new APS(out)), System.err ); this.res = res; @@ -67,7 +68,7 @@ } return code; } - + private static final class APS extends OutputStream { private final Appendable out; @@ -79,8 +80,18 @@ out.append((char)b); } } - static int produceTo(Appendable w, Bck2Brwsr.Resources resources, StringArray arr) throws IOException { - ClosureWrapper cw = new ClosureWrapper(w, resources, arr); + + private static String[] generateArguments( + ObfuscationLevel obfuscationLevel) { + String[] finalArgs = ARGS.clone(); + finalArgs[1] = obfuscationLevel.toString(); + + return finalArgs; + } + + static int produceTo(Appendable w, ObfuscationLevel obfuscationLevel, Bck2Brwsr.Resources resources, StringArray arr) throws IOException { + ClosureWrapper cw = new ClosureWrapper(w, obfuscationLevel, resources, + arr); try { return cw.doRun(); } catch (FlagUsageException ex) {