rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/InvokeDynamicTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 12 Aug 2014 19:22:39 +0200
branchjdk8
changeset 1667 335691ea9714
parent 1656 589affbf39a2
child 1668 ec0ee77173e9
permissions -rw-r--r--
We have emulation for all the rt/emul/compact classes
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.vm8;
    19 
    20 import java.io.ByteArrayInputStream;
    21 import java.io.IOException;
    22 import java.io.InputStream;
    23 import java.lang.invoke.CallSite;
    24 import java.lang.invoke.MethodHandles;
    25 import java.lang.invoke.MethodType;
    26 import java.lang.reflect.Method;
    27 import java.net.URL;
    28 import java.util.Enumeration;
    29 import org.apidesign.vm4brwsr.Bck2Brwsr;
    30 import org.objectweb.asm.ClassReader;
    31 import org.objectweb.asm.ClassVisitor;
    32 import org.objectweb.asm.ClassWriter;
    33 import org.objectweb.asm.Handle;
    34 import org.objectweb.asm.MethodVisitor;
    35 import org.objectweb.asm.Opcodes;
    36 import static org.objectweb.asm.Opcodes.ASM4;
    37 import static org.objectweb.asm.Opcodes.INVOKESTATIC;
    38 import static org.testng.Assert.*;
    39 import org.testng.annotations.AfterClass;
    40 import org.testng.annotations.BeforeClass;
    41 import org.testng.annotations.Test;
    42 
    43 /**
    44  *
    45  * @author Jaroslav Tulach <jtulach@netbeans.org>
    46  */
    47 public class InvokeDynamicTest {
    48     private static Class<?> invokeDynamicClass;
    49     private static byte[] invokeDynamicBytes;
    50     private static TestVM code;
    51     
    52     @Test public void simpleDynamicInJava() throws Exception {
    53         Method m = invokeDynamicClass.getMethod("dynamicSay");
    54         Object ret = m.invoke(m);
    55         assertEquals(ret, "Hello from Dynamic!");
    56     }
    57     
    58     @Test public void simpleDynamicInJS() throws Exception {
    59         code.assertExec(
    60             "Invoke dynamic can return a value", InvokeDynamic.class,
    61             "dynamicSay__Ljava_lang_String_2",
    62             "Hello from Dynamic!"
    63         );
    64     }
    65     
    66 
    67     @AfterClass
    68     public static void releaseTheCode() {
    69         code = null;
    70     }
    71 
    72     //
    73     // the following code is inspired by 
    74     // https://code.google.com/p/indy-maven-plugin/
    75     // which I don't want to use, as it is not in a public repository
    76     //
    77     @BeforeClass 
    78     public static void prepareClass() throws Exception {
    79         InputStream is = InvokeDynamic.class.getResourceAsStream("InvokeDynamic.class");
    80         assertNotNull(is, "Class found");
    81         
    82         ClassReader reader = new ClassReader(is) {
    83             @Override
    84             public short readShort(int index) {
    85                 if (index == 6) {
    86                     return Opcodes.V1_7;
    87                 }
    88                 return super.readShort(index);
    89             }
    90         };
    91         ClassWriter writer = new ClassWriter(reader, 0);
    92 
    93         reader.accept(
    94                 new ClassVisitor(ASM4, writer) {
    95                     @Override
    96                     public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions
    97                     ) {
    98                         MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    99                         return new InvokeDynamicProcessor(mv);
   100                     }
   101                 },
   102                 0);
   103         is.close();
   104         invokeDynamicBytes = writer.toByteArray();
   105         final boolean[] loaded = { false };
   106         ClassLoader l = new ClassLoader() {
   107             @Override
   108             public Class<?> loadClass(String name) throws ClassNotFoundException {
   109                 if (name.equals(InvokeDynamic.class.getName())) {
   110                     loaded[0] = true;
   111                     return defineClass(name, invokeDynamicBytes, 0, invokeDynamicBytes.length);
   112                 }
   113                 return super.loadClass(name);
   114             }
   115         };
   116         invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
   117         assertTrue(loaded[0], "InvokeDynamic class should be loaded!");
   118 
   119         final EmulResWithInvDyn emul = new EmulResWithInvDyn();
   120         code = TestVM.compileClass(
   121             null, null, emul,
   122             InvokeDynamic.class.getName().replace('.', '/')
   123         );
   124         
   125         assertTrue(emul.loaded, "InvokeDynamic class should be processed!");
   126     }
   127     
   128     
   129     private static class InvokeDynamicProcessor extends MethodVisitor {
   130         InvokeDynamicProcessor(MethodVisitor mv) {
   131             super(ASM4, mv);
   132         }
   133 
   134         @Override
   135         public void visitMethodInsn(int opcode, String owner, String name, String desc) {
   136             if (opcode == INVOKESTATIC) {
   137                 if (name.startsWith("TEST_dynamic_")) {
   138                     final String shortName = name.substring(13);
   139                     Handle mh = new Handle(
   140                         Opcodes.H_INVOKESTATIC, owner, shortName,
   141                         MethodType.methodType(
   142                             CallSite.class,
   143                             MethodHandles.Lookup.class,
   144                             String.class, 
   145                             MethodType.class
   146                         ).toMethodDescriptorString()
   147                     );
   148                     super.visitInvokeDynamicInsn(shortName, desc, mh);
   149                     return;
   150                 }
   151             }
   152             super.visitMethodInsn(opcode, owner, name, desc);
   153         }
   154     }
   155     
   156     private static class EmulResWithInvDyn implements Bck2Brwsr.Resources {
   157         boolean loaded;
   158         
   159         @Override
   160         public InputStream get(String name) throws IOException {
   161             if ("org/apidesign/bck2brwsr/vm8/InvokeDynamic.class".equals(name)) {
   162                 loaded = true;
   163                 return new ByteArrayInputStream(invokeDynamicBytes);
   164             }
   165             Enumeration<URL> en = InvokeDynamicTest.class.getClassLoader().getResources(name);
   166             URL u = null;
   167             while (en.hasMoreElements()) {
   168                 u = en.nextElement();
   169             }
   170             if (u == null) {
   171                 throw new IOException("Can't find " + name);
   172             }
   173             if (u.toExternalForm().contains("rt.jar!")) {
   174                 throw new IOException("No emulation for " + u);
   175             }
   176             return u.openStream();
   177         }
   178     }
   179     
   180 }