changeset 7632:a657ad5273ba

More tests for bad method handles
author briangoetz
date Wed, 13 Mar 2013 18:45:25 -0400
parents aa40e0a648e7
children b957521c820d
files test-ng/tests/org/openjdk/tests/java/lang/invoke/GenBadMHTest.java
diffstat 1 files changed, 111 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-ng/tests/org/openjdk/tests/java/lang/invoke/GenBadMHTest.java	Wed Mar 13 18:45:25 2013 -0400
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.tests.java.lang.invoke;
+
+import jdk.internal.org.objectweb.asm.ClassWriter;
+import jdk.internal.org.objectweb.asm.Handle;
+import jdk.internal.org.objectweb.asm.MethodVisitor;
+import jdk.internal.org.objectweb.asm.Opcodes;
+import org.testng.annotations.Test;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandleInfo;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Supplier;
+
+import static jdk.internal.org.objectweb.asm.Opcodes.*;
+import static jdk.internal.org.objectweb.asm.Opcodes.PUTFIELD;
+import static jdk.internal.org.objectweb.asm.Opcodes.RETURN;
+import static org.testng.Assert.assertTrue;
+
+/**
+ * GenBadMHTest
+ *
+ * @author Brian Goetz
+ */
+@Test
+public class GenBadMHTest {
+    public void testMHForStringToUpper() throws IOException, ReflectiveOperationException {
+        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
+        cw.visit(51, ACC_SUPER + ACC_PUBLIC, "Foo", null, "java/lang/Object", new String[0]);
+
+        // Constructor
+        MethodVisitor ctor = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
+        ctor.visitCode();
+        ctor.visitVarInsn(ALOAD, 0);
+        ctor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
+        ctor.visitInsn(RETURN);
+        ctor.visitMaxs(-1, -1);
+        ctor.visitEnd();
+
+        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "foo", "()Ljava/lang/Object;", null, null);
+        mv.visitLdcInsn(new Handle(Opcodes.H_INVOKEVIRTUAL, "java/lang/String", "toUpperCase", "()Ljava/lang/String;"));
+        mv.visitInsn(ARETURN);
+        mv.visitMaxs(-1, -1);
+        mv.visitEnd();
+
+        cw.visitEnd();
+
+        byte[] bytes = cw.toByteArray();
+        File f = new File("Foo.class");
+        try (FileOutputStream fos = new FileOutputStream(f)) {
+            fos.write(bytes);
+        }
+
+        List<URL> list = new ArrayList<>();
+        list.add(new URL("file:" + f.getCanonicalFile().getParent().replace("\\", "/") + "/"));
+        Class<?> clazz = Class.forName("Foo", true, new URLClassLoader(list.toArray(new URL[list.size()])));
+        Method m = clazz.getMethod("foo");
+        MethodHandle mh = (MethodHandle) m.invoke(clazz.newInstance());
+        MethodHandleInfo mhi = new MethodHandleInfo(mh);
+        assertTrue(mhi.getReferenceKind() == MethodHandleInfo.REF_invokeVirtual);
+    }
+
+    public void testPrivateInnerCtorRef() {
+        Supplier<Object> ctor = new Enclosing().foo();
+        Enclosing.Inner inner = (Enclosing.Inner) ctor.get();
+    }
+
+    static class Enclosing {
+
+        public Supplier<Object> foo() {
+            return Inner::new;
+        }
+
+        private class Inner {
+            private Inner() {}
+        }
+    }
+}