Different meaning of root vs. added classes. Ability to explicitly enumerate classes that should be exported and available with fully qualified name.
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.URISyntaxException;
26 import java.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.List;
29 import java.util.jar.JarEntry;
30 import java.util.jar.JarFile;
31 import java.util.zip.ZipEntry;
32 import org.apidesign.vm4brwsr.Bck2Brwsr;
36 * @author Jaroslav Tulach
39 static String compileJAR(final JarFile jar) throws IOException {
40 List<String> arr = new ArrayList<>();
41 List<String> classes = new ArrayList<>();
42 listJAR(jar, classes, arr);
43 StringWriter w = new StringWriter();
45 class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
47 public InputStream get(String resource) throws IOException {
48 InputStream is = jar.getInputStream(new ZipEntry(resource));
49 return is == null ? super.get(resource) : is;
53 Bck2Brwsr.newCompiler()
54 .addClasses(classes.toArray(new String[0]))
56 .resources(new JarRes())
60 } catch (Throwable ex) {
61 throw new IOException("Cannot compile: ", ex);
67 static String compileFromClassPath(URL u) throws IOException, URISyntaxException {
68 File f = new File(u.toURI());
69 for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
70 if (!f.getPath().startsWith(s)) {
73 File root = new File(s);
74 List<String> arr = new ArrayList<>();
75 List<String> classes = new ArrayList<>();
76 listDir(root, null, classes, arr);
77 StringWriter w = new StringWriter();
79 Bck2Brwsr.newCompiler()
80 .addRootClasses(classes.toArray(new String[0]))
82 .resources(new EmulationResources())
86 } catch (ClassFormatError ex) {
87 throw new IOException(ex);
95 private static void listJAR(JarFile j, List<String> classes, List<String> resources) throws IOException {
96 Enumeration<JarEntry> en = j.entries();
97 while (en.hasMoreElements()) {
98 JarEntry e = en.nextElement();
99 final String n = e.getName();
100 if (n.endsWith("/")) {
103 int last = n.lastIndexOf('/');
104 String pkg = n.substring(0, last + 1);
108 if (n.endsWith(".class")) {
109 classes.add(n.substring(0, n.length() - 6));
116 private static boolean skipPkg(String pkg) {
117 return pkg.equals("org/apidesign/bck2brwsr/launcher/");
120 private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
121 File[] arr = f.listFiles();
123 if (f.getName().endsWith(".class")) {
124 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
126 resources.add(pref + f.getName());
129 for (File ch : arr) {
131 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
136 static class EmulationResources implements Bck2Brwsr.Resources {
139 public InputStream get(String name) throws IOException {
140 Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
142 while (en.hasMoreElements()) {
143 u = en.nextElement();
146 throw new IOException("Can't find " + name);
148 if (u.toExternalForm().contains("rt.jar!")) {
149 throw new IOException("No emulation for " + u);
151 return u.openStream();