rt/emul/compact/src/test/java/org/apidesign/vm4brwsr/dynamic/InvokeDynamicTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 09 Aug 2014 12:15:18 +0200
branchjdk8
changeset 1648 905b2a470def
permissions -rw-r--r--
Can these tests be placed in compact profile
     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.vm4brwsr.dynamic;
    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         ClassWriter writer = new ClassWriter(reader, 0);
    84 
    85         reader.accept(
    86                 new ClassVisitor(ASM4, writer) {
    87                     @Override
    88                     public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions
    89                     ) {
    90                         MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    91                         return new InvokeDynamicProcessor(mv);
    92                     }
    93                 },
    94                 0);
    95         is.close();
    96         invokeDynamicBytes = writer.toByteArray();
    97         ClassLoader l = new ClassLoader() {
    98             @Override
    99             public Class<?> loadClass(String name) throws ClassNotFoundException {
   100                 if (name.equals(InvokeDynamic.class.getName())) {
   101                     return defineClass(name, invokeDynamicBytes, 0, invokeDynamicBytes.length);
   102                 }
   103                 return super.loadClass(name);
   104             }
   105         };
   106         invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
   107         
   108         code = TestVM.compileClass(
   109             null, null, new EmulationResourcesWithException(),
   110             InvokeDynamic.class.getName().replace('.', '/')
   111         );
   112     }
   113     
   114     
   115     private static class InvokeDynamicProcessor extends MethodVisitor {
   116         InvokeDynamicProcessor(MethodVisitor mv) {
   117             super(ASM4, mv);
   118         }
   119 
   120         @Override
   121         public void visitMethodInsn(int opcode, String owner, String name, String desc) {
   122             if (opcode == INVOKESTATIC) {
   123                 if (name.startsWith("TEST_dynamic_")) {
   124                     final String shortName = name.substring(13);
   125                     Handle mh = new Handle(
   126                         Opcodes.H_INVOKESTATIC, owner, shortName,
   127                         MethodType.methodType(
   128                             CallSite.class,
   129                             MethodHandles.Lookup.class,
   130                             String.class, 
   131                             MethodType.class
   132                         ).toMethodDescriptorString()
   133                     );
   134                     super.visitInvokeDynamicInsn(shortName, desc, mh);
   135                     return;
   136                 }
   137             }
   138             super.visitMethodInsn(opcode, owner, name, desc);
   139         }
   140     }
   141     
   142     private static class EmulationResourcesWithException implements Bck2Brwsr.Resources {
   143         @Override
   144         public InputStream get(String name) throws IOException {
   145             if ("org/apidesign/vm4brwsr/InvokeDynamic.class".equals(name)) {
   146                 return new ByteArrayInputStream(invokeDynamicBytes);
   147             }
   148             if ("java/net/URI.class".equals(name)) {
   149                 // skip
   150                 return null;
   151             }
   152             if ("java/net/URLConnection.class".equals(name)) {
   153                 // skip
   154                 return null;
   155             }
   156             if ("java/lang/System.class".equals(name)) {
   157                 // skip
   158                 return null;
   159             }
   160             Enumeration<URL> en = InvokeDynamicTest.class.getClassLoader().getResources(name);
   161             URL u = null;
   162             while (en.hasMoreElements()) {
   163                 u = en.nextElement();
   164             }
   165             if (u == null) {
   166                 throw new IOException("Can't find " + name);
   167             }
   168             if (u.toExternalForm().contains("rt.jar!")) {
   169                 throw new IOException("No emulation for " + u);
   170             }
   171             return u.openStream();
   172         }
   173     }
   174     
   175 }