1.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/InvokeDynamicTest.java Sun Aug 10 07:12:30 2014 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,174 +0,0 @@
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.ByteArrayInputStream;
1.24 -import java.io.IOException;
1.25 -import java.io.InputStream;
1.26 -import java.lang.invoke.CallSite;
1.27 -import java.lang.invoke.MethodHandles;
1.28 -import java.lang.invoke.MethodType;
1.29 -import java.lang.reflect.Method;
1.30 -import java.net.URL;
1.31 -import java.util.Enumeration;
1.32 -import org.objectweb.asm.ClassReader;
1.33 -import org.objectweb.asm.ClassVisitor;
1.34 -import org.objectweb.asm.ClassWriter;
1.35 -import org.objectweb.asm.Handle;
1.36 -import org.objectweb.asm.MethodVisitor;
1.37 -import org.objectweb.asm.Opcodes;
1.38 -import static org.objectweb.asm.Opcodes.ASM4;
1.39 -import static org.objectweb.asm.Opcodes.INVOKESTATIC;
1.40 -import static org.testng.Assert.*;
1.41 -import org.testng.annotations.AfterClass;
1.42 -import org.testng.annotations.BeforeClass;
1.43 -import org.testng.annotations.Test;
1.44 -
1.45 -/**
1.46 - *
1.47 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.48 - */
1.49 -public class InvokeDynamicTest {
1.50 - private static Class<?> invokeDynamicClass;
1.51 - private static byte[] invokeDynamicBytes;
1.52 - private static TestVM code;
1.53 -
1.54 - @Test public void simpleDynamicInJava() throws Exception {
1.55 - Method m = invokeDynamicClass.getMethod("dynamicSay");
1.56 - Object ret = m.invoke(m);
1.57 - assertEquals(ret, "Hello from Dynamic!");
1.58 - }
1.59 -
1.60 - @Test public void simpleDynamicInJS() throws Exception {
1.61 - code.assertExec(
1.62 - "Invoke dynamic can return a value", InvokeDynamic.class,
1.63 - "dynamic__Ljava_lang_String_2",
1.64 - "Hello from Dynamic!"
1.65 - );
1.66 - }
1.67 -
1.68 -
1.69 - @AfterClass
1.70 - public static void releaseTheCode() {
1.71 - code = null;
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 is = InvokeDynamic.class.getResourceAsStream("InvokeDynamic.class");
1.82 - assertNotNull(is, "Class found");
1.83 -
1.84 - ClassReader reader = new ClassReader(is);
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 - is.close();
1.98 - 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 - code = TestVM.compileClass(
1.111 - null, null, new EmulationResourcesWithException(),
1.112 - InvokeDynamic.class.getName().replace('.', '/')
1.113 - );
1.114 - }
1.115 -
1.116 -
1.117 - private static class InvokeDynamicProcessor extends MethodVisitor {
1.118 - InvokeDynamicProcessor(MethodVisitor mv) {
1.119 - super(ASM4, mv);
1.120 - }
1.121 -
1.122 - @Override
1.123 - public void visitMethodInsn(int opcode, String owner, String name, String desc) {
1.124 - if (opcode == INVOKESTATIC) {
1.125 - if (name.startsWith("TEST_dynamic_")) {
1.126 - final String shortName = name.substring(13);
1.127 - Handle mh = new Handle(
1.128 - Opcodes.H_INVOKESTATIC, owner, shortName,
1.129 - MethodType.methodType(
1.130 - CallSite.class,
1.131 - MethodHandles.Lookup.class,
1.132 - String.class,
1.133 - MethodType.class
1.134 - ).toMethodDescriptorString()
1.135 - );
1.136 - super.visitInvokeDynamicInsn(shortName, desc, mh);
1.137 - return;
1.138 - }
1.139 - }
1.140 - super.visitMethodInsn(opcode, owner, name, desc);
1.141 - }
1.142 - }
1.143 -
1.144 - private static class EmulationResourcesWithException implements Bck2Brwsr.Resources {
1.145 - @Override
1.146 - public InputStream get(String name) throws IOException {
1.147 - if ("org/apidesign/vm4brwsr/InvokeDynamic.class".equals(name)) {
1.148 - return new ByteArrayInputStream(invokeDynamicBytes);
1.149 - }
1.150 - if ("java/net/URI.class".equals(name)) {
1.151 - // skip
1.152 - return null;
1.153 - }
1.154 - if ("java/net/URLConnection.class".equals(name)) {
1.155 - // skip
1.156 - return null;
1.157 - }
1.158 - if ("java/lang/System.class".equals(name)) {
1.159 - // skip
1.160 - return null;
1.161 - }
1.162 - Enumeration<URL> en = InvokeDynamicTest.class.getClassLoader().getResources(name);
1.163 - URL u = null;
1.164 - while (en.hasMoreElements()) {
1.165 - u = en.nextElement();
1.166 - }
1.167 - if (u == null) {
1.168 - throw new IOException("Can't find " + name);
1.169 - }
1.170 - if (u.toExternalForm().contains("rt.jar!")) {
1.171 - throw new IOException("No emulation for " + u);
1.172 - }
1.173 - return u.openStream();
1.174 - }
1.175 - }
1.176 -
1.177 -}