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.bck2brwsr.vm8;
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) {
84 public short readShort(int index) {
88 return super.readShort(index);
91 ClassWriter writer = new ClassWriter(reader, 0);
94 new ClassVisitor(ASM4, writer) {
96 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions
98 MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
99 return new InvokeDynamicProcessor(mv);
104 invokeDynamicBytes = writer.toByteArray();
105 ClassLoader l = new ClassLoader() {
107 public Class<?> loadClass(String name) throws ClassNotFoundException {
108 if (name.equals(InvokeDynamic.class.getName())) {
109 return defineClass(name, invokeDynamicBytes, 0, invokeDynamicBytes.length);
111 return super.loadClass(name);
114 invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
116 code = TestVM.compileClass(
117 null, null, new EmulationResourcesWithException(),
118 InvokeDynamic.class.getName().replace('.', '/')
123 private static class InvokeDynamicProcessor extends MethodVisitor {
124 InvokeDynamicProcessor(MethodVisitor mv) {
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(
137 MethodHandles.Lookup.class,
140 ).toMethodDescriptorString()
142 super.visitInvokeDynamicInsn(shortName, desc, mh);
146 super.visitMethodInsn(opcode, owner, name, desc);
150 private static class EmulationResourcesWithException implements Bck2Brwsr.Resources {
152 public InputStream get(String name) throws IOException {
153 if ("org/apidesign/vm4brwsr/InvokeDynamic.class".equals(name)) {
154 return new ByteArrayInputStream(invokeDynamicBytes);
156 if ("java/net/URI.class".equals(name)) {
160 if ("java/net/URLConnection.class".equals(name)) {
164 if ("java/lang/System.class".equals(name)) {
168 Enumeration<URL> en = InvokeDynamicTest.class.getClassLoader().getResources(name);
170 while (en.hasMoreElements()) {
171 u = en.nextElement();
174 throw new IOException("Can't find " + name);
176 if (u.toExternalForm().contains("rt.jar!")) {
177 throw new IOException("No emulation for " + u);
179 return u.openStream();