Fixing unit test.
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.io.BufferedWriter;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.Writer;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.Enumeration;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.LinkedList;
32 import java.util.List;
35 /** Generator of JavaScript from bytecode of classes on classpath of the VM.
37 * @author Jaroslav Tulach <jtulach@netbeans.org>
42 public static void main(String... args) throws IOException {
43 if (args.length < 2) {
44 System.err.println("Usage: java -cp ... -jar ... <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
48 Writer w = new BufferedWriter(new FileWriter(args[0]));
49 List<String> classes = Arrays.asList(args).subList(1, args.length);
54 static void compile(Appendable out, String... names) throws IOException {
55 compile(out, Arrays.asList(names));
57 static void compile(Appendable out, List<String> names) throws IOException {
58 compile(GenJS.class.getClassLoader(), out, names);
60 static void compile(ClassLoader l, Appendable out, List<String> names) throws IOException {
61 for (String baseClass : names) {
62 Map<String,String> processed = new HashMap<String, String>();
63 LinkedList<String> toProcess = new LinkedList<String>();
64 toProcess.add(baseClass);
67 Iterator<String> it = toProcess.iterator();
68 while (it.hasNext() && name == null) {
70 if (processed.get(n) != null) {
78 if (name.startsWith("java/")
79 && !name.equals("java/lang/Object")
80 && !name.equals("java/lang/Class")
81 && !name.equals("java/lang/Math")
82 && !name.equals("java/lang/Number")
83 && !name.equals("java/lang/NumberFormatException")
84 && !name.equals("java/lang/IllegalArgumentException")
85 && !name.equals("java/lang/Integer")
86 && !name.equals("java/lang/Float")
87 && !name.equals("java/lang/Double")
88 && !name.equals("java/lang/Throwable")
89 && !name.equals("java/lang/Exception")
90 && !name.equals("java/lang/RuntimeException")
91 && !name.equals("java/lang/UnsupportedOperationException")
92 && !name.equals("java/lang/String")
93 && !name.equals("java/lang/String$CaseInsensitiveComparator")
94 && !name.equals("java/lang/StringBuilder")
95 && !name.equals("java/lang/AbstractStringBuilder")
97 processed.put(name, "");
100 InputStream is = loadClass(l, name);
102 throw new IOException("Can't find class " + name);
104 LinkedList<String> scripts = new LinkedList<String>();
106 String initCode = ByteCodeToJavaScript.compile(is, out, toProcess, scripts);
107 processed.put(name, initCode == null ? "" : initCode);
108 } catch (RuntimeException ex) {
109 if (out instanceof CharSequence) {
110 CharSequence seq = (CharSequence)out;
111 int lastBlock = seq.length();
112 while (lastBlock-- >= 0) {
113 if (seq.charAt(lastBlock) == '{') {
117 throw new IOException("Error while compiling " + name + "\n"
118 + seq.subSequence(lastBlock + 1, seq.length()), ex
121 throw new IOException("Error while compiling " + name + "\n"
126 for (String resource : scripts) {
127 while (resource.startsWith("/")) {
128 resource = resource.substring(1);
130 InputStream emul = l.getResourceAsStream(resource);
132 throw new IOException("Can't find " + resource);
134 readResource(emul, out);
138 Collections.reverse(toProcess);
140 for (String clazz : toProcess) {
141 String initCode = processed.remove(clazz);
142 if (initCode != null) {
143 out.append(initCode).append("\n");
148 private static void readResource(InputStream emul, Appendable out) throws IOException {
152 int ch = emul.read();
156 if (ch < 0 || ch > 255) {
157 throw new IOException("Invalid char in emulation " + ch);
164 out.append((char)ch);
171 out.append('/').append((char)ch);
194 private static InputStream loadClass(ClassLoader l, String name) throws IOException {
195 Enumeration<URL> en = l.getResources(name + ".class");
197 while (en.hasMoreElements()) {
198 u = en.nextElement();
201 throw new IOException("Can't find " + name);
203 return u.openStream();