rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java
branchjdk8
changeset 1678 35daab73e225
parent 1628 c16121d8020b
child 1679 93f4fbc4d1b7
     1.1 --- a/rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java	Mon Jun 09 21:27:49 2014 +0200
     1.2 +++ b/rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java	Sat Sep 13 13:44:01 2014 +0200
     1.3 @@ -25,8 +25,10 @@
     1.4  import java.net.URL;
     1.5  import java.util.ArrayList;
     1.6  import java.util.Enumeration;
     1.7 +import java.util.HashMap;
     1.8  import java.util.HashSet;
     1.9  import java.util.List;
    1.10 +import java.util.Map;
    1.11  import java.util.Set;
    1.12  import java.util.jar.JarEntry;
    1.13  import java.util.jar.JarFile;
    1.14 @@ -65,23 +67,26 @@
    1.15       */
    1.16      public static Bck2Brwsr configureFrom(Bck2Brwsr c, File jar) throws IOException {
    1.17          final JarFile jf = new JarFile(jar);
    1.18 -        List<String> classes = new ArrayList<>();
    1.19 +        final List<String> classes = new ArrayList<>();
    1.20          List<String> resources = new ArrayList<>();
    1.21          Set<String> exported = new HashSet<>();
    1.22 -
    1.23 -        listJAR(jf, classes, resources, exported);
    1.24 -        
    1.25 -        String cp = jf.getManifest().getMainAttributes().getValue("Class-Path"); // NOI18N
    1.26 -        String[] classpath = cp == null ? new String[0] : cp.split(" ");
    1.27 -
    1.28          class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
    1.29 -
    1.30 +            JarRes() {
    1.31 +                super(classes);
    1.32 +            }
    1.33              @Override
    1.34              public InputStream get(String resource) throws IOException {
    1.35                  InputStream is = jf.getInputStream(new ZipEntry(resource));
    1.36                  return is == null ? super.get(resource) : is;
    1.37              }
    1.38          }
    1.39 +        JarRes jarRes = new JarRes();
    1.40 +
    1.41 +        listJAR(jf, jarRes, resources, exported);
    1.42 +        
    1.43 +        String cp = jf.getManifest().getMainAttributes().getValue("Class-Path"); // NOI18N
    1.44 +        String[] classpath = cp == null ? new String[0] : cp.split(" ");
    1.45 +
    1.46          if (c == null) {
    1.47              c = Bck2Brwsr.newCompiler();
    1.48          }
    1.49 @@ -91,11 +96,11 @@
    1.50              .addClasses(classes.toArray(new String[classes.size()]))
    1.51              .addExported(exported.toArray(new String[exported.size()]))
    1.52              .addResources(resources.toArray(new String[resources.size()]))
    1.53 -            .resources(new JarRes());
    1.54 +            .resources(jarRes);
    1.55      }
    1.56      
    1.57      private static void listJAR(
    1.58 -        JarFile j, List<String> classes,
    1.59 +        JarFile j, EmulationResources classes,
    1.60          List<String> resources, Set<String> keep
    1.61      ) throws IOException {
    1.62          Enumeration<JarEntry> en = j.entries();
    1.63 @@ -114,7 +119,7 @@
    1.64                  keep.add(pkg);
    1.65              }
    1.66              if (n.endsWith(".class")) {
    1.67 -                classes.add(n.substring(0, n.length() - 6));
    1.68 +                classes.addClassResource(n);
    1.69              } else {
    1.70                  resources.add(n);
    1.71                  if (n.startsWith("META-INF/services/") && keep != null) {
    1.72 @@ -142,8 +147,51 @@
    1.73              }
    1.74          }
    1.75      }
    1.76 +    
    1.77 +    static byte[] readFrom(InputStream is) throws IOException {
    1.78 +        int expLen = is.available();
    1.79 +        if (expLen < 1) {
    1.80 +            expLen = 1;
    1.81 +        }
    1.82 +        byte[] arr = new byte[expLen];
    1.83 +        int pos = 0;
    1.84 +        for (;;) {
    1.85 +            int read = is.read(arr, pos, arr.length - pos);
    1.86 +            if (read == -1) {
    1.87 +                break;
    1.88 +            }
    1.89 +            pos += read;
    1.90 +            if (pos == arr.length) {
    1.91 +                byte[] tmp = new byte[arr.length * 2];
    1.92 +                System.arraycopy(arr, 0, tmp, 0, arr.length);
    1.93 +                arr = tmp;
    1.94 +            }
    1.95 +        }
    1.96 +        if (pos != arr.length) {
    1.97 +            byte[] tmp = new byte[pos];
    1.98 +            System.arraycopy(arr, 0, tmp, 0, pos);
    1.99 +            arr = tmp;
   1.100 +        }
   1.101 +        return arr;
   1.102 +    }
   1.103 +    
   1.104  
   1.105      static class EmulationResources implements Bck2Brwsr.Resources {
   1.106 +        private final List<String> classes;
   1.107 +        private final Map<String,byte[]> converted = new HashMap<>();
   1.108 +        private final BytecodeProcessor proc;
   1.109 +
   1.110 +        protected EmulationResources(List<String> classes) {
   1.111 +            this.classes = classes;
   1.112 +            BytecodeProcessor p;
   1.113 +            try {
   1.114 +                Class<?> bpClass = Class.forName("org.apidesign.bck2brwsr.aot.RetroLambda");
   1.115 +                p = (BytecodeProcessor) bpClass.newInstance();
   1.116 +            } catch (Throwable t) {
   1.117 +                p = null;
   1.118 +            }
   1.119 +            this.proc = p;
   1.120 +        }
   1.121  
   1.122          @Override
   1.123          public InputStream get(String name) throws IOException {
   1.124 @@ -162,6 +210,23 @@
   1.125              }
   1.126              return u.openStream();
   1.127          }
   1.128 +
   1.129 +        private void addClassResource(String n) throws IOException {
   1.130 +            if (proc != null) {
   1.131 +                try (InputStream is = this.get(n)) {
   1.132 +                    Map<String, byte[]> conv = proc.process(n, readFrom(is), this);
   1.133 +                    if (conv != null) {
   1.134 +                        if (!conv.containsKey(n)) {
   1.135 +                            throw new IOException("Cannot find " + n + " among " + conv);
   1.136 +                        }
   1.137 +                        converted.putAll(conv);
   1.138 +                        return;
   1.139 +                    }
   1.140 +                }
   1.141 +            }
   1.142 +            classes.add(n.substring(0, n.length() - 6));
   1.143 +        }
   1.144      }
   1.145      
   1.146 +    
   1.147  }