rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/InvokeDynamicTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 10 Aug 2014 07:12:30 +0200
branchjdk8
changeset 1654 da24a2411ee7
parent 1648 rt/emul/compact/src/test/java/org/apidesign/vm4brwsr/dynamic/InvokeDynamicTest.java@905b2a470def
child 1656 589affbf39a2
permissions -rw-r--r--
Moving the test to JDK8 project, making it compilable and parsable by Asm
     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             "dynamic__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         ClassLoader l = new ClassLoader() {
   106             @Override
   107             public Class<?> loadClass(String name) throws ClassNotFoundException {
   108                 if (name.equals(InvokeDynamic.class.getName())) {
   109                     return defineClass(name, invokeDynamicBytes, 0, invokeDynamicBytes.length);
   110                 }
   111                 return super.loadClass(name);
   112             }
   113         };
   114         invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
   115         
   116         code = TestVM.compileClass(
   117             null, null, new EmulationResourcesWithException(),
   118             InvokeDynamic.class.getName().replace('.', '/')
   119         );
   120     }
   121     
   122     
   123     private static class InvokeDynamicProcessor extends MethodVisitor {
   124         InvokeDynamicProcessor(MethodVisitor mv) {
   125             super(ASM4, mv);
   126         }
   127 
   128         @Override
   129         public void visitMethodInsn(int opcode, String owner, String name, String desc) {
   130             if (opcode == INVOKESTATIC) {
   131                 if (name.startsWith("TEST_dynamic_")) {
   132                     final String shortName = name.substring(13);
   133                     Handle mh = new Handle(
   134                         Opcodes.H_INVOKESTATIC, owner, shortName,
   135                         MethodType.methodType(
   136                             CallSite.class,
   137                             MethodHandles.Lookup.class,
   138                             String.class, 
   139                             MethodType.class
   140                         ).toMethodDescriptorString()
   141                     );
   142                     super.visitInvokeDynamicInsn(shortName, desc, mh);
   143                     return;
   144                 }
   145             }
   146             super.visitMethodInsn(opcode, owner, name, desc);
   147         }
   148     }
   149     
   150     private static class EmulationResourcesWithException implements Bck2Brwsr.Resources {
   151         @Override
   152         public InputStream get(String name) throws IOException {
   153             if ("org/apidesign/vm4brwsr/InvokeDynamic.class".equals(name)) {
   154                 return new ByteArrayInputStream(invokeDynamicBytes);
   155             }
   156             if ("java/net/URI.class".equals(name)) {
   157                 // skip
   158                 return null;
   159             }
   160             if ("java/net/URLConnection.class".equals(name)) {
   161                 // skip
   162                 return null;
   163             }
   164             if ("java/lang/System.class".equals(name)) {
   165                 // skip
   166                 return null;
   167             }
   168             Enumeration<URL> en = InvokeDynamicTest.class.getClassLoader().getResources(name);
   169             URL u = null;
   170             while (en.hasMoreElements()) {
   171                 u = en.nextElement();
   172             }
   173             if (u == null) {
   174                 throw new IOException("Can't find " + name);
   175             }
   176             if (u.toExternalForm().contains("rt.jar!")) {
   177                 throw new IOException("No emulation for " + u);
   178             }
   179             return u.openStream();
   180         }
   181     }
   182     
   183 }