vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
changeset 29 dcb98731b000
child 34 6fcc0dfbe324
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java	Tue Sep 25 12:11:03 2012 +0200
     1.3 @@ -0,0 +1,76 @@
     1.4 +package org.apidesign.vm4brwsr;
     1.5 +
     1.6 +import java.io.BufferedWriter;
     1.7 +import java.io.FileWriter;
     1.8 +import java.io.IOException;
     1.9 +import java.io.InputStream;
    1.10 +import java.io.Writer;
    1.11 +import java.util.Arrays;
    1.12 +import java.util.HashSet;
    1.13 +import java.util.LinkedList;
    1.14 +import java.util.List;
    1.15 +import java.util.Set;
    1.16 +
    1.17 +/** Generator of JavaScript from bytecode of classes on classpath of the VM.
    1.18 + *
    1.19 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.20 + */
    1.21 +final class GenJS {
    1.22 +    private GenJS() {}
    1.23 +    
    1.24 +    public static void main(String... args) throws IOException {
    1.25 +        if (args.length < 2) {
    1.26 +            System.err.println("Usage: java -cp ... -jar ... <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
    1.27 +            return;
    1.28 +        }
    1.29 +        
    1.30 +        Writer w = new BufferedWriter(new FileWriter(args[0]));
    1.31 +        List<String> classes = Arrays.asList(args).subList(1, args.length);
    1.32 +        compile(w, classes);
    1.33 +        w.close();
    1.34 +    }
    1.35 +    
    1.36 +    static void compile(Appendable out, String... names) throws IOException {
    1.37 +        compile(out, Arrays.asList(names));
    1.38 +    }
    1.39 +    static void compile(Appendable out, List<String> names) throws IOException {
    1.40 +        Set<String> processed = new HashSet<String>();
    1.41 +        LinkedList<String> toProcess = new LinkedList<String>(names);
    1.42 +        for (;;) {
    1.43 +            toProcess.removeAll(processed);
    1.44 +            if (toProcess.isEmpty()) {
    1.45 +                break;
    1.46 +            }
    1.47 +            String name = toProcess.getFirst();
    1.48 +            processed.add(name);
    1.49 +            if (name.startsWith("java/") && !name.equals("java/lang/Object")) {
    1.50 +                continue;
    1.51 +            }
    1.52 +            InputStream is = GenJS.class.getClassLoader().getResourceAsStream(name + ".class");
    1.53 +            if (is == null) {
    1.54 +                throw new IOException("Can't find class " + name); 
    1.55 +            }
    1.56 +            try {
    1.57 +                ByteCodeToJavaScript.compile(is, out, toProcess);
    1.58 +            } catch (RuntimeException ex) {
    1.59 +                if (out instanceof CharSequence) {
    1.60 +                    CharSequence seq = (CharSequence)out;
    1.61 +                    int lastBlock = seq.length();
    1.62 +                    while (lastBlock-- >= 0) {
    1.63 +                        if (seq.charAt(lastBlock) == '{') {
    1.64 +                            break;
    1.65 +                        }
    1.66 +                    }
    1.67 +                    throw new IOException("Error while compiling " + name + "\n" 
    1.68 +                        + seq.subSequence(lastBlock + 1, seq.length()), ex
    1.69 +                    );
    1.70 +                } else {
    1.71 +                    throw new IOException("Error while compiling " + name + "\n" 
    1.72 +                        + out, ex
    1.73 +                    );
    1.74 +                }
    1.75 +            }
    1.76 +        }
    1.77 +    }
    1.78 +    
    1.79 +}