rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/InvokeDynamicTest.java
branchjdk8
changeset 1680 3b553acbd931
parent 1679 93f4fbc4d1b7
child 1681 2082d4c4bf11
     1.1 --- a/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/InvokeDynamicTest.java	Sat Sep 13 14:19:43 2014 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,189 +0,0 @@
     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 -    /* Well, supporting general invokeDynamic is 
    1.62 -    huge hassle, so giving up. More at
    1.63 -    http://wiki.apidesign.org/wiki/InvokeDynamic
    1.64 -    
    1.65 -    @Test public void simpleDynamicInJS() throws Exception {
    1.66 -        code().assertExec(
    1.67 -            "Invoke dynamic can return a value", InvokeDynamic.class,
    1.68 -            "dynamicSay__Ljava_lang_String_2",
    1.69 -            "Hello from Dynamic!"
    1.70 -        );
    1.71 -    }
    1.72 -    */
    1.73 -    
    1.74 -    private TestVM code() throws Exception {
    1.75 -        if (code == null) {
    1.76 -            final EmulResWithInvDyn emul = new EmulResWithInvDyn();
    1.77 -            code = TestVM.compileClass(
    1.78 -                    null, null, emul,
    1.79 -                    InvokeDynamic.class.getName().replace('.', '/')
    1.80 -            );
    1.81 -
    1.82 -            assertTrue(emul.loaded, "InvokeDynamic class should be processed!");
    1.83 -        }
    1.84 -        return code;
    1.85 -    }
    1.86 -
    1.87 -    @AfterClass
    1.88 -    public static void releaseTheCode() {
    1.89 -        code = null;
    1.90 -    }
    1.91 -
    1.92 -    //
    1.93 -    // the following code is inspired by 
    1.94 -    // https://code.google.com/p/indy-maven-plugin/
    1.95 -    // which I don't want to use, as it is not in a public repository
    1.96 -    //
    1.97 -    @BeforeClass 
    1.98 -    public static void prepareClass() throws Exception {
    1.99 -        InputStream is = InvokeDynamic.class.getResourceAsStream("InvokeDynamic.class");
   1.100 -        assertNotNull(is, "Class found");
   1.101 -        
   1.102 -        ClassReader reader = new ClassReader(is) {
   1.103 -            @Override
   1.104 -            public short readShort(int index) {
   1.105 -                if (index == 6) {
   1.106 -                    return Opcodes.V1_7;
   1.107 -                }
   1.108 -                return super.readShort(index);
   1.109 -            }
   1.110 -        };
   1.111 -        ClassWriter writer = new ClassWriter(reader, 0);
   1.112 -
   1.113 -        reader.accept(
   1.114 -                new ClassVisitor(ASM4, writer) {
   1.115 -                    @Override
   1.116 -                    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions
   1.117 -                    ) {
   1.118 -                        MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
   1.119 -                        return new InvokeDynamicProcessor(mv);
   1.120 -                    }
   1.121 -                },
   1.122 -                0);
   1.123 -        is.close();
   1.124 -        invokeDynamicBytes = writer.toByteArray();
   1.125 -        final boolean[] loaded = { false };
   1.126 -        ClassLoader l = new ClassLoader() {
   1.127 -            @Override
   1.128 -            public Class<?> loadClass(String name) throws ClassNotFoundException {
   1.129 -                if (name.equals(InvokeDynamic.class.getName())) {
   1.130 -                    loaded[0] = true;
   1.131 -                    return defineClass(name, invokeDynamicBytes, 0, invokeDynamicBytes.length);
   1.132 -                }
   1.133 -                return super.loadClass(name);
   1.134 -            }
   1.135 -        };
   1.136 -        invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
   1.137 -        assertTrue(loaded[0], "InvokeDynamic class should be loaded!");
   1.138 -    }
   1.139 -    
   1.140 -    
   1.141 -    private static class InvokeDynamicProcessor extends MethodVisitor {
   1.142 -        InvokeDynamicProcessor(MethodVisitor mv) {
   1.143 -            super(ASM4, mv);
   1.144 -        }
   1.145 -
   1.146 -        @Override
   1.147 -        public void visitMethodInsn(int opcode, String owner, String name, String desc) {
   1.148 -            if (opcode == INVOKESTATIC) {
   1.149 -                if (name.startsWith("TEST_dynamic_")) {
   1.150 -                    final String shortName = name.substring(13);
   1.151 -                    Handle mh = new Handle(
   1.152 -                        Opcodes.H_INVOKESTATIC, owner, shortName,
   1.153 -                        MethodType.methodType(
   1.154 -                            CallSite.class,
   1.155 -                            MethodHandles.Lookup.class,
   1.156 -                            String.class, 
   1.157 -                            MethodType.class
   1.158 -                        ).toMethodDescriptorString()
   1.159 -                    );
   1.160 -                    super.visitInvokeDynamicInsn(shortName, desc, mh);
   1.161 -                    return;
   1.162 -                }
   1.163 -            }
   1.164 -            super.visitMethodInsn(opcode, owner, name, desc);
   1.165 -        }
   1.166 -    }
   1.167 -    
   1.168 -    private static class EmulResWithInvDyn implements Bck2Brwsr.Resources {
   1.169 -        boolean loaded;
   1.170 -        
   1.171 -        @Override
   1.172 -        public InputStream get(String name) throws IOException {
   1.173 -            if ("org/apidesign/bck2brwsr/vm8/InvokeDynamic.class".equals(name)) {
   1.174 -                loaded = true;
   1.175 -                return new ByteArrayInputStream(invokeDynamicBytes);
   1.176 -            }
   1.177 -            Enumeration<URL> en = InvokeDynamicTest.class.getClassLoader().getResources(name);
   1.178 -            URL u = null;
   1.179 -            while (en.hasMoreElements()) {
   1.180 -                u = en.nextElement();
   1.181 -            }
   1.182 -            if (u == null) {
   1.183 -                throw new IOException("Can't find " + name);
   1.184 -            }
   1.185 -            if (u.toExternalForm().contains("rt.jar!")) {
   1.186 -                throw new IOException("No emulation for " + u);
   1.187 -            }
   1.188 -            return u.openStream();
   1.189 -        }
   1.190 -    }
   1.191 -    
   1.192 -}