rt/emul/compact/src/test/java/org/apidesign/vm4brwsr/dynamic/InvokeDynamicTest.java
1.1 --- a/rt/emul/compact/src/test/java/org/apidesign/vm4brwsr/dynamic/InvokeDynamicTest.java Sun Aug 10 07:02:12 2014 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,175 +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.dynamic;
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.apidesign.vm4brwsr.Bck2Brwsr;
1.33 -import org.objectweb.asm.ClassReader;
1.34 -import org.objectweb.asm.ClassVisitor;
1.35 -import org.objectweb.asm.ClassWriter;
1.36 -import org.objectweb.asm.Handle;
1.37 -import org.objectweb.asm.MethodVisitor;
1.38 -import org.objectweb.asm.Opcodes;
1.39 -import static org.objectweb.asm.Opcodes.ASM4;
1.40 -import static org.objectweb.asm.Opcodes.INVOKESTATIC;
1.41 -import static org.testng.Assert.*;
1.42 -import org.testng.annotations.AfterClass;
1.43 -import org.testng.annotations.BeforeClass;
1.44 -import org.testng.annotations.Test;
1.45 -
1.46 -/**
1.47 - *
1.48 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.49 - */
1.50 -public class InvokeDynamicTest {
1.51 - private static Class<?> invokeDynamicClass;
1.52 - private static byte[] invokeDynamicBytes;
1.53 - private static TestVM code;
1.54 -
1.55 - @Test public void simpleDynamicInJava() throws Exception {
1.56 - Method m = invokeDynamicClass.getMethod("dynamicSay");
1.57 - Object ret = m.invoke(m);
1.58 - assertEquals(ret, "Hello from Dynamic!");
1.59 - }
1.60 -
1.61 - @Test public void simpleDynamicInJS() throws Exception {
1.62 - code.assertExec(
1.63 - "Invoke dynamic can return a value", InvokeDynamic.class,
1.64 - "dynamic__Ljava_lang_String_2",
1.65 - "Hello from Dynamic!"
1.66 - );
1.67 - }
1.68 -
1.69 -
1.70 - @AfterClass
1.71 - public static void releaseTheCode() {
1.72 - code = null;
1.73 - }
1.74 -
1.75 - //
1.76 - // the following code is inspired by
1.77 - // https://code.google.com/p/indy-maven-plugin/
1.78 - // which I don't want to use, as it is not in a public repository
1.79 - //
1.80 - @BeforeClass
1.81 - public static void prepareClass() throws Exception {
1.82 - InputStream is = InvokeDynamic.class.getResourceAsStream("InvokeDynamic.class");
1.83 - assertNotNull(is, "Class found");
1.84 -
1.85 - ClassReader reader = new ClassReader(is);
1.86 - ClassWriter writer = new ClassWriter(reader, 0);
1.87 -
1.88 - reader.accept(
1.89 - new ClassVisitor(ASM4, writer) {
1.90 - @Override
1.91 - public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions
1.92 - ) {
1.93 - MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
1.94 - return new InvokeDynamicProcessor(mv);
1.95 - }
1.96 - },
1.97 - 0);
1.98 - is.close();
1.99 - invokeDynamicBytes = writer.toByteArray();
1.100 - ClassLoader l = new ClassLoader() {
1.101 - @Override
1.102 - public Class<?> loadClass(String name) throws ClassNotFoundException {
1.103 - if (name.equals(InvokeDynamic.class.getName())) {
1.104 - return defineClass(name, invokeDynamicBytes, 0, invokeDynamicBytes.length);
1.105 - }
1.106 - return super.loadClass(name);
1.107 - }
1.108 - };
1.109 - invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
1.110 -
1.111 - code = TestVM.compileClass(
1.112 - null, null, new EmulationResourcesWithException(),
1.113 - InvokeDynamic.class.getName().replace('.', '/')
1.114 - );
1.115 - }
1.116 -
1.117 -
1.118 - private static class InvokeDynamicProcessor extends MethodVisitor {
1.119 - InvokeDynamicProcessor(MethodVisitor mv) {
1.120 - super(ASM4, mv);
1.121 - }
1.122 -
1.123 - @Override
1.124 - public void visitMethodInsn(int opcode, String owner, String name, String desc) {
1.125 - if (opcode == INVOKESTATIC) {
1.126 - if (name.startsWith("TEST_dynamic_")) {
1.127 - final String shortName = name.substring(13);
1.128 - Handle mh = new Handle(
1.129 - Opcodes.H_INVOKESTATIC, owner, shortName,
1.130 - MethodType.methodType(
1.131 - CallSite.class,
1.132 - MethodHandles.Lookup.class,
1.133 - String.class,
1.134 - MethodType.class
1.135 - ).toMethodDescriptorString()
1.136 - );
1.137 - super.visitInvokeDynamicInsn(shortName, desc, mh);
1.138 - return;
1.139 - }
1.140 - }
1.141 - super.visitMethodInsn(opcode, owner, name, desc);
1.142 - }
1.143 - }
1.144 -
1.145 - private static class EmulationResourcesWithException implements Bck2Brwsr.Resources {
1.146 - @Override
1.147 - public InputStream get(String name) throws IOException {
1.148 - if ("org/apidesign/vm4brwsr/InvokeDynamic.class".equals(name)) {
1.149 - return new ByteArrayInputStream(invokeDynamicBytes);
1.150 - }
1.151 - if ("java/net/URI.class".equals(name)) {
1.152 - // skip
1.153 - return null;
1.154 - }
1.155 - if ("java/net/URLConnection.class".equals(name)) {
1.156 - // skip
1.157 - return null;
1.158 - }
1.159 - if ("java/lang/System.class".equals(name)) {
1.160 - // skip
1.161 - return null;
1.162 - }
1.163 - Enumeration<URL> en = InvokeDynamicTest.class.getClassLoader().getResources(name);
1.164 - URL u = null;
1.165 - while (en.hasMoreElements()) {
1.166 - u = en.nextElement();
1.167 - }
1.168 - if (u == null) {
1.169 - throw new IOException("Can't find " + name);
1.170 - }
1.171 - if (u.toExternalForm().contains("rt.jar!")) {
1.172 - throw new IOException("No emulation for " + u);
1.173 - }
1.174 - return u.openStream();
1.175 - }
1.176 - }
1.177 -
1.178 -}