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;
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.objectweb.asm.ClassReader;
30 import org.objectweb.asm.ClassVisitor;
31 import org.objectweb.asm.ClassWriter;
32 import org.objectweb.asm.Handle;
33 import org.objectweb.asm.MethodVisitor;
34 import org.objectweb.asm.Opcodes;
35 import static org.objectweb.asm.Opcodes.ASM4;
36 import static org.objectweb.asm.Opcodes.INVOKESTATIC;
37 import static org.testng.Assert.*;
38 import org.testng.annotations.AfterClass;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Test;
44 * @author Jaroslav Tulach <jtulach@netbeans.org>
46 public class InvokeDynamicTest {
47 private static Class<?> invokeDynamicClass;
48 private static byte[] invokeDynamicBytes;
49 private static TestVM code;
51 @Test public void simpleDynamicInJava() throws Exception {
52 Method m = invokeDynamicClass.getMethod("dynamicSay");
53 Object ret = m.invoke(m);
54 assertEquals(ret, "Hello from Dynamic!");
57 @Test public void simpleDynamicInJS() throws Exception {
59 "Invoke dynamic can return a value", InvokeDynamic.class,
60 "dynamic__Ljava_lang_String_2",
67 public static void releaseTheCode() {
72 // the following code is inspired by
73 // https://code.google.com/p/indy-maven-plugin/
74 // which I don't want to use, as it is not in a public repository
77 public static void prepareClass() throws Exception {
78 InputStream is = InvokeDynamic.class.getResourceAsStream("InvokeDynamic.class");
79 assertNotNull(is, "Class found");
81 ClassReader reader = new ClassReader(is);
82 ClassWriter writer = new ClassWriter(reader, 0);
85 new ClassVisitor(ASM4, writer) {
87 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions
89 MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
90 return new InvokeDynamicProcessor(mv);
95 invokeDynamicBytes = writer.toByteArray();
96 ClassLoader l = new ClassLoader() {
98 public Class<?> loadClass(String name) throws ClassNotFoundException {
99 if (name.equals(InvokeDynamic.class.getName())) {
100 return defineClass(name, invokeDynamicBytes, 0, invokeDynamicBytes.length);
102 return super.loadClass(name);
105 invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
107 code = TestVM.compileClass(
108 null, null, new EmulationResourcesWithException(),
109 InvokeDynamic.class.getName().replace('.', '/')
114 private static class InvokeDynamicProcessor extends MethodVisitor {
115 InvokeDynamicProcessor(MethodVisitor mv) {
120 public void visitMethodInsn(int opcode, String owner, String name, String desc) {
121 if (opcode == INVOKESTATIC) {
122 if (name.startsWith("TEST_dynamic_")) {
123 final String shortName = name.substring(13);
124 Handle mh = new Handle(
125 Opcodes.H_INVOKESTATIC, owner, shortName,
126 MethodType.methodType(
128 MethodHandles.Lookup.class,
131 ).toMethodDescriptorString()
133 super.visitInvokeDynamicInsn(shortName, desc, mh);
137 super.visitMethodInsn(opcode, owner, name, desc);
141 private static class EmulationResourcesWithException implements Bck2Brwsr.Resources {
143 public InputStream get(String name) throws IOException {
144 if ("org/apidesign/vm4brwsr/InvokeDynamic.class".equals(name)) {
145 return new ByteArrayInputStream(invokeDynamicBytes);
147 if ("java/net/URI.class".equals(name)) {
151 if ("java/net/URLConnection.class".equals(name)) {
155 if ("java/lang/System.class".equals(name)) {
159 Enumeration<URL> en = InvokeDynamicTest.class.getClassLoader().getResources(name);
161 while (en.hasMoreElements()) {
162 u = en.nextElement();
165 throw new IOException("Can't find " + name);
167 if (u.toExternalForm().contains("rt.jar!")) {
168 throw new IOException("No emulation for " + u);
170 return u.openStream();