Main method to compile some classes
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 25 Sep 2012 12:11:03 +0200
changeset 29dcb98731b000
parent 28 81ad7a739fed
child 30 7efb52f76270
Main method to compile some classes
vm/pom.xml
vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
     1.1 --- a/vm/pom.xml	Tue Sep 25 09:55:34 2012 +0200
     1.2 +++ b/vm/pom.xml	Tue Sep 25 12:11:03 2012 +0200
     1.3 @@ -45,18 +45,29 @@
     1.4        <connection>scm:hg:http://source.apidesign.org/hg/bck2brwsr</connection>
     1.5        <url>http://source.apidesign.org/hg/bck2brwsr</url>
     1.6    </scm>
     1.7 -    <build>
     1.8 -        <plugins>
     1.9 -            <plugin>
    1.10 -                <groupId>com.mycila.maven-license-plugin</groupId>
    1.11 -                <artifactId>maven-license-plugin</artifactId>
    1.12 -                <version>1.9.0</version>
    1.13 -                <configuration>
    1.14 -                    <header>src/header.txt</header>
    1.15 -                </configuration>
    1.16 -            </plugin>
    1.17 -        </plugins>
    1.18 -    </build>
    1.19 +  <build>
    1.20 +      <plugins>
    1.21 +          <plugin>
    1.22 +              <groupId>org.apache.maven.plugins</groupId>
    1.23 +              <artifactId>maven-jar-plugin</artifactId>
    1.24 +              <configuration>
    1.25 +                  <archive>
    1.26 +                      <manifest>
    1.27 +                          <mainClass>org.apidesign.vm4brwsr.GenJS</mainClass>
    1.28 +                      </manifest>
    1.29 +                  </archive>
    1.30 +              </configuration>
    1.31 +          </plugin>
    1.32 +          <plugin>
    1.33 +              <groupId>com.mycila.maven-license-plugin</groupId>
    1.34 +              <artifactId>maven-license-plugin</artifactId>
    1.35 +              <version>1.9.0</version>
    1.36 +              <configuration>
    1.37 +                  <header>src/header.txt</header>
    1.38 +              </configuration>
    1.39 +          </plugin>
    1.40 +      </plugins>
    1.41 +  </build>
    1.42    <dependencies>
    1.43      <dependency>
    1.44        <groupId>org.testng</groupId>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java	Tue Sep 25 12:11:03 2012 +0200
     2.3 @@ -0,0 +1,76 @@
     2.4 +package org.apidesign.vm4brwsr;
     2.5 +
     2.6 +import java.io.BufferedWriter;
     2.7 +import java.io.FileWriter;
     2.8 +import java.io.IOException;
     2.9 +import java.io.InputStream;
    2.10 +import java.io.Writer;
    2.11 +import java.util.Arrays;
    2.12 +import java.util.HashSet;
    2.13 +import java.util.LinkedList;
    2.14 +import java.util.List;
    2.15 +import java.util.Set;
    2.16 +
    2.17 +/** Generator of JavaScript from bytecode of classes on classpath of the VM.
    2.18 + *
    2.19 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.20 + */
    2.21 +final class GenJS {
    2.22 +    private GenJS() {}
    2.23 +    
    2.24 +    public static void main(String... args) throws IOException {
    2.25 +        if (args.length < 2) {
    2.26 +            System.err.println("Usage: java -cp ... -jar ... <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
    2.27 +            return;
    2.28 +        }
    2.29 +        
    2.30 +        Writer w = new BufferedWriter(new FileWriter(args[0]));
    2.31 +        List<String> classes = Arrays.asList(args).subList(1, args.length);
    2.32 +        compile(w, classes);
    2.33 +        w.close();
    2.34 +    }
    2.35 +    
    2.36 +    static void compile(Appendable out, String... names) throws IOException {
    2.37 +        compile(out, Arrays.asList(names));
    2.38 +    }
    2.39 +    static void compile(Appendable out, List<String> names) throws IOException {
    2.40 +        Set<String> processed = new HashSet<String>();
    2.41 +        LinkedList<String> toProcess = new LinkedList<String>(names);
    2.42 +        for (;;) {
    2.43 +            toProcess.removeAll(processed);
    2.44 +            if (toProcess.isEmpty()) {
    2.45 +                break;
    2.46 +            }
    2.47 +            String name = toProcess.getFirst();
    2.48 +            processed.add(name);
    2.49 +            if (name.startsWith("java/") && !name.equals("java/lang/Object")) {
    2.50 +                continue;
    2.51 +            }
    2.52 +            InputStream is = GenJS.class.getClassLoader().getResourceAsStream(name + ".class");
    2.53 +            if (is == null) {
    2.54 +                throw new IOException("Can't find class " + name); 
    2.55 +            }
    2.56 +            try {
    2.57 +                ByteCodeToJavaScript.compile(is, out, toProcess);
    2.58 +            } catch (RuntimeException ex) {
    2.59 +                if (out instanceof CharSequence) {
    2.60 +                    CharSequence seq = (CharSequence)out;
    2.61 +                    int lastBlock = seq.length();
    2.62 +                    while (lastBlock-- >= 0) {
    2.63 +                        if (seq.charAt(lastBlock) == '{') {
    2.64 +                            break;
    2.65 +                        }
    2.66 +                    }
    2.67 +                    throw new IOException("Error while compiling " + name + "\n" 
    2.68 +                        + seq.subSequence(lastBlock + 1, seq.length()), ex
    2.69 +                    );
    2.70 +                } else {
    2.71 +                    throw new IOException("Error while compiling " + name + "\n" 
    2.72 +                        + out, ex
    2.73 +                    );
    2.74 +                }
    2.75 +            }
    2.76 +        }
    2.77 +    }
    2.78 +    
    2.79 +}
     3.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java	Tue Sep 25 09:55:34 2012 +0200
     3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java	Tue Sep 25 12:11:03 2012 +0200
     3.3 @@ -17,15 +17,7 @@
     3.4  */
     3.5  package org.apidesign.vm4brwsr;
     3.6  
     3.7 -import org.apidesign.vm4brwsr.ByteCodeToJavaScript;
     3.8  import java.io.IOException;
     3.9 -import java.io.InputStream;
    3.10 -import java.util.Arrays;
    3.11 -import java.util.HashSet;
    3.12 -import java.util.Iterator;
    3.13 -import java.util.LinkedList;
    3.14 -import java.util.Set;
    3.15 -import java.util.TreeSet;
    3.16  import javax.script.Invocable;
    3.17  import javax.script.ScriptEngine;
    3.18  import javax.script.ScriptEngineManager;
    3.19 @@ -162,32 +154,7 @@
    3.20          if (sb == null) {
    3.21              sb = new StringBuilder();
    3.22          }
    3.23 -        Set<String> processed = new HashSet<String>();
    3.24 -
    3.25 -        LinkedList<String> toProcess = new LinkedList<String>(Arrays.asList(names));
    3.26 -        for (;;) {
    3.27 -            toProcess.removeAll(processed);
    3.28 -            if (toProcess.isEmpty()) {
    3.29 -                break;
    3.30 -            }
    3.31 -            String name = toProcess.getFirst();
    3.32 -            processed.add(name);
    3.33 -            if (name.startsWith("java/") && !name.equals("java/lang/Object")) {
    3.34 -                continue;
    3.35 -            }
    3.36 -            InputStream is = StaticMethodTest.class.getClassLoader().getResourceAsStream(name + ".class");
    3.37 -            assertNotNull(is, "Class file found");
    3.38 -            try {
    3.39 -                ByteCodeToJavaScript.compile(is, sb, toProcess);
    3.40 -            } catch (RuntimeException ex) {
    3.41 -                int lastBlock = sb.lastIndexOf("{");
    3.42 -                throw new IllegalStateException(
    3.43 -                    "Error while compiling " + name + "\n" + 
    3.44 -                    sb.substring(0, sb.length()), 
    3.45 -                    ex
    3.46 -                );
    3.47 -            }
    3.48 -        }
    3.49 +        GenJS.compile(sb, names);
    3.50          ScriptEngineManager sem = new ScriptEngineManager();
    3.51          ScriptEngine js = sem.getEngineByExtension("js");
    3.52          try {