2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 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.aot;
20 import java.io.BufferedReader;
21 import java.io.ByteArrayInputStream;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
28 import java.util.ArrayList;
29 import java.util.Enumeration;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.List;
35 import java.util.jar.Attributes;
36 import java.util.jar.JarEntry;
37 import java.util.jar.JarFile;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
40 import java.util.zip.ZipEntry;
41 import org.apidesign.vm4brwsr.Bck2Brwsr;
43 /** Utilities to process JAR files and set a compiler
47 * @author Jaroslav Tulach
49 public final class Bck2BrwsrJars {
50 private static final Logger LOG = Logger.getLogger(Bck2BrwsrJars.class.getName());
52 private Bck2BrwsrJars() {
55 /** Creates new compiler pre-configured from the content of
56 * provided JAR file. The compiler will compile all classes.
57 * The system understands OSGi manifest entries and NetBeans
58 * module system manifest entries and will export
59 * all packages that are exported in the JAR file. The system
60 * also recognizes META-INF/services and makes sure the class names
63 * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
64 * {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
65 * {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
66 * Can be <code>null</code> - in such case an
67 * {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
68 * @param jar the file to process
69 * @return newly configured compiler
70 * @throws IOException if something goes wrong
72 public static Bck2Brwsr configureFrom(Bck2Brwsr c, File jar) throws IOException {
73 return configureFrom(c, jar, null);
76 /** Creates new compiler pre-configured from the content of
77 * provided JAR file. The compiler will compile all classes.
78 * The system understands OSGi manifest entries and NetBeans
79 * module system manifest entries and will export
80 * all packages that are exported in the JAR file. The system
81 * also recognizes META-INF/services and makes sure the class names
84 * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
85 * {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
86 * {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
87 * Can be <code>null</code> - in such case an
88 * {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
89 * @param jar the file to process
90 * @param classpath additional resources to make available during
91 * compilation, but not include them in the generated JavaScript
92 * @return newly configured compiler
93 * @throws IOException if something goes wrong
96 public static Bck2Brwsr configureFrom(
97 Bck2Brwsr c, File jar, final ClassLoader classpath
98 ) throws IOException {
99 return configureFrom(c, jar, classpath, true);
102 /** Creates new compiler pre-configured from the content of
103 * provided JAR file. The compiler will compile all classes.
104 * The system understands OSGi manifest entries and NetBeans
105 * module system manifest entries and will export
106 * all packages that are exported in the JAR file. The system
107 * also recognizes META-INF/services and makes sure the class names
110 * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
111 * {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
112 * {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
113 * Can be <code>null</code> - in such case an
114 * {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
115 * @param jar the file to process
116 * @param classpath additional resources to make available during
117 * compilation, but not include them in the generated JavaScript
118 * @param ignoreBootClassPath should we ignore classes on bootclasspath?
119 * @return newly configured compiler
120 * @throws IOException if something goes wrong
123 public static Bck2Brwsr configureFrom(
124 Bck2Brwsr c, File jar, final ClassLoader classpath, final boolean ignoreBootClassPath
125 ) throws IOException {
126 if (jar.isDirectory()) {
127 return configureDir(ignoreBootClassPath, c, jar, classpath);
129 final JarFile jf = new JarFile(jar);
130 final List<String> classes = new ArrayList<>();
131 List<String> resources = new ArrayList<>();
132 Set<String> exported = new HashSet<>();
133 class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
135 super(ignoreBootClassPath, classpath, classes);
138 public InputStream get(String resource) throws IOException {
139 InputStream is = getConverted(resource);
143 if (resource.startsWith("/")) {
144 resource = resource.substring(1);
146 is = jf.getInputStream(new ZipEntry(resource));
147 return is == null ? super.get(resource) : is;
150 JarRes jarRes = new JarRes();
152 listJAR(jf, jarRes, resources, exported);
154 String cp = jf.getManifest().getMainAttributes().getValue("Class-Path"); // NOI18N
155 String[] parts = cp == null ? new String[0] : cp.split(" ");
158 c = Bck2Brwsr.newCompiler();
163 .addClasses(classes.toArray(new String[classes.size()]))
164 .addExported(exported.toArray(new String[exported.size()]))
165 .addResources(resources.toArray(new String[resources.size()]))
169 private static void listJAR(
170 JarFile j, EmulationResources classes,
171 List<String> resources, Set<String> keep
172 ) throws IOException {
173 Enumeration<JarEntry> en = j.entries();
174 while (en.hasMoreElements()) {
175 JarEntry e = en.nextElement();
176 final String n = e.getName();
177 if (n.endsWith("/")) {
180 if (n.startsWith("META-INF/maven/")) {
183 int last = n.lastIndexOf('/');
184 String pkg = n.substring(0, last + 1);
185 if (pkg.startsWith("java/")) {
188 if (n.endsWith(".class")) {
189 classes.addClassResource(n);
192 if (n.startsWith("META-INF/services/") && keep != null) {
193 BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
195 String l = r.readLine();
199 if (l.startsWith("#")) {
202 keep.add(l.replace('.', '/'));
209 final Attributes mainAttr = j.getManifest().getMainAttributes();
210 exportPublicPackages(mainAttr, keep);
214 static void exportPublicPackages(final Attributes mainAttr, Set<String> keep) {
215 String exp = mainAttr.getValue("Export-Package"); // NOI18N
217 for (String def : exp.split(",")) {
218 for (String sep : def.split(";")) {
219 keep.add(sep.replace('.', '/') + "/");
225 exp = mainAttr.getValue("OpenIDE-Module-Public-Packages");
227 for (String def : exp.split(",")) {
229 if (def.endsWith(".*")) {
230 keep.add(def.substring(0, def.length() - 1).replace('.', '/'));
236 static byte[] readFrom(InputStream is) throws IOException {
237 int expLen = is.available();
241 byte[] arr = new byte[expLen];
244 int read = is.read(arr, pos, arr.length - pos);
249 if (pos == arr.length) {
250 byte[] tmp = new byte[arr.length * 2];
251 System.arraycopy(arr, 0, tmp, 0, arr.length);
255 if (pos != arr.length) {
256 byte[] tmp = new byte[pos];
257 System.arraycopy(arr, 0, tmp, 0, pos);
264 static class EmulationResources implements Bck2Brwsr.Resources {
265 private final List<String> classes;
266 private final Map<String,byte[]> converted = new HashMap<>();
267 private final BytecodeProcessor proc;
268 private final ClassLoader cp;
269 private final boolean ignoreBootClassPath;
271 protected EmulationResources(boolean ignoreBootClassPath, ClassLoader cp, List<String> classes) {
272 this.ignoreBootClassPath = ignoreBootClassPath;
273 this.classes = classes;
274 this.cp = cp != null ? cp : Bck2BrwsrJars.class.getClassLoader();
277 Class<?> bpClass = Class.forName("org.apidesign.bck2brwsr.aot.RetroLambda");
278 p = (BytecodeProcessor) bpClass.newInstance();
279 } catch (Throwable t) {
285 protected final InputStream getConverted(String name) throws IOException {
286 byte[] arr = converted.get(name);
288 return new ByteArrayInputStream(arr);
294 public InputStream get(String name) throws IOException {
295 InputStream is = getConverted(name);
299 Enumeration<URL> en = cp.getResources(name);
301 while (en.hasMoreElements()) {
302 u = en.nextElement();
305 LOG.log(Level.FINE, "Cannot find {0}", name);
308 if (ignoreBootClassPath && u.toExternalForm().contains("/rt.jar!")) {
309 LOG.log(Level.WARNING, "No bootdelegation for {0}", name);
312 return u.openStream();
315 private void addClassResource(String n) throws IOException {
317 try (InputStream is = this.get(n)) {
318 Map<String, byte[]> conv = proc.process(n, readFrom(is), this);
320 boolean found = false;
321 for (Map.Entry<String, byte[]> entrySet : conv.entrySet()) {
322 String res = entrySet.getKey();
323 byte[] bytes = entrySet.getValue();
327 assert res.endsWith(".class") : "Wrong resource: " + res;
328 converted.put(res, bytes);
329 classes.add(res.substring(0, res.length() - 6));
332 throw new IOException("Cannot find " + n + " among " + conv);
338 classes.add(n.substring(0, n.length() - 6));
342 private static Bck2Brwsr configureDir(final boolean ignoreBootClassPath, Bck2Brwsr c, final File dir, ClassLoader cp) throws IOException {
343 List<String> arr = new ArrayList<>();
344 List<String> classes = new ArrayList<>();
345 class DirRes extends EmulationResources {
346 public DirRes(ClassLoader cp, List<String> classes) {
347 super(ignoreBootClassPath, cp, classes);
351 public InputStream get(String name) throws IOException {
352 InputStream is = super.get(name);
356 File r = new File(dir, name.replace('/', File.separatorChar));
358 return new FileInputStream(r);
363 DirRes dirRes = new DirRes(cp, classes);
364 listDir(dir, null, dirRes, arr);
366 c = Bck2Brwsr.newCompiler();
369 .addRootClasses(classes.toArray(new String[0]))
370 .addResources(arr.toArray(new String[0]))
372 //.obfuscation(ObfuscationLevel.FULL)
376 private static void listDir(
377 File f, String pref, EmulationResources res, List<String> resources
378 ) throws IOException {
379 File[] arr = f.listFiles();
381 if (f.getName().endsWith(".class")) {
382 res.addClassResource(pref + f.getName());
384 resources.add(pref + f.getName());
387 for (File ch : arr) {
388 listDir(ch, pref == null ? "" : pref + f.getName() + "/", res, resources);