1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/InvokeDynamicTest.java Sat Aug 09 10:42:22 2014 +0200
1.3 @@ -0,0 +1,137 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.vm4brwsr;
1.22 +
1.23 +import java.io.InputStream;
1.24 +import java.lang.invoke.CallSite;
1.25 +import java.lang.invoke.MethodHandles;
1.26 +import java.lang.invoke.MethodType;
1.27 +import java.lang.reflect.Method;
1.28 +import org.objectweb.asm.ClassReader;
1.29 +import org.objectweb.asm.ClassVisitor;
1.30 +import org.objectweb.asm.ClassWriter;
1.31 +import org.objectweb.asm.Handle;
1.32 +import org.objectweb.asm.MethodVisitor;
1.33 +import org.objectweb.asm.Opcodes;
1.34 +import static org.objectweb.asm.Opcodes.ASM4;
1.35 +import static org.objectweb.asm.Opcodes.INVOKESTATIC;
1.36 +import static org.testng.Assert.*;
1.37 +import org.testng.annotations.BeforeClass;
1.38 +import org.testng.annotations.Test;
1.39 +
1.40 +/**
1.41 + *
1.42 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.43 + */
1.44 +public class InvokeDynamicTest {
1.45 + private static Class<?> invokeDynamicClass;
1.46 +
1.47 + @Test public void simpleDynamicInJava() throws Exception {
1.48 + Method m = invokeDynamicClass.getMethod("dynamicSay");
1.49 + Object ret = m.invoke(m);
1.50 + assertEquals(ret, "Hello from Dynamic!");
1.51 + }
1.52 +
1.53 +
1.54 +/*
1.55 + private static TestVM code;
1.56 +
1.57 + @BeforeClass
1.58 + public void compileTheCode() throws Exception {
1.59 + code = TestVM.compileClass(InvokeDynamic.class.getName());
1.60 + }
1.61 +
1.62 + @AfterClass
1.63 + public static void releaseTheCode() {
1.64 + code = null;
1.65 + }
1.66 +
1.67 + private void assertExec(
1.68 + String msg, Class clazz, String method, Object expRes, Object... args
1.69 + ) throws Exception {
1.70 + code.assertExec(msg, clazz, method, expRes, args);
1.71 + }
1.72 + */
1.73 +
1.74 + //
1.75 + // the following code is inspired by
1.76 + // https://code.google.com/p/indy-maven-plugin/
1.77 + // which I don't want to use, as it is not in a public repository
1.78 + //
1.79 + @BeforeClass
1.80 + public static void prepareClass() throws Exception {
1.81 + InputStream input = InvokeDynamic.class.getResourceAsStream("InvokeDynamic.class");
1.82 + assertNotNull(input, "Class found");
1.83 +
1.84 + ClassReader reader = new ClassReader(input);
1.85 + ClassWriter writer = new ClassWriter(reader, 0);
1.86 +
1.87 + reader.accept(
1.88 + new ClassVisitor(ASM4, writer) {
1.89 + @Override
1.90 + public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions
1.91 + ) {
1.92 + MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
1.93 + return new InvokeDynamicProcessor(mv);
1.94 + }
1.95 + },
1.96 + 0);
1.97 + input.close();
1.98 + final byte[] invokeDynamicBytes = writer.toByteArray();
1.99 + ClassLoader l = new ClassLoader() {
1.100 + @Override
1.101 + public Class<?> loadClass(String name) throws ClassNotFoundException {
1.102 + if (name.equals(InvokeDynamic.class.getName())) {
1.103 + return defineClass(name, invokeDynamicBytes, 0, invokeDynamicBytes.length);
1.104 + }
1.105 + return super.loadClass(name);
1.106 + }
1.107 + };
1.108 + invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
1.109 + }
1.110 +
1.111 +
1.112 + private static class InvokeDynamicProcessor extends MethodVisitor {
1.113 + InvokeDynamicProcessor(MethodVisitor mv) {
1.114 + super(ASM4, mv);
1.115 + }
1.116 +
1.117 + @Override
1.118 + public void visitMethodInsn(int opcode, String owner, String name, String desc) {
1.119 + if (opcode == INVOKESTATIC) {
1.120 + if (name.startsWith("TEST_dynamic_")) {
1.121 + final String shortName = name.substring(13);
1.122 + Handle mh = new Handle(
1.123 + Opcodes.H_INVOKESTATIC, owner, shortName,
1.124 + MethodType.methodType(
1.125 + CallSite.class,
1.126 + MethodHandles.Lookup.class,
1.127 + String.class,
1.128 + MethodType.class
1.129 + ).toMethodDescriptorString()
1.130 + );
1.131 + super.visitInvokeDynamicInsn(shortName, desc, mh);
1.132 + return;
1.133 + }
1.134 + }
1.135 + super.visitMethodInsn(opcode, owner, name, desc);
1.136 + }
1.137 + }
1.138 +
1.139 +
1.140 +}