rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 26 Sep 2014 13:42:15 +0200
changeset 1710 6db177c4f72c
parent 1696 ce34fdc36fac
child 1724 50ad005d1597
permissions -rw-r--r--
Need to be able to load classes from classpath to find out if a method overrides something in a superclass.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.aot;
    19 
    20 import java.io.BufferedReader;
    21 import java.io.ByteArrayInputStream;
    22 import java.io.File;
    23 import java.io.FileInputStream;
    24 import java.io.IOException;
    25 import java.io.InputStream;
    26 import java.io.InputStreamReader;
    27 import java.net.URL;
    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;
    33 import java.util.Map;
    34 import java.util.Set;
    35 import java.util.jar.JarEntry;
    36 import java.util.jar.JarFile;
    37 import java.util.logging.Level;
    38 import java.util.logging.Logger;
    39 import java.util.zip.ZipEntry;
    40 import org.apidesign.vm4brwsr.Bck2Brwsr;
    41 
    42 /** Utilities to process JAR files and set a compiler
    43  * up.
    44  *
    45  * @since 0.9
    46  * @author Jaroslav Tulach
    47  */
    48 public final class Bck2BrwsrJars {
    49     private static final Logger LOG = Logger.getLogger(Bck2BrwsrJars.class.getName());
    50 
    51     private Bck2BrwsrJars() {
    52     }
    53     
    54     /** Creates new compiler pre-configured from the content of 
    55      * provided JAR file. The compiler will compile all classes.
    56      * The system understands OSGi manifest entries and will export
    57      * all packages that are exported in the JAR file. The system
    58      * also recognizes META-INF/services and makes sure the class names
    59      * are not mangled.
    60      * 
    61      * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
    62      *    {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
    63      *    {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
    64      *    Can be <code>null</code> - in such case an 
    65      *    {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
    66      * @param jar the file to process
    67      * @return newly configured compiler
    68      * @throws IOException if something goes wrong
    69      */
    70     public static Bck2Brwsr configureFrom(Bck2Brwsr c, File jar) throws IOException {
    71         return configureFrom(c, jar, null);
    72     }
    73     
    74     /** Creates new compiler pre-configured from the content of 
    75      * provided JAR file. The compiler will compile all classes.
    76      * The system understands OSGi manifest entries and will export
    77      * all packages that are exported in the JAR file. The system
    78      * also recognizes META-INF/services and makes sure the class names
    79      * are not mangled.
    80      * 
    81      * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
    82      *    {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
    83      *    {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
    84      *    Can be <code>null</code> - in such case an 
    85      *    {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
    86      * @param jar the file to process
    87      * @param classpath additional resources to make available during
    88      *   compilation, but not include them in the generated JavaScript
    89      * @return newly configured compiler
    90      * @throws IOException if something goes wrong
    91      * @since 0.11
    92      */
    93     public static Bck2Brwsr configureFrom(
    94         Bck2Brwsr c, File jar, final ClassLoader classpath
    95     ) throws IOException {
    96         if (jar.isDirectory()) {
    97             return configureDir(c, jar, classpath);
    98         }
    99         final JarFile jf = new JarFile(jar);
   100         final List<String> classes = new ArrayList<>();
   101         List<String> resources = new ArrayList<>();
   102         Set<String> exported = new HashSet<>();
   103         class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
   104             JarRes() {
   105                 super(classpath, classes);
   106             }
   107             @Override
   108             public InputStream get(String resource) throws IOException {
   109                 InputStream is = getConverted(resource);
   110                 if (is != null) {
   111                     return is;
   112                 }
   113                 is = jf.getInputStream(new ZipEntry(resource));
   114                 return is == null ? super.get(resource) : is;
   115             }
   116         }
   117         JarRes jarRes = new JarRes();
   118 
   119         listJAR(jf, jarRes, resources, exported);
   120         
   121         String cp = jf.getManifest().getMainAttributes().getValue("Class-Path"); // NOI18N
   122         String[] parts = cp == null ? new String[0] : cp.split(" ");
   123 
   124         if (c == null) {
   125             c = Bck2Brwsr.newCompiler();
   126         }
   127         
   128         return c
   129             .library(parts)
   130             .addClasses(classes.toArray(new String[classes.size()]))
   131             .addExported(exported.toArray(new String[exported.size()]))
   132             .addResources(resources.toArray(new String[resources.size()]))
   133             .resources(jarRes);
   134     }
   135     
   136     private static void listJAR(
   137         JarFile j, EmulationResources classes,
   138         List<String> resources, Set<String> keep
   139     ) throws IOException {
   140         Enumeration<JarEntry> en = j.entries();
   141         while (en.hasMoreElements()) {
   142             JarEntry e = en.nextElement();
   143             final String n = e.getName();
   144             if (n.endsWith("/")) {
   145                 continue;
   146             }
   147             if (n.startsWith("META-INF/maven/")) {
   148                 continue;
   149             }
   150             int last = n.lastIndexOf('/');
   151             String pkg = n.substring(0, last + 1);
   152             if (pkg.startsWith("java/")) {
   153                 keep.add(pkg);
   154             }
   155             if (n.endsWith(".class")) {
   156                 classes.addClassResource(n);
   157             } else {
   158                 resources.add(n);
   159                 if (n.startsWith("META-INF/services/") && keep != null) {
   160                     BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
   161                     for (;;) {
   162                         String l = r.readLine();
   163                         if (l == null) {
   164                             break;
   165                         }
   166                         if (l.startsWith("#")) {
   167                             continue;
   168                         }
   169                         keep.add(l.replace('.', '/'));
   170                     }
   171                 }
   172             }
   173         }
   174         String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
   175         if (exp != null && keep != null) {
   176             for (String def : exp.split(",")) {
   177                 for (String sep : def.split(";")) {
   178                     keep.add(sep.replace('.', '/') + "/");
   179                     break;
   180                 }
   181             }
   182         }
   183     }
   184     
   185     static byte[] readFrom(InputStream is) throws IOException {
   186         int expLen = is.available();
   187         if (expLen < 1) {
   188             expLen = 1;
   189         }
   190         byte[] arr = new byte[expLen];
   191         int pos = 0;
   192         for (;;) {
   193             int read = is.read(arr, pos, arr.length - pos);
   194             if (read == -1) {
   195                 break;
   196             }
   197             pos += read;
   198             if (pos == arr.length) {
   199                 byte[] tmp = new byte[arr.length * 2];
   200                 System.arraycopy(arr, 0, tmp, 0, arr.length);
   201                 arr = tmp;
   202             }
   203         }
   204         if (pos != arr.length) {
   205             byte[] tmp = new byte[pos];
   206             System.arraycopy(arr, 0, tmp, 0, pos);
   207             arr = tmp;
   208         }
   209         return arr;
   210     }
   211     
   212 
   213     static class EmulationResources implements Bck2Brwsr.Resources {
   214         private final List<String> classes;
   215         private final Map<String,byte[]> converted = new HashMap<>();
   216         private final BytecodeProcessor proc;
   217         private final ClassLoader cp;
   218 
   219         protected EmulationResources(ClassLoader cp, List<String> classes) {
   220             this.classes = classes;
   221             this.cp = cp != null ? cp : Bck2BrwsrJars.class.getClassLoader();
   222             BytecodeProcessor p;
   223             try {
   224                 Class<?> bpClass = Class.forName("org.apidesign.bck2brwsr.aot.RetroLambda");
   225                 p = (BytecodeProcessor) bpClass.newInstance();
   226             } catch (Throwable t) {
   227                 p = null;
   228             }
   229             this.proc = p;
   230         }
   231 
   232         protected final InputStream getConverted(String name) throws IOException {
   233             byte[] arr = converted.get(name);
   234             if (arr != null) {
   235                 return new ByteArrayInputStream(arr);
   236             }
   237             return null;
   238         }
   239         
   240         @Override
   241         public InputStream get(String name) throws IOException {
   242             InputStream is = getConverted(name);
   243             if (is != null) {
   244                 return is;
   245             }
   246             Enumeration<URL> en = cp.getResources(name);
   247             URL u = null;
   248             while (en.hasMoreElements()) {
   249                 u = en.nextElement();
   250             }
   251             if (u == null) {
   252                 LOG.log(Level.WARNING, "Cannot find {0}", name);
   253                 return null;
   254             }
   255             if (u.toExternalForm().contains("/rt.jar!")) {
   256                 LOG.log(Level.WARNING, "{0}No bootdelegation for ", name);
   257                 return null;
   258             }
   259             return u.openStream();
   260         }
   261 
   262         private void addClassResource(String n) throws IOException {
   263             if (proc != null) {
   264                 try (InputStream is = this.get(n)) {
   265                     Map<String, byte[]> conv = proc.process(n, readFrom(is), this);
   266                     if (conv != null) {
   267                         boolean found = false;
   268                         for (Map.Entry<String, byte[]> entrySet : conv.entrySet()) {
   269                             String res = entrySet.getKey();
   270                             byte[] bytes = entrySet.getValue();
   271                             if (res.equals(n)) {
   272                                 found = true;
   273                             }
   274                             assert res.endsWith(".class") : "Wrong resource: " + res;
   275                             converted.put(res, bytes);
   276                             classes.add(res.substring(0, res.length() - 6));
   277                         }
   278                         if (!found) {
   279                             throw new IOException("Cannot find " + n + " among " + conv);
   280                         }
   281                         return;
   282                     }
   283                 }
   284             }
   285             classes.add(n.substring(0, n.length() - 6));
   286         }
   287     }
   288     
   289     private static Bck2Brwsr configureDir(Bck2Brwsr c, final File dir, ClassLoader cp) throws IOException {
   290         List<String> arr = new ArrayList<>();
   291         List<String> classes = new ArrayList<>();
   292         class DirRes extends EmulationResources {
   293             public DirRes(ClassLoader cp, List<String> classes) {
   294                 super(cp, classes);
   295             }
   296 
   297             @Override
   298             public InputStream get(String name) throws IOException {
   299                 InputStream is = super.get(name);
   300                 if (is != null) {
   301                     return is;
   302                 }
   303                 File r = new File(dir, name.replace('/', File.separatorChar));
   304                 if (r.exists()) {
   305                     return new FileInputStream(r);
   306                 }
   307                 return null;
   308             }
   309         }
   310         DirRes dirRes = new DirRes(cp, classes);
   311         listDir(dir, null, dirRes, arr);
   312         if (c == null) {
   313             c = Bck2Brwsr.newCompiler();
   314         }
   315         return c
   316         .addRootClasses(classes.toArray(new String[0]))
   317         .addResources(arr.toArray(new String[0]))
   318         .library()
   319         //.obfuscation(ObfuscationLevel.FULL)
   320         .resources(dirRes);
   321     }
   322 
   323     private static void listDir(
   324         File f, String pref, EmulationResources res, List<String> resources
   325     ) throws IOException {
   326         File[] arr = f.listFiles();
   327         if (arr == null) {
   328             if (f.getName().endsWith(".class")) {
   329                 res.addClassResource(pref + f.getName());
   330             } else {
   331                 resources.add(pref + f.getName());
   332             }
   333         } else {
   334             for (File ch : arr) {
   335                 listDir(ch, pref == null ? "" : pref + f.getName() + "/", res, resources);
   336             }
   337         }
   338     }
   339     
   340 }