jaroslav@1644
|
1 |
/**
|
jaroslav@1644
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1644
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1644
|
4 |
*
|
jaroslav@1644
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1644
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1644
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1644
|
8 |
*
|
jaroslav@1644
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1644
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1644
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1644
|
12 |
* GNU General Public License for more details.
|
jaroslav@1644
|
13 |
*
|
jaroslav@1644
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1644
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1644
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@1644
|
17 |
*/
|
jaroslav@1644
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@1644
|
19 |
|
jaroslav@1644
|
20 |
import java.io.InputStream;
|
jaroslav@1644
|
21 |
import java.lang.invoke.CallSite;
|
jaroslav@1644
|
22 |
import java.lang.invoke.MethodHandles;
|
jaroslav@1644
|
23 |
import java.lang.invoke.MethodType;
|
jaroslav@1644
|
24 |
import java.lang.reflect.Method;
|
jaroslav@1644
|
25 |
import org.objectweb.asm.ClassReader;
|
jaroslav@1644
|
26 |
import org.objectweb.asm.ClassVisitor;
|
jaroslav@1644
|
27 |
import org.objectweb.asm.ClassWriter;
|
jaroslav@1644
|
28 |
import org.objectweb.asm.Handle;
|
jaroslav@1644
|
29 |
import org.objectweb.asm.MethodVisitor;
|
jaroslav@1644
|
30 |
import org.objectweb.asm.Opcodes;
|
jaroslav@1644
|
31 |
import static org.objectweb.asm.Opcodes.ASM4;
|
jaroslav@1644
|
32 |
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
|
jaroslav@1644
|
33 |
import static org.testng.Assert.*;
|
jaroslav@1644
|
34 |
import org.testng.annotations.BeforeClass;
|
jaroslav@1644
|
35 |
import org.testng.annotations.Test;
|
jaroslav@1644
|
36 |
|
jaroslav@1644
|
37 |
/**
|
jaroslav@1644
|
38 |
*
|
jaroslav@1644
|
39 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@1644
|
40 |
*/
|
jaroslav@1644
|
41 |
public class InvokeDynamicTest {
|
jaroslav@1644
|
42 |
private static Class<?> invokeDynamicClass;
|
jaroslav@1644
|
43 |
|
jaroslav@1644
|
44 |
@Test public void simpleDynamicInJava() throws Exception {
|
jaroslav@1644
|
45 |
Method m = invokeDynamicClass.getMethod("dynamicSay");
|
jaroslav@1644
|
46 |
Object ret = m.invoke(m);
|
jaroslav@1644
|
47 |
assertEquals(ret, "Hello from Dynamic!");
|
jaroslav@1644
|
48 |
}
|
jaroslav@1644
|
49 |
|
jaroslav@1644
|
50 |
|
jaroslav@1644
|
51 |
/*
|
jaroslav@1644
|
52 |
private static TestVM code;
|
jaroslav@1644
|
53 |
|
jaroslav@1644
|
54 |
@BeforeClass
|
jaroslav@1644
|
55 |
public void compileTheCode() throws Exception {
|
jaroslav@1644
|
56 |
code = TestVM.compileClass(InvokeDynamic.class.getName());
|
jaroslav@1644
|
57 |
}
|
jaroslav@1644
|
58 |
|
jaroslav@1644
|
59 |
@AfterClass
|
jaroslav@1644
|
60 |
public static void releaseTheCode() {
|
jaroslav@1644
|
61 |
code = null;
|
jaroslav@1644
|
62 |
}
|
jaroslav@1644
|
63 |
|
jaroslav@1644
|
64 |
private void assertExec(
|
jaroslav@1644
|
65 |
String msg, Class clazz, String method, Object expRes, Object... args
|
jaroslav@1644
|
66 |
) throws Exception {
|
jaroslav@1644
|
67 |
code.assertExec(msg, clazz, method, expRes, args);
|
jaroslav@1644
|
68 |
}
|
jaroslav@1644
|
69 |
*/
|
jaroslav@1644
|
70 |
|
jaroslav@1644
|
71 |
//
|
jaroslav@1644
|
72 |
// the following code is inspired by
|
jaroslav@1644
|
73 |
// https://code.google.com/p/indy-maven-plugin/
|
jaroslav@1644
|
74 |
// which I don't want to use, as it is not in a public repository
|
jaroslav@1644
|
75 |
//
|
jaroslav@1644
|
76 |
@BeforeClass
|
jaroslav@1644
|
77 |
public static void prepareClass() throws Exception {
|
jaroslav@1644
|
78 |
InputStream input = InvokeDynamic.class.getResourceAsStream("InvokeDynamic.class");
|
jaroslav@1644
|
79 |
assertNotNull(input, "Class found");
|
jaroslav@1644
|
80 |
|
jaroslav@1644
|
81 |
ClassReader reader = new ClassReader(input);
|
jaroslav@1644
|
82 |
ClassWriter writer = new ClassWriter(reader, 0);
|
jaroslav@1644
|
83 |
|
jaroslav@1644
|
84 |
reader.accept(
|
jaroslav@1644
|
85 |
new ClassVisitor(ASM4, writer) {
|
jaroslav@1644
|
86 |
@Override
|
jaroslav@1644
|
87 |
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions
|
jaroslav@1644
|
88 |
) {
|
jaroslav@1644
|
89 |
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
|
jaroslav@1644
|
90 |
return new InvokeDynamicProcessor(mv);
|
jaroslav@1644
|
91 |
}
|
jaroslav@1644
|
92 |
},
|
jaroslav@1644
|
93 |
0);
|
jaroslav@1644
|
94 |
input.close();
|
jaroslav@1644
|
95 |
final byte[] invokeDynamicBytes = writer.toByteArray();
|
jaroslav@1644
|
96 |
ClassLoader l = new ClassLoader() {
|
jaroslav@1644
|
97 |
@Override
|
jaroslav@1644
|
98 |
public Class<?> loadClass(String name) throws ClassNotFoundException {
|
jaroslav@1644
|
99 |
if (name.equals(InvokeDynamic.class.getName())) {
|
jaroslav@1644
|
100 |
return defineClass(name, invokeDynamicBytes, 0, invokeDynamicBytes.length);
|
jaroslav@1644
|
101 |
}
|
jaroslav@1644
|
102 |
return super.loadClass(name);
|
jaroslav@1644
|
103 |
}
|
jaroslav@1644
|
104 |
};
|
jaroslav@1644
|
105 |
invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
|
jaroslav@1644
|
106 |
}
|
jaroslav@1644
|
107 |
|
jaroslav@1644
|
108 |
|
jaroslav@1644
|
109 |
private static class InvokeDynamicProcessor extends MethodVisitor {
|
jaroslav@1644
|
110 |
InvokeDynamicProcessor(MethodVisitor mv) {
|
jaroslav@1644
|
111 |
super(ASM4, mv);
|
jaroslav@1644
|
112 |
}
|
jaroslav@1644
|
113 |
|
jaroslav@1644
|
114 |
@Override
|
jaroslav@1644
|
115 |
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
|
jaroslav@1644
|
116 |
if (opcode == INVOKESTATIC) {
|
jaroslav@1644
|
117 |
if (name.startsWith("TEST_dynamic_")) {
|
jaroslav@1644
|
118 |
final String shortName = name.substring(13);
|
jaroslav@1644
|
119 |
Handle mh = new Handle(
|
jaroslav@1644
|
120 |
Opcodes.H_INVOKESTATIC, owner, shortName,
|
jaroslav@1644
|
121 |
MethodType.methodType(
|
jaroslav@1644
|
122 |
CallSite.class,
|
jaroslav@1644
|
123 |
MethodHandles.Lookup.class,
|
jaroslav@1644
|
124 |
String.class,
|
jaroslav@1644
|
125 |
MethodType.class
|
jaroslav@1644
|
126 |
).toMethodDescriptorString()
|
jaroslav@1644
|
127 |
);
|
jaroslav@1644
|
128 |
super.visitInvokeDynamicInsn(shortName, desc, mh);
|
jaroslav@1644
|
129 |
return;
|
jaroslav@1644
|
130 |
}
|
jaroslav@1644
|
131 |
}
|
jaroslav@1644
|
132 |
super.visitMethodInsn(opcode, owner, name, desc);
|
jaroslav@1644
|
133 |
}
|
jaroslav@1644
|
134 |
}
|
jaroslav@1644
|
135 |
|
jaroslav@1644
|
136 |
|
jaroslav@1644
|
137 |
}
|