rt/emul/compact/src/test/java/org/apidesign/vm4brwsr/dynamic/InvokeDynamicTest.java
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.vm4brwsr.dynamic;
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;
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;
45 * @author Jaroslav Tulach <jtulach@netbeans.org>
47 public class InvokeDynamicTest {
48 private static Class<?> invokeDynamicClass;
49 private static byte[] invokeDynamicBytes;
50 private static TestVM code;
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!");
58 @Test public void simpleDynamicInJS() throws Exception {
60 "Invoke dynamic can return a value", InvokeDynamic.class,
61 "dynamic__Ljava_lang_String_2",
68 public static void releaseTheCode() {
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
78 public static void prepareClass() throws Exception {
79 InputStream is = InvokeDynamic.class.getResourceAsStream("InvokeDynamic.class");
80 assertNotNull(is, "Class found");
82 ClassReader reader = new ClassReader(is);
83 ClassWriter writer = new ClassWriter(reader, 0);
86 new ClassVisitor(ASM4, writer) {
88 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions
90 MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
91 return new InvokeDynamicProcessor(mv);
96 invokeDynamicBytes = writer.toByteArray();
97 ClassLoader l = new ClassLoader() {
99 public Class<?> loadClass(String name) throws ClassNotFoundException {
100 if (name.equals(InvokeDynamic.class.getName())) {
101 return defineClass(name, invokeDynamicBytes, 0, invokeDynamicBytes.length);
103 return super.loadClass(name);
106 invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
108 code = TestVM.compileClass(
109 null, null, new EmulationResourcesWithException(),
110 InvokeDynamic.class.getName().replace('.', '/')
115 private static class InvokeDynamicProcessor extends MethodVisitor {
116 InvokeDynamicProcessor(MethodVisitor mv) {
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(
129 MethodHandles.Lookup.class,
132 ).toMethodDescriptorString()
134 super.visitInvokeDynamicInsn(shortName, desc, mh);
138 super.visitMethodInsn(opcode, owner, name, desc);
142 private static class EmulationResourcesWithException implements Bck2Brwsr.Resources {
144 public InputStream get(String name) throws IOException {
145 if ("org/apidesign/vm4brwsr/InvokeDynamic.class".equals(name)) {
146 return new ByteArrayInputStream(invokeDynamicBytes);
148 if ("java/net/URI.class".equals(name)) {
152 if ("java/net/URLConnection.class".equals(name)) {
156 if ("java/lang/System.class".equals(name)) {
160 Enumeration<URL> en = InvokeDynamicTest.class.getClassLoader().getResources(name);
162 while (en.hasMoreElements()) {
163 u = en.nextElement();
166 throw new IOException("Can't find " + name);
168 if (u.toExternalForm().contains("rt.jar!")) {
169 throw new IOException("No emulation for " + u);
171 return u.openStream();