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.jar.Manifest;
39 import java.util.logging.Level;
40 import java.util.logging.Logger;
41 import java.util.zip.ZipEntry;
42 import org.apidesign.vm4brwsr.Bck2Brwsr;
44 /** Utilities to process JAR files and set a compiler
48 * @author Jaroslav Tulach
50 public final class Bck2BrwsrJars {
51 private static final Logger LOG = Logger.getLogger(Bck2BrwsrJars.class.getName());
53 private Bck2BrwsrJars() {
56 /** Creates new compiler pre-configured from the content of
57 * provided JAR file. The compiler will compile all classes.
58 * The system understands OSGi manifest entries and NetBeans
59 * module system manifest entries and will export
60 * all packages that are exported in the JAR file. The system
61 * also recognizes META-INF/services and makes sure the class names
64 * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
65 * {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
66 * {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
67 * Can be <code>null</code> - in such case an
68 * {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
69 * @param jar the file to process
70 * @return newly configured compiler
71 * @throws IOException if something goes wrong
73 public static Bck2Brwsr configureFrom(Bck2Brwsr c, File jar) throws IOException {
74 return configureFrom(c, jar, null);
77 /** Creates new compiler pre-configured from the content of
78 * provided JAR file. The compiler will compile all classes.
79 * The system understands OSGi manifest entries and NetBeans
80 * module system manifest entries and will export
81 * all packages that are exported in the JAR file. The system
82 * also recognizes META-INF/services and makes sure the class names
85 * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
86 * {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
87 * {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
88 * Can be <code>null</code> - in such case an
89 * {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
90 * @param jar the file to process
91 * @param classpath additional resources to make available during
92 * compilation, but not include them in the generated JavaScript
93 * @return newly configured compiler
94 * @throws IOException if something goes wrong
97 public static Bck2Brwsr configureFrom(
98 Bck2Brwsr c, File jar, final ClassLoader classpath
99 ) throws IOException {
100 return configureFrom(c, jar, classpath, true);
103 /** Creates new compiler pre-configured from the content of
104 * provided JAR file. The compiler will compile all classes.
105 * The system understands OSGi manifest entries and NetBeans
106 * module system manifest entries and will export
107 * all packages that are exported in the JAR file. The system
108 * also recognizes META-INF/services and makes sure the class names
111 * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
112 * {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
113 * {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
114 * Can be <code>null</code> - in such case an
115 * {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
116 * @param jar the file to process
117 * @param classpath additional resources to make available during
118 * compilation, but not include them in the generated JavaScript
119 * @param ignoreBootClassPath should we ignore classes on bootclasspath?
120 * @return newly configured compiler
121 * @throws IOException if something goes wrong
124 public static Bck2Brwsr configureFrom(
125 Bck2Brwsr c, File jar, final ClassLoader classpath, final boolean ignoreBootClassPath
126 ) throws IOException {
127 if (jar.isDirectory()) {
128 return configureDir(ignoreBootClassPath, c, jar, classpath);
130 final JarFile jf = new JarFile(jar);
131 final List<String> classes = new ArrayList<>();
132 List<String> resources = new ArrayList<>();
133 Set<String> exported = new HashSet<>();
134 class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
136 super(ignoreBootClassPath, classpath, classes);
139 public InputStream get(String resource) throws IOException {
140 InputStream is = getConverted(resource);
144 if (resource.startsWith("/")) {
145 resource = resource.substring(1);
147 ZipEntry ze = jf.getEntry(resource);
149 is = jf.getInputStream(ze);
151 return is == null ? super.get(resource) : is;
154 JarRes jarRes = new JarRes();
156 listJAR(jf, jarRes, resources, exported);
157 final Manifest manifest = jf.getManifest();
158 final Attributes mainAttributes = manifest == null ? null : manifest.getMainAttributes();
159 String cp = mainAttributes == null ? null : mainAttributes.getValue("Class-Path"); // NOI18N
160 String[] parts = cp == null ? new String[0] : cp.split(" ");
163 c = Bck2Brwsr.newCompiler();
168 .addClasses(classes.toArray(new String[classes.size()]))
169 .addExported(exported.toArray(new String[exported.size()]))
170 .addResources(resources.toArray(new String[resources.size()]))
174 private static void listJAR(
175 JarFile j, EmulationResources classes,
176 List<String> resources, Set<String> keep
177 ) throws IOException {
178 Enumeration<JarEntry> en = j.entries();
179 while (en.hasMoreElements()) {
180 JarEntry e = en.nextElement();
181 final String n = e.getName();
182 if (n.endsWith("/")) {
185 if (n.startsWith("META-INF/maven/")) {
188 int last = n.lastIndexOf('/');
189 String pkg = n.substring(0, last + 1);
190 if (pkg.startsWith("java/")) {
193 if (n.endsWith(".class")) {
194 classes.addClassResource(n);
197 if (n.startsWith("META-INF/services/") && keep != null) {
198 BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
200 String l = r.readLine();
204 if (l.startsWith("#")) {
207 keep.add(l.replace('.', '/'));
214 final Manifest manifest = j.getManifest();
215 if (manifest != null) {
216 final Attributes mainAttr = manifest.getMainAttributes();
217 if (mainAttr != null) {
218 exportPublicPackages(mainAttr, keep);
224 static void exportPublicPackages(final Attributes mainAttr, Set<String> keep) {
225 String exp = mainAttr.getValue("Export-Package"); // NOI18N
227 for (String def : exp.split(",")) {
228 for (String sep : def.split(";")) {
229 keep.add(sep.replace('.', '/') + "/");
235 exp = mainAttr.getValue("OpenIDE-Module-Public-Packages");
237 for (String def : exp.split(",")) {
239 if (def.endsWith(".*")) {
240 keep.add(def.substring(0, def.length() - 1).replace('.', '/'));
246 static byte[] readFrom(InputStream is) throws IOException {
247 int expLen = is.available();
251 byte[] arr = new byte[expLen];
254 int read = is.read(arr, pos, arr.length - pos);
259 if (pos == arr.length) {
260 byte[] tmp = new byte[arr.length * 2];
261 System.arraycopy(arr, 0, tmp, 0, arr.length);
265 if (pos != arr.length) {
266 byte[] tmp = new byte[pos];
267 System.arraycopy(arr, 0, tmp, 0, pos);
274 static class EmulationResources implements Bck2Brwsr.Resources {
275 private final List<String> classes;
276 private final Map<String,byte[]> converted = new HashMap<>();
277 private final BytecodeProcessor proc;
278 private final ClassLoader cp;
279 private final boolean ignoreBootClassPath;
281 protected EmulationResources(boolean ignoreBootClassPath, ClassLoader cp, List<String> classes) {
282 this.ignoreBootClassPath = ignoreBootClassPath;
283 this.classes = classes;
284 this.cp = cp != null ? cp : Bck2BrwsrJars.class.getClassLoader();
287 Class<?> bpClass = Class.forName("org.apidesign.bck2brwsr.aot.RetroLambda");
288 p = (BytecodeProcessor) bpClass.newInstance();
289 } catch (Throwable t) {
295 protected final InputStream getConverted(String name) throws IOException {
296 byte[] arr = converted.get(name);
298 return new ByteArrayInputStream(arr);
304 public InputStream get(String name) throws IOException {
305 InputStream is = getConverted(name);
309 return getFromCp(name);
312 private InputStream getFromCp(String name) throws IOException {
313 Enumeration<URL> en = cp.getResources(name);
315 while (en.hasMoreElements()) {
316 u = en.nextElement();
319 LOG.log(Level.FINE, "Cannot find {0}", name);
322 if (ignoreBootClassPath && u.toExternalForm().contains("/rt.jar!")) {
323 LOG.log(Level.WARNING, "No bootdelegation for {0}", name);
326 return u.openStream();
329 private final class NoConvRes implements Bck2Brwsr.Resources {
331 public InputStream get(String resource) throws IOException {
332 return getFromCp(resource);
336 private void addClassResource(String n) throws IOException {
338 try (InputStream is = this.get(n)) {
339 Map<String, byte[]> conv = proc.process(n, readFrom(is), new NoConvRes());
341 boolean found = false;
342 for (Map.Entry<String, byte[]> entrySet : conv.entrySet()) {
343 String res = entrySet.getKey();
344 byte[] bytes = entrySet.getValue();
348 assert res.endsWith(".class") : "Wrong resource: " + res;
349 converted.put(res, bytes);
350 classes.add(res.substring(0, res.length() - 6));
353 throw new IOException("Cannot find " + n + " among " + conv);
359 classes.add(n.substring(0, n.length() - 6));
363 private static Bck2Brwsr configureDir(final boolean ignoreBootClassPath, Bck2Brwsr c, final File dir, ClassLoader cp) throws IOException {
364 List<String> arr = new ArrayList<>();
365 List<String> classes = new ArrayList<>();
366 class DirRes extends EmulationResources {
367 public DirRes(ClassLoader cp, List<String> classes) {
368 super(ignoreBootClassPath, cp, classes);
372 public InputStream get(String name) throws IOException {
373 InputStream is = super.get(name);
377 File r = new File(dir, name.replace('/', File.separatorChar));
379 return new FileInputStream(r);
384 DirRes dirRes = new DirRes(cp, classes);
385 listDir(dir, null, dirRes, arr);
387 c = Bck2Brwsr.newCompiler();
390 .addRootClasses(classes.toArray(new String[0]))
391 .addResources(arr.toArray(new String[0]))
393 //.obfuscation(ObfuscationLevel.FULL)
397 private static void listDir(
398 File f, String pref, EmulationResources res, List<String> resources
399 ) throws IOException {
400 File[] arr = f.listFiles();
402 if (f.getName().endsWith(".class")) {
403 res.addClassResource(pref + f.getName());
405 resources.add(pref + f.getName());
408 for (File ch : arr) {
409 listDir(ch, pref == null ? "" : pref + f.getName() + "/", res, resources);