Merging from default branch and resolving conflicts. mvn install -DskipTests passes OK.
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.bck2brwsr.launcher;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.StringWriter;
24 import java.net.JarURLConnection;
25 import java.net.URISyntaxException;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.List;
30 import java.util.jar.JarEntry;
31 import java.util.jar.JarFile;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34 import java.util.zip.ZipEntry;
35 import org.apidesign.bck2brwsr.launcher.BaseHTTPLauncher.Res;
36 import org.apidesign.vm4brwsr.Bck2Brwsr;
40 * @author Jaroslav Tulach
43 static String compileJAR(final JarFile jar) throws IOException {
44 List<String> arr = new ArrayList<>();
45 List<String> classes = new ArrayList<>();
46 listJAR(jar, classes, arr);
47 StringWriter w = new StringWriter();
49 class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
51 public InputStream get(String resource) throws IOException {
52 InputStream is = jar.getInputStream(new ZipEntry(resource));
53 return is == null ? super.get(resource) : is;
57 Bck2Brwsr.newCompiler()
58 .addClasses(classes.toArray(new String[0]))
59 .addResources(arr.toArray(new String[0]))
61 .resources(new JarRes())
65 } catch (Throwable ex) {
66 throw new IOException("Cannot compile: ", ex);
72 static String compileFromClassPath(URL u, final Res r) throws IOException {
75 f = new File(u.toURI());
76 } catch (URISyntaxException ex) {
77 throw new IOException(ex);
79 for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
80 if (!f.getPath().startsWith(s)) {
83 File root = new File(s);
84 List<String> arr = new ArrayList<>();
85 List<String> classes = new ArrayList<>();
86 listDir(root, null, classes, arr);
87 StringWriter w = new StringWriter();
89 Bck2Brwsr.newCompiler()
90 .addRootClasses(classes.toArray(new String[0]))
91 .addResources(arr.toArray(new String[0]))
93 .resources(new EmulationResources() {
95 public InputStream get(String resource) throws IOException {
96 return r != null ? r.get(resource, 0).openStream() : super.get(resource);
102 } catch (ClassFormatError ex) {
103 throw new IOException(ex);
111 private static void listJAR(JarFile j, List<String> classes, List<String> resources) throws IOException {
112 Enumeration<JarEntry> en = j.entries();
113 while (en.hasMoreElements()) {
114 JarEntry e = en.nextElement();
115 final String n = e.getName();
116 if (n.contains("package-info")) {
119 if (n.endsWith("/")) {
122 int last = n.lastIndexOf('/');
123 String pkg = n.substring(0, last + 1);
127 if (n.endsWith(".class")) {
128 classes.add(n.substring(0, n.length() - 6));
135 private static boolean skipPkg(String pkg) {
136 return pkg.equals("org/apidesign/bck2brwsr/launcher/");
139 private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
140 File[] arr = f.listFiles();
142 if (f.getName().equals("package-info.class")) {
145 if (f.getName().endsWith(".class")) {
146 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
148 resources.add(pref + f.getName());
151 for (File ch : arr) {
153 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
158 static void compileVM(StringBuilder sb, final Res r) throws IOException {
159 URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
160 JarURLConnection juc = (JarURLConnection)u.openConnection();
162 List<String> arr = new ArrayList<>();
163 List<String> classes = new ArrayList<>();
164 listJAR(juc.getJarFile(), classes, arr);
166 Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
167 .resources(new Bck2Brwsr.Resources() {
169 public InputStream get(String resource) throws IOException {
170 return r.get(resource, 0).openStream();
175 static class EmulationResources implements Bck2Brwsr.Resources {
178 public InputStream get(String name) throws IOException {
179 Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
181 while (en.hasMoreElements()) {
182 u = en.nextElement();
185 throw new IOException("Can't find " + name);
187 if (u.toExternalForm().contains("rt.jar!")) {
188 throw new IOException("No emulation for " + u);
190 return u.openStream();