boot/src/main/java/org/netbeans/html/boot/impl/FnUtils.java
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 80702021b851
child 365 5c93ad8c7a15
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/boot/src/main/java/org/netbeans/html/boot/impl/FnUtils.java	Mon Dec 16 16:59:43 2013 +0100
     1.3 @@ -0,0 +1,597 @@
     1.4 +/**
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     1.8 + *
     1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    1.10 + * Other names may be trademarks of their respective owners.
    1.11 + *
    1.12 + * The contents of this file are subject to the terms of either the GNU
    1.13 + * General Public License Version 2 only ("GPL") or the Common
    1.14 + * Development and Distribution License("CDDL") (collectively, the
    1.15 + * "License"). You may not use this file except in compliance with the
    1.16 + * License. You can obtain a copy of the License at
    1.17 + * http://www.netbeans.org/cddl-gplv2.html
    1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.19 + * specific language governing permissions and limitations under the
    1.20 + * License.  When distributing the software, include this License Header
    1.21 + * Notice in each file and include the License file at
    1.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    1.23 + * particular file as subject to the "Classpath" exception as provided
    1.24 + * by Oracle in the GPL Version 2 section of the License file that
    1.25 + * accompanied this code. If applicable, add the following below the
    1.26 + * License Header, with the fields enclosed by brackets [] replaced by
    1.27 + * your own identifying information:
    1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.29 + *
    1.30 + * Contributor(s):
    1.31 + *
    1.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    1.33 + * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
    1.34 + *
    1.35 + * If you wish your version of this file to be governed by only the CDDL
    1.36 + * or only the GPL Version 2, indicate your decision by adding
    1.37 + * "[Contributor] elects to include this software in this distribution
    1.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.39 + * single choice of license, a recipient has the option to distribute
    1.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    1.41 + * to extend the choice of license to its licensees as provided above.
    1.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.43 + * Version 2 license, then the option applies only if the new code is
    1.44 + * made subject to such option by the copyright holder.
    1.45 + */
    1.46 +package org.netbeans.html.boot.impl;
    1.47 +
    1.48 +import java.io.Closeable;
    1.49 +import java.io.InputStream;
    1.50 +import java.io.InputStreamReader;
    1.51 +import java.io.Reader;
    1.52 +import java.net.URL;
    1.53 +import java.util.ArrayList;
    1.54 +import java.util.Collections;
    1.55 +import java.util.Enumeration;
    1.56 +import java.util.List;
    1.57 +import java.util.concurrent.Callable;
    1.58 +import org.apidesign.html.boot.spi.Fn;
    1.59 +import org.objectweb.asm.AnnotationVisitor;
    1.60 +import org.objectweb.asm.ClassReader;
    1.61 +import org.objectweb.asm.ClassVisitor;
    1.62 +import org.objectweb.asm.ClassWriter;
    1.63 +import org.objectweb.asm.Label;
    1.64 +import org.objectweb.asm.MethodVisitor;
    1.65 +import org.objectweb.asm.Opcodes;
    1.66 +import org.objectweb.asm.Type;
    1.67 +import org.objectweb.asm.signature.SignatureReader;
    1.68 +import org.objectweb.asm.signature.SignatureVisitor;
    1.69 +import org.objectweb.asm.signature.SignatureWriter;
    1.70 +
    1.71 +/**
    1.72 + *
    1.73 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.74 + */
    1.75 +public final class FnUtils implements Fn.Presenter {
    1.76 +    
    1.77 +    private FnUtils() {
    1.78 +    }
    1.79 +    
    1.80 +    public static boolean isJavaScriptCapable(ClassLoader l) {
    1.81 +        if (l instanceof JsClassLoader) {
    1.82 +            return true;
    1.83 +        }
    1.84 +        Class<?> clazz;
    1.85 +        try (Closeable c = Fn.activate(new FnUtils())) {
    1.86 +            clazz = Class.forName(Test.class.getName(), true, l);
    1.87 +            final Object is = ((Callable<?>)clazz.newInstance()).call();
    1.88 +            return Boolean.TRUE.equals(is);
    1.89 +        } catch (Exception ex) {
    1.90 +            return false;
    1.91 +        }
    1.92 +    }
    1.93 +    
    1.94 +    public static boolean isValid(Fn fn) {
    1.95 +        return fn != null && fn.isValid();
    1.96 +    }
    1.97 +
    1.98 +    public static ClassLoader newLoader(final FindResources f, final Fn.Presenter d, ClassLoader parent) {
    1.99 +        return new JsClassLoader(parent) {
   1.100 +            @Override
   1.101 +            protected URL findResource(String name) {
   1.102 +                List<URL> l = res(name, true);
   1.103 +                return l.isEmpty() ? null : l.get(0);
   1.104 +            }
   1.105 +            
   1.106 +            @Override
   1.107 +            protected Enumeration<URL> findResources(String name) {
   1.108 +                return Collections.enumeration(res(name, false));
   1.109 +            }
   1.110 +            
   1.111 +            private List<URL> res(String name, boolean oneIsEnough) {
   1.112 +                List<URL> l = new ArrayList<URL>();
   1.113 +                f.findResources(name, l, oneIsEnough);
   1.114 +                return l;
   1.115 +            }
   1.116 +            
   1.117 +            @Override
   1.118 +            protected Fn defineFn(String code, String... names) {
   1.119 +                return d.defineFn(code, names);
   1.120 +            }
   1.121 +
   1.122 +            @Override
   1.123 +            protected void loadScript(Reader code) throws Exception {
   1.124 +                d.loadScript(code);
   1.125 +            }
   1.126 +        };
   1.127 +    }
   1.128 +
   1.129 +    static String callback(final String body) {
   1.130 +        return new JsCallback() {
   1.131 +            @Override
   1.132 +            protected CharSequence callMethod(
   1.133 +                String ident, String fqn, String method, String params
   1.134 +            ) {
   1.135 +                StringBuilder sb = new StringBuilder();
   1.136 +                sb.append("vm.").append(mangle(fqn, method, params));
   1.137 +                sb.append("(");
   1.138 +                if (ident != null) {
   1.139 +                    sb.append(ident);
   1.140 +                }
   1.141 +                return sb;
   1.142 +            }
   1.143 +
   1.144 +        }.parse(body);
   1.145 +    }
   1.146 +
   1.147 +    static void loadScript(ClassLoader jcl, String resource) {
   1.148 +        final InputStream script = jcl.getResourceAsStream(resource);
   1.149 +        if (script == null) {
   1.150 +            throw new NullPointerException("Can't find " + resource);
   1.151 +        }
   1.152 +        try {
   1.153 +            Reader isr = null;
   1.154 +            try {
   1.155 +                isr = new InputStreamReader(script, "UTF-8");
   1.156 +                FnContext.currentPresenter().loadScript(isr);
   1.157 +            } finally {
   1.158 +                if (isr != null) {
   1.159 +                    isr.close();
   1.160 +                }
   1.161 +            }
   1.162 +        } catch (Exception ex) {
   1.163 +            throw new IllegalStateException("Can't execute " + resource, ex);
   1.164 +        } 
   1.165 +    }
   1.166 +
   1.167 +    @Override
   1.168 +    public Fn defineFn(String code, String... names) {
   1.169 +        return new TrueFn();
   1.170 +    }
   1.171 +
   1.172 +    @Override
   1.173 +    public void displayPage(URL page, Runnable onPageLoad) {
   1.174 +    }
   1.175 +
   1.176 +    @Override
   1.177 +    public void loadScript(Reader code) throws Exception {
   1.178 +    }
   1.179 +    
   1.180 +    private static final class FindInClass extends ClassVisitor {
   1.181 +        private String name;
   1.182 +        private int found;
   1.183 +        private ClassLoader loader;
   1.184 +        private String resource;
   1.185 +
   1.186 +        public FindInClass(ClassLoader l, ClassVisitor cv) {
   1.187 +            super(Opcodes.ASM4, cv);
   1.188 +            this.loader = l;
   1.189 +        }
   1.190 +
   1.191 +        @Override
   1.192 +        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
   1.193 +            this.name = name;
   1.194 +            super.visit(version, access, name, signature, superName, interfaces);
   1.195 +        }
   1.196 +
   1.197 +        @Override
   1.198 +        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
   1.199 +            if ("Lnet/java/html/js/JavaScriptResource;".equals(desc)) {
   1.200 +                return new LoadResource();
   1.201 +            }
   1.202 +            return super.visitAnnotation(desc, visible);
   1.203 +        }
   1.204 +
   1.205 +        @Override
   1.206 +        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
   1.207 +            return new FindInMethod(access, name, desc,
   1.208 +                    super.visitMethod(access & (~Opcodes.ACC_NATIVE), name, desc, signature, exceptions)
   1.209 +            );
   1.210 +        }
   1.211 +
   1.212 +        private final class FindInMethod extends MethodVisitor {
   1.213 +
   1.214 +            private final String name;
   1.215 +            private final String desc;
   1.216 +            private final int access;
   1.217 +            private List<String> args;
   1.218 +            private String body;
   1.219 +            private boolean bodyGenerated;
   1.220 +
   1.221 +            public FindInMethod(int access, String name, String desc, MethodVisitor mv) {
   1.222 +                super(Opcodes.ASM4, mv);
   1.223 +                this.access = access;
   1.224 +                this.name = name;
   1.225 +                this.desc = desc;
   1.226 +            }
   1.227 +
   1.228 +            @Override
   1.229 +            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
   1.230 +                if ("Lnet/java/html/js/JavaScriptBody;".equals(desc) // NOI18N
   1.231 +                        || "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;".equals(desc) // NOI18N
   1.232 +                        ) {
   1.233 +                    found++;
   1.234 +                    return new FindInAnno();
   1.235 +                }
   1.236 +                return super.visitAnnotation(desc, visible);
   1.237 +            }
   1.238 +
   1.239 +            private void generateJSBody(List<String> args, String body) {
   1.240 +                this.args = args;
   1.241 +                this.body = body;
   1.242 +            }
   1.243 +
   1.244 +            @Override
   1.245 +            public void visitCode() {
   1.246 +                if (body == null) {
   1.247 +                    return;
   1.248 +                }
   1.249 +                generateBody();
   1.250 +            }
   1.251 +
   1.252 +            private boolean generateBody() {
   1.253 +                if (bodyGenerated) {
   1.254 +                    return false;
   1.255 +                }
   1.256 +                bodyGenerated = true;
   1.257 +
   1.258 +                super.visitFieldInsn(
   1.259 +                        Opcodes.GETSTATIC, FindInClass.this.name,
   1.260 +                        "$$fn$$" + name + "_" + found,
   1.261 +                        "Lorg/apidesign/html/boot/spi/Fn;"
   1.262 +                );
   1.263 +                super.visitInsn(Opcodes.DUP);
   1.264 +                super.visitMethodInsn(
   1.265 +                        Opcodes.INVOKESTATIC,
   1.266 +                        "org/apidesign/html/boot/spi/Fn", "isValid",
   1.267 +                        "(Lorg/apidesign/html/boot/spi/Fn;)Z"
   1.268 +                );
   1.269 +                Label ifNotNull = new Label();
   1.270 +                super.visitJumpInsn(Opcodes.IFNE, ifNotNull);
   1.271 +
   1.272 +                // init Fn
   1.273 +                super.visitInsn(Opcodes.POP);
   1.274 +                super.visitLdcInsn(Type.getObjectType(FindInClass.this.name));
   1.275 +                super.visitLdcInsn(body);
   1.276 +                super.visitIntInsn(Opcodes.SIPUSH, args.size());
   1.277 +                super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
   1.278 +                boolean needsVM = false;
   1.279 +                for (int i = 0; i < args.size(); i++) {
   1.280 +                    assert !needsVM;
   1.281 +                    String argName = args.get(i);
   1.282 +                    needsVM = "vm".equals(argName);
   1.283 +                    super.visitInsn(Opcodes.DUP);
   1.284 +                    super.visitIntInsn(Opcodes.BIPUSH, i);
   1.285 +                    super.visitLdcInsn(argName);
   1.286 +                    super.visitInsn(Opcodes.AASTORE);
   1.287 +                }
   1.288 +                super.visitMethodInsn(Opcodes.INVOKESTATIC,
   1.289 +                        "org/apidesign/html/boot/spi/Fn", "define",
   1.290 +                        "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/String;)Lorg/apidesign/html/boot/spi/Fn;"
   1.291 +                );
   1.292 +                if (resource != null) {
   1.293 +                    super.visitLdcInsn(Type.getObjectType(FindInClass.this.name));
   1.294 +                    super.visitLdcInsn(resource);
   1.295 +                    super.visitMethodInsn(Opcodes.INVOKESTATIC,
   1.296 +                            "org/apidesign/html/boot/spi/Fn", "preload",
   1.297 +                            "(Lorg/apidesign/html/boot/spi/Fn;Ljava/lang/Class;Ljava/lang/String;)Lorg/apidesign/html/boot/spi/Fn;"
   1.298 +                    );
   1.299 +                }
   1.300 +                super.visitInsn(Opcodes.DUP);
   1.301 +                super.visitFieldInsn(
   1.302 +                        Opcodes.PUTSTATIC, FindInClass.this.name,
   1.303 +                        "$$fn$$" + name + "_" + found,
   1.304 +                        "Lorg/apidesign/html/boot/spi/Fn;"
   1.305 +                );
   1.306 +                // end of Fn init
   1.307 +
   1.308 +                super.visitLabel(ifNotNull);
   1.309 +
   1.310 +                final int offset;
   1.311 +                if ((access & Opcodes.ACC_STATIC) == 0) {
   1.312 +                    offset = 1;
   1.313 +                    super.visitIntInsn(Opcodes.ALOAD, 0);
   1.314 +                } else {
   1.315 +                    offset = 0;
   1.316 +                    super.visitInsn(Opcodes.ACONST_NULL);
   1.317 +                }
   1.318 +
   1.319 +                super.visitIntInsn(Opcodes.SIPUSH, args.size());
   1.320 +                super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
   1.321 +
   1.322 +                class SV extends SignatureVisitor {
   1.323 +
   1.324 +                    private boolean nowReturn;
   1.325 +                    private Type returnType;
   1.326 +                    private int index;
   1.327 +                    private int loadIndex = offset;
   1.328 +
   1.329 +                    public SV() {
   1.330 +                        super(Opcodes.ASM4);
   1.331 +                    }
   1.332 +
   1.333 +                    @Override
   1.334 +                    public void visitBaseType(char descriptor) {
   1.335 +                        final Type t = Type.getType("" + descriptor);
   1.336 +                        if (nowReturn) {
   1.337 +                            returnType = t;
   1.338 +                            return;
   1.339 +                        }
   1.340 +                        FindInMethod.super.visitInsn(Opcodes.DUP);
   1.341 +                        FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index++);
   1.342 +                        FindInMethod.super.visitVarInsn(t.getOpcode(Opcodes.ILOAD), loadIndex++);
   1.343 +                        String factory;
   1.344 +                        switch (descriptor) {
   1.345 +                            case 'I':
   1.346 +                                factory = "java/lang/Integer";
   1.347 +                                break;
   1.348 +                            case 'J':
   1.349 +                                factory = "java/lang/Long";
   1.350 +                                loadIndex++;
   1.351 +                                break;
   1.352 +                            case 'S':
   1.353 +                                factory = "java/lang/Short";
   1.354 +                                break;
   1.355 +                            case 'F':
   1.356 +                                factory = "java/lang/Float";
   1.357 +                                break;
   1.358 +                            case 'D':
   1.359 +                                factory = "java/lang/Double";
   1.360 +                                loadIndex++;
   1.361 +                                break;
   1.362 +                            case 'Z':
   1.363 +                                factory = "java/lang/Boolean";
   1.364 +                                break;
   1.365 +                            case 'C':
   1.366 +                                factory = "java/lang/Character";
   1.367 +                                break;
   1.368 +                            case 'B':
   1.369 +                                factory = "java/lang/Byte";
   1.370 +                                break;
   1.371 +                            default:
   1.372 +                                throw new IllegalStateException(t.toString());
   1.373 +                        }
   1.374 +                        FindInMethod.super.visitMethodInsn(Opcodes.INVOKESTATIC,
   1.375 +                                factory, "valueOf", "(" + descriptor + ")L" + factory + ";"
   1.376 +                        );
   1.377 +                        FindInMethod.super.visitInsn(Opcodes.AASTORE);
   1.378 +                    }
   1.379 +
   1.380 +                    @Override
   1.381 +                    public SignatureVisitor visitArrayType() {
   1.382 +                        if (nowReturn) {
   1.383 +                            throw new IllegalStateException("Not supported yet");
   1.384 +                        }
   1.385 +                        loadObject();
   1.386 +                        return new SignatureWriter();
   1.387 +                    }
   1.388 +
   1.389 +                    @Override
   1.390 +                    public void visitClassType(String name) {
   1.391 +                        if (nowReturn) {
   1.392 +                            returnType = Type.getObjectType(name);
   1.393 +                            return;
   1.394 +                        }
   1.395 +                        loadObject();
   1.396 +                    }
   1.397 +
   1.398 +                    @Override
   1.399 +                    public SignatureVisitor visitReturnType() {
   1.400 +                        nowReturn = true;
   1.401 +                        return this;
   1.402 +                    }
   1.403 +
   1.404 +                    private void loadObject() {
   1.405 +                        FindInMethod.super.visitInsn(Opcodes.DUP);
   1.406 +                        FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index++);
   1.407 +                        FindInMethod.super.visitVarInsn(Opcodes.ALOAD, loadIndex++);
   1.408 +                        FindInMethod.super.visitInsn(Opcodes.AASTORE);
   1.409 +                    }
   1.410 +
   1.411 +                }
   1.412 +                SV sv = new SV();
   1.413 +                SignatureReader sr = new SignatureReader(desc);
   1.414 +                sr.accept(sv);
   1.415 +
   1.416 +                if (needsVM) {
   1.417 +                    FindInMethod.super.visitInsn(Opcodes.DUP);
   1.418 +                    FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, sv.index);
   1.419 +                    int lastSlash = FindInClass.this.name.lastIndexOf('/');
   1.420 +                    String jsCallbacks = FindInClass.this.name.substring(0, lastSlash + 1) + "$JsCallbacks$";
   1.421 +                    FindInMethod.super.visitFieldInsn(Opcodes.GETSTATIC, jsCallbacks, "VM", "L" + jsCallbacks + ";");
   1.422 +                    FindInMethod.super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, jsCallbacks, "current", "()L" + jsCallbacks + ";");
   1.423 +                    FindInMethod.super.visitInsn(Opcodes.AASTORE);
   1.424 +                }
   1.425 +
   1.426 +                super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
   1.427 +                        "org/apidesign/html/boot/spi/Fn", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"
   1.428 +                );
   1.429 +                switch (sv.returnType.getSort()) {
   1.430 +                    case Type.VOID:
   1.431 +                        super.visitInsn(Opcodes.RETURN);
   1.432 +                        break;
   1.433 +                    case Type.ARRAY:
   1.434 +                    case Type.OBJECT:
   1.435 +                        super.visitTypeInsn(Opcodes.CHECKCAST, sv.returnType.getInternalName());
   1.436 +                        super.visitInsn(Opcodes.ARETURN);
   1.437 +                        break;
   1.438 +                    case Type.BOOLEAN:
   1.439 +                        super.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Boolean");
   1.440 +                        super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
   1.441 +                                "java/lang/Boolean", "booleanValue", "()Z"
   1.442 +                        );
   1.443 +                        super.visitInsn(Opcodes.IRETURN);
   1.444 +                        break;
   1.445 +                    default:
   1.446 +                        super.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Number");
   1.447 +                        super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
   1.448 +                                "java/lang/Number", sv.returnType.getClassName() + "Value", "()" + sv.returnType.getDescriptor()
   1.449 +                        );
   1.450 +                        super.visitInsn(sv.returnType.getOpcode(Opcodes.IRETURN));
   1.451 +                }
   1.452 +                return true;
   1.453 +            }
   1.454 +
   1.455 +            @Override
   1.456 +            public void visitEnd() {
   1.457 +                super.visitEnd();
   1.458 +                if (body != null) {
   1.459 +                    if (generateBody()) {
   1.460 +                        // native method
   1.461 +                        super.visitMaxs(1, 0);
   1.462 +                    }
   1.463 +                    FindInClass.this.visitField(
   1.464 +                            Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
   1.465 +                            "$$fn$$" + name + "_" + found,
   1.466 +                            "Lorg/apidesign/html/boot/spi/Fn;",
   1.467 +                            null, null
   1.468 +                    );
   1.469 +                }
   1.470 +            }
   1.471 +
   1.472 +            private final class FindInAnno extends AnnotationVisitor {
   1.473 +
   1.474 +                private List<String> args = new ArrayList<String>();
   1.475 +                private String body;
   1.476 +                private boolean javacall = false;
   1.477 +
   1.478 +                public FindInAnno() {
   1.479 +                    super(Opcodes.ASM4);
   1.480 +                }
   1.481 +
   1.482 +                @Override
   1.483 +                public void visit(String name, Object value) {
   1.484 +                    if (name == null) {
   1.485 +                        args.add((String) value);
   1.486 +                        return;
   1.487 +                    }
   1.488 +                    if (name.equals("javacall")) { // NOI18N
   1.489 +                        javacall = (Boolean) value;
   1.490 +                        return;
   1.491 +                    }
   1.492 +                    assert name.equals("body");
   1.493 +                    body = (String) value;
   1.494 +                }
   1.495 +
   1.496 +                @Override
   1.497 +                public AnnotationVisitor visitArray(String name) {
   1.498 +                    return this;
   1.499 +                }
   1.500 +
   1.501 +                @Override
   1.502 +                public void visitEnd() {
   1.503 +                    if (body != null) {
   1.504 +                        if (javacall) {
   1.505 +                            body = callback(body);
   1.506 +                            args.add("vm");
   1.507 +                        }
   1.508 +                        generateJSBody(args, body);
   1.509 +                    }
   1.510 +                }
   1.511 +            }
   1.512 +        }
   1.513 +
   1.514 +        private final class LoadResource extends AnnotationVisitor {
   1.515 +
   1.516 +            public LoadResource() {
   1.517 +                super(Opcodes.ASM4);
   1.518 +            }
   1.519 +
   1.520 +            @Override
   1.521 +            public void visit(String attrName, Object value) {
   1.522 +                String relPath = (String) value;
   1.523 +                if (relPath.startsWith("/")) {
   1.524 +                    resource = relPath;
   1.525 +                } else {
   1.526 +                    int last = name.lastIndexOf('/');
   1.527 +                    String fullPath = name.substring(0, last + 1) + relPath;
   1.528 +                    resource = fullPath;
   1.529 +                }
   1.530 +            }
   1.531 +        }
   1.532 +    }
   1.533 +
   1.534 +    private static class ClassWriterEx extends ClassWriter {
   1.535 +
   1.536 +        private ClassLoader loader;
   1.537 +
   1.538 +        public ClassWriterEx(ClassLoader l, ClassReader classReader, int flags) {
   1.539 +            super(classReader, flags);
   1.540 +            this.loader = l;
   1.541 +        }
   1.542 +
   1.543 +        @Override
   1.544 +        protected String getCommonSuperClass(final String type1, final String type2) {
   1.545 +            Class<?> c, d;
   1.546 +            try {
   1.547 +                c = Class.forName(type1.replace('/', '.'), false, loader);
   1.548 +                d = Class.forName(type2.replace('/', '.'), false, loader);
   1.549 +            } catch (Exception e) {
   1.550 +                throw new RuntimeException(e.toString());
   1.551 +            }
   1.552 +            if (c.isAssignableFrom(d)) {
   1.553 +                return type1;
   1.554 +            }
   1.555 +            if (d.isAssignableFrom(c)) {
   1.556 +                return type2;
   1.557 +            }
   1.558 +            if (c.isInterface() || d.isInterface()) {
   1.559 +                return "java/lang/Object";
   1.560 +            } else {
   1.561 +                do {
   1.562 +                    c = c.getSuperclass();
   1.563 +                } while (!c.isAssignableFrom(d));
   1.564 +                return c.getName().replace('.', '/');
   1.565 +            }
   1.566 +        }
   1.567 +    }
   1.568 +
   1.569 +    static byte[] transform(ClassLoader loader, byte[] arr) {
   1.570 +        ClassReader cr = new ClassReader(arr) {
   1.571 +            // to allow us to compile with -profile compact1 on 
   1.572 +            // JDK8 while processing the class as JDK7, the highest
   1.573 +            // class format asm 4.1 understands to
   1.574 +            @Override
   1.575 +            public short readShort(int index) {
   1.576 +                short s = super.readShort(index);
   1.577 +                if (index == 6 && s > Opcodes.V1_7) {
   1.578 +                    return Opcodes.V1_7;
   1.579 +                }
   1.580 +                return s;
   1.581 +            }
   1.582 +        };
   1.583 +        FindInClass tst = new FindInClass(loader, null);
   1.584 +        cr.accept(tst, 0);
   1.585 +        if (tst.found > 0) {
   1.586 +            ClassWriter w = new ClassWriterEx(loader, cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
   1.587 +            FindInClass fic = new FindInClass(loader, w);
   1.588 +            cr.accept(fic, 0);
   1.589 +            arr = w.toByteArray();
   1.590 +        }
   1.591 +        return arr;
   1.592 +    }
   1.593 +
   1.594 +    private static final class TrueFn extends Fn {
   1.595 +        @Override
   1.596 +        public Object invoke(Object thiz, Object... args) throws Exception {
   1.597 +            return Boolean.TRUE;
   1.598 +        }
   1.599 +    } // end of TrueFn
   1.600 +}