Path: blob/master/test/jdk/java/lang/invoke/7157574/Test7157574.java
41153 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*247157574 method handles returned by reflective lookup API sometimes have wrong receiver type2526When an inherited non-static field or method is looked up in a class C using Lookup.findVirtual(C...), etc., the JSR 292 API, the first argument of the resulting method handle must be the receiver ('this'), and must be the requested class (or more specific, in the case of findSpecial or a lookup of a protected method).2728But currently, if a supertype T defines the looked-up method or field and C inherits it, the returned method handle might have the more specific initial type T.2930The relevant javadoc (and 292 spec.) is as follows:31* The formal parameter {@code this} stands for the self-reference of type {@code C};32* if it is present, it is always the leading argument to the method handle invocation.33* (In the case of some {@code protected} members, {@code this} may be34* restricted in type to the lookup class; see below.)3536Because of this bug, all of the assertions fail in the following example:37*/3839/* @test40* @bug 715757441* @summary method handles returned by reflective lookup API sometimes have wrong receiver type42*43* @run main Test715757444*/4546import java.lang.invoke.*;47import static java.lang.invoke.MethodHandles.*;48import static java.lang.invoke.MethodType.*;49public class Test7157574 {50interface Intf { void ig1(); void ig2(); void ig3(); void ig4(); void m1(); }51abstract static class Super implements Intf { public abstract void m2(); public int f2; }52abstract static class Sub extends Super { }53public static void main(String... av) throws Throwable {54MethodHandle m1 = lookup().findVirtual(Sub.class, "m1", methodType(void.class));55System.out.println(m1);56MethodHandle m2 = lookup().findVirtual(Sub.class, "m2", methodType(void.class));57System.out.println(m2);58MethodHandle f2 = lookup().findGetter(Sub.class, "f2", int.class);59System.out.println(f2);60MethodHandle f2s = lookup().findSetter(Sub.class, "f2", int.class);61System.out.println(f2s);62MethodHandle chc = lookup().findVirtual(Sub.class, "hashCode", methodType(int.class));63System.out.println(chc);64MethodHandle ihc = lookup().findVirtual(Intf.class, "hashCode", methodType(int.class));65System.out.println(ihc);66assertEquals(Sub.class, m1.type().parameterType(0));67assertEquals(Sub.class, m2.type().parameterType(0));68assertEquals(Sub.class, f2.type().parameterType(0));69assertEquals(Sub.class, f2s.type().parameterType(0));70assertEquals(Sub.class, chc.type().parameterType(0));71assertEquals(Intf.class, ihc.type().parameterType(0));72// test the MHs on a concrete version of Sub73class C extends Sub {74public void m1() { this.f2 = -1; }75public void m2() { this.f2 = -2; }76// Pack the vtable of Intf with leading junk:77private void ig() { throw new RuntimeException(); }78public void ig1() { ig(); }79public void ig2() { ig(); }80public void ig3() { ig(); }81public void ig4() { ig(); }82}83testConcrete(new C(), m1, m2, f2, f2s, chc, ihc);84}85private static void testConcrete(Sub s,86MethodHandle m1, MethodHandle m2,87MethodHandle f2, MethodHandle f2s,88MethodHandle chc, MethodHandle ihc89) throws Throwable {90s.f2 = 0;91m1.invokeExact(s);92assertEquals(-1, s.f2);93m2.invokeExact(s);94assertEquals(-2, s.f2);95s.f2 = 2;96assertEquals(2, (int) f2.invokeExact(s));97f2s.invokeExact(s, 0);98assertEquals(0, s.f2);99assertEquals(s.hashCode(), (int) chc.invokeExact(s));100assertEquals(s.hashCode(), (int) ihc.invokeExact((Intf)s));101}102103private static void assertEquals(Object expect, Object observe) {104if (java.util.Objects.equals(expect, observe)) return;105String msg = ("expected "+expect+" but observed "+observe);106System.out.println("FAILED: "+msg);107throw new AssertionError(msg);108}109}110111112