Support for exporting whole packages. addRootClasses defined in terms of addClasses and addExported.
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;
20 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.StringWriter;
26 import java.net.JarURLConnection;
27 import java.net.URISyntaxException;
29 import java.util.ArrayList;
30 import java.util.Enumeration;
31 import java.util.HashSet;
32 import java.util.List;
34 import java.util.jar.JarEntry;
35 import java.util.jar.JarFile;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38 import java.util.zip.ZipEntry;
39 import org.apidesign.bck2brwsr.launcher.BaseHTTPLauncher.Res;
40 import org.apidesign.vm4brwsr.Bck2Brwsr;
44 * @author Jaroslav Tulach
47 private static final Logger LOG = Logger.getLogger(CompileCP.class.getName());
48 static String compileJAR(final JarFile jar, Set<String> testClasses)
50 List<String> arr = new ArrayList<>();
51 List<String> classes = new ArrayList<>();
52 Set<String> keep = new HashSet<String>(testClasses);
53 listJAR(jar, classes, arr, keep);
55 StringWriter w = new StringWriter();
57 class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
59 public InputStream get(String resource) throws IOException {
60 InputStream is = jar.getInputStream(new ZipEntry(resource));
61 return is == null ? super.get(resource) : is;
65 Bck2Brwsr.newCompiler()
66 .addClasses(classes.toArray(new String[0]))
67 .addExported(keep.toArray(new String[0]))
68 .addResources(arr.toArray(new String[0]))
70 .resources(new JarRes())
74 } catch (IOException ex) {
76 } catch (Throwable ex) {
77 throw new IOException("Cannot compile: ", ex);
83 static String compileFromClassPath(URL u, final Res r) throws IOException {
86 f = new File(u.toURI());
87 } catch (URISyntaxException ex) {
88 throw new IOException(ex);
90 for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
91 if (!f.getPath().startsWith(s)) {
94 File root = new File(s);
95 List<String> arr = new ArrayList<>();
96 List<String> classes = new ArrayList<>();
97 listDir(root, null, classes, arr);
98 StringWriter w = new StringWriter();
100 Bck2Brwsr.newCompiler()
101 .addRootClasses(classes.toArray(new String[0]))
102 .addResources(arr.toArray(new String[0]))
104 .resources(new EmulationResources() {
106 public InputStream get(String resource) throws IOException {
108 final URL url = r.get(resource, 0);
109 return url == null ? null : url.openStream();
111 return super.get(resource);
117 } catch (ClassFormatError ex) {
118 throw new IOException(ex);
126 private static void listJAR(
127 JarFile j, List<String> classes,
128 List<String> resources, Set<String> keep
129 ) throws IOException {
130 Enumeration<JarEntry> en = j.entries();
131 while (en.hasMoreElements()) {
132 JarEntry e = en.nextElement();
133 final String n = e.getName();
134 if (n.endsWith("/")) {
137 int last = n.lastIndexOf('/');
138 String pkg = n.substring(0, last + 1);
142 if (n.endsWith(".class")) {
143 classes.add(n.substring(0, n.length() - 6));
146 if (n.startsWith("META-INF/services/") && keep != null) {
147 BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
149 String l = r.readLine();
153 if (l.startsWith("#")) {
156 keep.add(l.replace('.', '/'));
161 String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
162 if (exp != null && keep != null) {
163 for (String def : exp.split(",")) {
164 for (String sep : def.split(";")) {
165 keep.add(sep.replace('.', '/') + "/");
172 private static boolean skipPkg(String pkg) {
173 return pkg.equals("org/apidesign/bck2brwsr/launcher/");
176 private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
177 File[] arr = f.listFiles();
179 if (f.getName().endsWith(".class")) {
180 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
182 resources.add(pref + f.getName());
185 for (File ch : arr) {
187 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
192 static void compileVM(StringBuilder sb, final Res r) throws IOException {
193 List<String> arr = new ArrayList<>();
194 List<String> classes = new ArrayList<>();
197 URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
198 JarURLConnection juc = (JarURLConnection)u.openConnection();
199 listJAR(juc.getJarFile(), classes, arr, null);
202 URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
203 JarURLConnection juc = (JarURLConnection)u.openConnection();
204 listJAR(juc.getJarFile(), classes, arr, null);
207 Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
208 .resources(new Bck2Brwsr.Resources() {
210 public InputStream get(String resource) throws IOException {
211 final URL url = r.get(resource, 0);
212 return url == null ? null : url.openStream();
217 static class EmulationResources implements Bck2Brwsr.Resources {
220 public InputStream get(String name) throws IOException {
221 Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
223 while (en.hasMoreElements()) {
224 u = en.nextElement();
227 LOG.log(Level.WARNING, "Cannot find {0}", name);
230 if (u.toExternalForm().contains("/rt.jar!")) {
231 LOG.warning(name + "No bootdelegation for ");
234 return u.openStream();