Export only test classes, classes from exported packages and those referenced in META-INF/services
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> exported = new HashSet<String>();
53 Set<String> keep = new HashSet<String>(testClasses);
54 listJAR(jar, classes, arr, exported, keep);
55 List<String> root = new ArrayList<>();
56 for (String c : classes) {
57 if (keep.contains(c)) {
61 int slash = c.lastIndexOf('/');
62 String pkg = c.substring(0, slash + 1);
63 if (exported.contains(pkg)) {
68 StringWriter w = new StringWriter();
70 class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
72 public InputStream get(String resource) throws IOException {
73 InputStream is = jar.getInputStream(new ZipEntry(resource));
74 return is == null ? super.get(resource) : is;
78 Bck2Brwsr.newCompiler()
79 .addClasses(classes.toArray(new String[0]))
80 .addRootClasses(root.toArray(new String[0]))
81 .addResources(arr.toArray(new String[0]))
83 .resources(new JarRes())
87 } catch (IOException ex) {
89 } catch (Throwable ex) {
90 throw new IOException("Cannot compile: ", ex);
96 static String compileFromClassPath(URL u, final Res r) throws IOException {
99 f = new File(u.toURI());
100 } catch (URISyntaxException ex) {
101 throw new IOException(ex);
103 for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
104 if (!f.getPath().startsWith(s)) {
107 File root = new File(s);
108 List<String> arr = new ArrayList<>();
109 List<String> classes = new ArrayList<>();
110 listDir(root, null, classes, arr);
111 StringWriter w = new StringWriter();
113 Bck2Brwsr.newCompiler()
114 .addRootClasses(classes.toArray(new String[0]))
115 .addResources(arr.toArray(new String[0]))
117 .resources(new EmulationResources() {
119 public InputStream get(String resource) throws IOException {
121 final URL url = r.get(resource, 0);
122 return url == null ? null : url.openStream();
124 return super.get(resource);
130 } catch (ClassFormatError ex) {
131 throw new IOException(ex);
139 private static void listJAR(
140 JarFile j, List<String> classes,
141 List<String> resources, Set<String> exported, Set<String> keep
142 ) throws IOException {
143 Enumeration<JarEntry> en = j.entries();
144 while (en.hasMoreElements()) {
145 JarEntry e = en.nextElement();
146 final String n = e.getName();
147 if (n.contains("package-info")) {
150 if (n.endsWith("/")) {
153 int last = n.lastIndexOf('/');
154 String pkg = n.substring(0, last + 1);
158 if (n.endsWith(".class")) {
159 classes.add(n.substring(0, n.length() - 6));
162 if (n.startsWith("META-INF/services/") && keep != null) {
163 BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
165 String l = r.readLine();
169 if (l.startsWith("#")) {
172 keep.add(l.replace('.', '/'));
177 String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
178 if (exp != null && exported != null) {
179 for (String def : exp.split(",")) {
180 for (String sep : def.split(";")) {
181 exported.add(sep.replace('.', '/') + "/");
188 private static boolean skipPkg(String pkg) {
189 return pkg.equals("org/apidesign/bck2brwsr/launcher/");
192 private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
193 File[] arr = f.listFiles();
195 if (f.getName().equals("package-info.class")) {
198 if (f.getName().endsWith(".class")) {
199 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
201 resources.add(pref + f.getName());
204 for (File ch : arr) {
206 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
211 static void compileVM(StringBuilder sb, final Res r) throws IOException {
212 URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
213 JarURLConnection juc = (JarURLConnection)u.openConnection();
215 List<String> arr = new ArrayList<>();
216 List<String> classes = new ArrayList<>();
217 listJAR(juc.getJarFile(), classes, arr, null, null);
219 Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
220 .resources(new Bck2Brwsr.Resources() {
222 public InputStream get(String resource) throws IOException {
223 final URL url = r.get(resource, 0);
224 return url == null ? null : url.openStream();
229 static class EmulationResources implements Bck2Brwsr.Resources {
232 public InputStream get(String name) throws IOException {
233 Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
235 while (en.hasMoreElements()) {
236 u = en.nextElement();
239 LOG.log(Level.WARNING, "Cannot find {0}", name);
242 if (u.toExternalForm().contains("/rt.jar!")) {
243 LOG.warning(name + "No bootdelegation for ");
246 return u.openStream();