rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/RetroLambda.java
branchjdk8
changeset 1678 35daab73e225
child 1684 3238bffeaf12
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/RetroLambda.java	Sat Sep 13 13:44:01 2014 +0200
     1.3 @@ -0,0 +1,121 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.aot;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.io.InputStream;
    1.25 +import java.util.Arrays;
    1.26 +import java.util.HashMap;
    1.27 +import java.util.Map;
    1.28 +import net.orfjackal.retrolambda.LambdaClassBackporter;
    1.29 +import net.orfjackal.retrolambda.LambdaClassDumper;
    1.30 +import net.orfjackal.retrolambda.LambdaClassSaver;
    1.31 +import net.orfjackal.retrolambda.LambdaReifier;
    1.32 +import net.orfjackal.retrolambda.LambdaUsageBackporter;
    1.33 +import net.orfjackal.retrolambda.asm.Opcodes;
    1.34 +import org.apidesign.bck2brwsr.core.ExtraJavaScript;
    1.35 +import org.apidesign.vm4brwsr.Bck2Brwsr;
    1.36 +
    1.37 +/**
    1.38 + *
    1.39 + * @author Jaroslav Tulach
    1.40 + */
    1.41 +@ExtraJavaScript(processByteCode = false, resource="")
    1.42 +final class RetroLambda extends LambdaClassSaver implements BytecodeProcessor {
    1.43 +    private Map<String,byte[]> converted;
    1.44 +    
    1.45 +    public RetroLambda() {
    1.46 +        super(null, Opcodes.V1_7);
    1.47 +    }
    1.48 +
    1.49 +    @Override
    1.50 +    public void saveIfLambda(String className, byte[] bytecode) {
    1.51 +        if (LambdaReifier.isLambdaClassToReify(className)) {
    1.52 +            try {
    1.53 +                System.out.println("Saving lambda class: " + className);
    1.54 +                byte[] backportedBytecode = LambdaClassBackporter.transform(bytecode, Opcodes.V1_7);
    1.55 +                putBytecode(className, backportedBytecode);
    1.56 +            } catch (Throwable t) {
    1.57 +                // print to stdout to keep in sync with other log output
    1.58 +                throw new IllegalStateException("ERROR: Failed to backport lambda class: " + className);
    1.59 +            }
    1.60 +        }
    1.61 +    }
    1.62 +
    1.63 +    private void putBytecode(String className, byte[] backportedBytecode) {
    1.64 +        if (converted == null) {
    1.65 +            converted = new HashMap<>();
    1.66 +        }
    1.67 +        converted.put(className, backportedBytecode);
    1.68 +    }
    1.69 +    
    1.70 +    @Override
    1.71 +    public Map<String, byte[]> process(
    1.72 +        String className, byte[] byteCode, Bck2Brwsr.Resources resources
    1.73 +    ) throws IOException {
    1.74 +        int minor = byteCode[4] << 8 | byteCode[5];
    1.75 +        int major = byteCode[6] << 8 | byteCode[7];
    1.76 +        System.err.println("processing: " + className + " major: "+ major + " minor: " + minor);
    1.77 +        if (major <= 51) {
    1.78 +            return null;
    1.79 +        }
    1.80 +        
    1.81 +        ClassLoader prev = Thread.currentThread().getContextClassLoader();
    1.82 +        try (LambdaClassDumper dumper = new LambdaClassDumper(this)) {
    1.83 +            Thread.currentThread().setContextClassLoader(new ResLdr(resources));
    1.84 +            dumper.install();
    1.85 +            
    1.86 +            byte[] newB = LambdaUsageBackporter.transform(byteCode, Opcodes.V1_7);
    1.87 +            if (!Arrays.equals(newB, byteCode)) {
    1.88 +                putBytecode(className, newB);
    1.89 +            }
    1.90 +        } finally {
    1.91 +            Thread.currentThread().setContextClassLoader(prev);
    1.92 +        }
    1.93 +        
    1.94 +        Map<String, byte[]> ret = converted;
    1.95 +        converted = null;
    1.96 +        return ret;
    1.97 +    }
    1.98 +   
    1.99 +    private static final class ResLdr extends ClassLoader {
   1.100 +        private final Bck2Brwsr.Resources res;
   1.101 +
   1.102 +        public ResLdr(Bck2Brwsr.Resources res) {
   1.103 +            this.res = res;
   1.104 +        }
   1.105 +        
   1.106 +        @Override
   1.107 +        public Class<?> loadClass(String name) throws ClassNotFoundException {
   1.108 +            Class<?> c = findLoadedClass(name);
   1.109 +            if (c != null) {
   1.110 +                return c;
   1.111 +            }
   1.112 +            String r = name.replace('.', '/') + ".class";
   1.113 +            try (InputStream is = res.get(r)) {
   1.114 +                if (is == null) {
   1.115 +                    throw new ClassNotFoundException(name);
   1.116 +                }
   1.117 +                byte[] arr = Bck2BrwsrJars.readFrom(is);
   1.118 +                return defineClass(name, arr, 0, arr.length);
   1.119 +            } catch (IOException e) {
   1.120 +                return super.loadClass(name);
   1.121 +            }
   1.122 +        }
   1.123 +    }    
   1.124 +}