rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/InvokeDynamicTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 17 Aug 2014 08:27:45 +0200
branchjdk8
changeset 1672 1efb2645c7aa
parent 1668 ec0ee77173e9
permissions -rw-r--r--
Executing first lambda expression on bck2brwsr thanks to preprocessing of retrolamda
     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     /* Well, supporting general invokeDynamic is 
    59     huge hassle, so giving up. More at
    60     http://wiki.apidesign.org/wiki/InvokeDynamic
    61     
    62     @Test public void simpleDynamicInJS() throws Exception {
    63         code().assertExec(
    64             "Invoke dynamic can return a value", InvokeDynamic.class,
    65             "dynamicSay__Ljava_lang_String_2",
    66             "Hello from Dynamic!"
    67         );
    68     }
    69     */
    70     
    71     private TestVM code() throws Exception {
    72         if (code == null) {
    73             final EmulResWithInvDyn emul = new EmulResWithInvDyn();
    74             code = TestVM.compileClass(
    75                     null, null, emul,
    76                     InvokeDynamic.class.getName().replace('.', '/')
    77             );
    78 
    79             assertTrue(emul.loaded, "InvokeDynamic class should be processed!");
    80         }
    81         return code;
    82     }
    83 
    84     @AfterClass
    85     public static void releaseTheCode() {
    86         code = null;
    87     }
    88 
    89     //
    90     // the following code is inspired by 
    91     // https://code.google.com/p/indy-maven-plugin/
    92     // which I don't want to use, as it is not in a public repository
    93     //
    94     @BeforeClass 
    95     public static void prepareClass() throws Exception {
    96         InputStream is = InvokeDynamic.class.getResourceAsStream("InvokeDynamic.class");
    97         assertNotNull(is, "Class found");
    98         
    99         ClassReader reader = new ClassReader(is) {
   100             @Override
   101             public short readShort(int index) {
   102                 if (index == 6) {
   103                     return Opcodes.V1_7;
   104                 }
   105                 return super.readShort(index);
   106             }
   107         };
   108         ClassWriter writer = new ClassWriter(reader, 0);
   109 
   110         reader.accept(
   111                 new ClassVisitor(ASM4, writer) {
   112                     @Override
   113                     public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions
   114                     ) {
   115                         MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
   116                         return new InvokeDynamicProcessor(mv);
   117                     }
   118                 },
   119                 0);
   120         is.close();
   121         invokeDynamicBytes = writer.toByteArray();
   122         final boolean[] loaded = { false };
   123         ClassLoader l = new ClassLoader() {
   124             @Override
   125             public Class<?> loadClass(String name) throws ClassNotFoundException {
   126                 if (name.equals(InvokeDynamic.class.getName())) {
   127                     loaded[0] = true;
   128                     return defineClass(name, invokeDynamicBytes, 0, invokeDynamicBytes.length);
   129                 }
   130                 return super.loadClass(name);
   131             }
   132         };
   133         invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
   134         assertTrue(loaded[0], "InvokeDynamic class should be loaded!");
   135     }
   136     
   137     
   138     private static class InvokeDynamicProcessor extends MethodVisitor {
   139         InvokeDynamicProcessor(MethodVisitor mv) {
   140             super(ASM4, mv);
   141         }
   142 
   143         @Override
   144         public void visitMethodInsn(int opcode, String owner, String name, String desc) {
   145             if (opcode == INVOKESTATIC) {
   146                 if (name.startsWith("TEST_dynamic_")) {
   147                     final String shortName = name.substring(13);
   148                     Handle mh = new Handle(
   149                         Opcodes.H_INVOKESTATIC, owner, shortName,
   150                         MethodType.methodType(
   151                             CallSite.class,
   152                             MethodHandles.Lookup.class,
   153                             String.class, 
   154                             MethodType.class
   155                         ).toMethodDescriptorString()
   156                     );
   157                     super.visitInvokeDynamicInsn(shortName, desc, mh);
   158                     return;
   159                 }
   160             }
   161             super.visitMethodInsn(opcode, owner, name, desc);
   162         }
   163     }
   164     
   165     private static class EmulResWithInvDyn implements Bck2Brwsr.Resources {
   166         boolean loaded;
   167         
   168         @Override
   169         public InputStream get(String name) throws IOException {
   170             if ("org/apidesign/bck2brwsr/vm8/InvokeDynamic.class".equals(name)) {
   171                 loaded = true;
   172                 return new ByteArrayInputStream(invokeDynamicBytes);
   173             }
   174             Enumeration<URL> en = InvokeDynamicTest.class.getClassLoader().getResources(name);
   175             URL u = null;
   176             while (en.hasMoreElements()) {
   177                 u = en.nextElement();
   178             }
   179             if (u == null) {
   180                 throw new IOException("Can't find " + name);
   181             }
   182             if (u.toExternalForm().contains("rt.jar!")) {
   183                 throw new IOException("No emulation for " + u);
   184             }
   185             return u.openStream();
   186         }
   187     }
   188     
   189 }