rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/InvokeDynamicTest.java
branchjdk8
changeset 1654 da24a2411ee7
parent 1648 905b2a470def
child 1656 589affbf39a2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/InvokeDynamicTest.java	Sun Aug 10 07:12:30 2014 +0200
     1.3 @@ -0,0 +1,183 @@
     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.vm8;
    1.22 +
    1.23 +import java.io.ByteArrayInputStream;
    1.24 +import java.io.IOException;
    1.25 +import java.io.InputStream;
    1.26 +import java.lang.invoke.CallSite;
    1.27 +import java.lang.invoke.MethodHandles;
    1.28 +import java.lang.invoke.MethodType;
    1.29 +import java.lang.reflect.Method;
    1.30 +import java.net.URL;
    1.31 +import java.util.Enumeration;
    1.32 +import org.apidesign.vm4brwsr.Bck2Brwsr;
    1.33 +import org.objectweb.asm.ClassReader;
    1.34 +import org.objectweb.asm.ClassVisitor;
    1.35 +import org.objectweb.asm.ClassWriter;
    1.36 +import org.objectweb.asm.Handle;
    1.37 +import org.objectweb.asm.MethodVisitor;
    1.38 +import org.objectweb.asm.Opcodes;
    1.39 +import static org.objectweb.asm.Opcodes.ASM4;
    1.40 +import static org.objectweb.asm.Opcodes.INVOKESTATIC;
    1.41 +import static org.testng.Assert.*;
    1.42 +import org.testng.annotations.AfterClass;
    1.43 +import org.testng.annotations.BeforeClass;
    1.44 +import org.testng.annotations.Test;
    1.45 +
    1.46 +/**
    1.47 + *
    1.48 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.49 + */
    1.50 +public class InvokeDynamicTest {
    1.51 +    private static Class<?> invokeDynamicClass;
    1.52 +    private static byte[] invokeDynamicBytes;
    1.53 +    private static TestVM code;
    1.54 +    
    1.55 +    @Test public void simpleDynamicInJava() throws Exception {
    1.56 +        Method m = invokeDynamicClass.getMethod("dynamicSay");
    1.57 +        Object ret = m.invoke(m);
    1.58 +        assertEquals(ret, "Hello from Dynamic!");
    1.59 +    }
    1.60 +    
    1.61 +    @Test public void simpleDynamicInJS() throws Exception {
    1.62 +        code.assertExec(
    1.63 +            "Invoke dynamic can return a value", InvokeDynamic.class,
    1.64 +            "dynamic__Ljava_lang_String_2",
    1.65 +            "Hello from Dynamic!"
    1.66 +        );
    1.67 +    }
    1.68 +    
    1.69 +
    1.70 +    @AfterClass
    1.71 +    public static void releaseTheCode() {
    1.72 +        code = null;
    1.73 +    }
    1.74 +
    1.75 +    //
    1.76 +    // the following code is inspired by 
    1.77 +    // https://code.google.com/p/indy-maven-plugin/
    1.78 +    // which I don't want to use, as it is not in a public repository
    1.79 +    //
    1.80 +    @BeforeClass 
    1.81 +    public static void prepareClass() throws Exception {
    1.82 +        InputStream is = InvokeDynamic.class.getResourceAsStream("InvokeDynamic.class");
    1.83 +        assertNotNull(is, "Class found");
    1.84 +        
    1.85 +        ClassReader reader = new ClassReader(is) {
    1.86 +            @Override
    1.87 +            public short readShort(int index) {
    1.88 +                if (index == 6) {
    1.89 +                    return Opcodes.V1_7;
    1.90 +                }
    1.91 +                return super.readShort(index);
    1.92 +            }
    1.93 +        };
    1.94 +        ClassWriter writer = new ClassWriter(reader, 0);
    1.95 +
    1.96 +        reader.accept(
    1.97 +                new ClassVisitor(ASM4, writer) {
    1.98 +                    @Override
    1.99 +                    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions
   1.100 +                    ) {
   1.101 +                        MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
   1.102 +                        return new InvokeDynamicProcessor(mv);
   1.103 +                    }
   1.104 +                },
   1.105 +                0);
   1.106 +        is.close();
   1.107 +        invokeDynamicBytes = writer.toByteArray();
   1.108 +        ClassLoader l = new ClassLoader() {
   1.109 +            @Override
   1.110 +            public Class<?> loadClass(String name) throws ClassNotFoundException {
   1.111 +                if (name.equals(InvokeDynamic.class.getName())) {
   1.112 +                    return defineClass(name, invokeDynamicBytes, 0, invokeDynamicBytes.length);
   1.113 +                }
   1.114 +                return super.loadClass(name);
   1.115 +            }
   1.116 +        };
   1.117 +        invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
   1.118 +        
   1.119 +        code = TestVM.compileClass(
   1.120 +            null, null, new EmulationResourcesWithException(),
   1.121 +            InvokeDynamic.class.getName().replace('.', '/')
   1.122 +        );
   1.123 +    }
   1.124 +    
   1.125 +    
   1.126 +    private static class InvokeDynamicProcessor extends MethodVisitor {
   1.127 +        InvokeDynamicProcessor(MethodVisitor mv) {
   1.128 +            super(ASM4, mv);
   1.129 +        }
   1.130 +
   1.131 +        @Override
   1.132 +        public void visitMethodInsn(int opcode, String owner, String name, String desc) {
   1.133 +            if (opcode == INVOKESTATIC) {
   1.134 +                if (name.startsWith("TEST_dynamic_")) {
   1.135 +                    final String shortName = name.substring(13);
   1.136 +                    Handle mh = new Handle(
   1.137 +                        Opcodes.H_INVOKESTATIC, owner, shortName,
   1.138 +                        MethodType.methodType(
   1.139 +                            CallSite.class,
   1.140 +                            MethodHandles.Lookup.class,
   1.141 +                            String.class, 
   1.142 +                            MethodType.class
   1.143 +                        ).toMethodDescriptorString()
   1.144 +                    );
   1.145 +                    super.visitInvokeDynamicInsn(shortName, desc, mh);
   1.146 +                    return;
   1.147 +                }
   1.148 +            }
   1.149 +            super.visitMethodInsn(opcode, owner, name, desc);
   1.150 +        }
   1.151 +    }
   1.152 +    
   1.153 +    private static class EmulationResourcesWithException implements Bck2Brwsr.Resources {
   1.154 +        @Override
   1.155 +        public InputStream get(String name) throws IOException {
   1.156 +            if ("org/apidesign/vm4brwsr/InvokeDynamic.class".equals(name)) {
   1.157 +                return new ByteArrayInputStream(invokeDynamicBytes);
   1.158 +            }
   1.159 +            if ("java/net/URI.class".equals(name)) {
   1.160 +                // skip
   1.161 +                return null;
   1.162 +            }
   1.163 +            if ("java/net/URLConnection.class".equals(name)) {
   1.164 +                // skip
   1.165 +                return null;
   1.166 +            }
   1.167 +            if ("java/lang/System.class".equals(name)) {
   1.168 +                // skip
   1.169 +                return null;
   1.170 +            }
   1.171 +            Enumeration<URL> en = InvokeDynamicTest.class.getClassLoader().getResources(name);
   1.172 +            URL u = null;
   1.173 +            while (en.hasMoreElements()) {
   1.174 +                u = en.nextElement();
   1.175 +            }
   1.176 +            if (u == null) {
   1.177 +                throw new IOException("Can't find " + name);
   1.178 +            }
   1.179 +            if (u.toExternalForm().contains("rt.jar!")) {
   1.180 +                throw new IOException("No emulation for " + u);
   1.181 +            }
   1.182 +            return u.openStream();
   1.183 +        }
   1.184 +    }
   1.185 +    
   1.186 +}