Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/7157574/Test7157574.java
47544 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*22*/2324/*257157574 method handles returned by reflective lookup API sometimes have wrong receiver type2627When 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).2829But 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.3031The relevant javadoc (and 292 spec.) is as follows:32* The formal parameter {@code this} stands for the self-reference of type {@code C};33* if it is present, it is always the leading argument to the method handle invocation.34* (In the case of some {@code protected} members, {@code this} may be35* restricted in type to the lookup class; see below.)3637Because of this bug, all of the assertions fail in the following example:38*/3940/* @test41* @bug 715757442* @summary method handles returned by reflective lookup API sometimes have wrong receiver type43*44* @run main Test715757445*/4647import java.lang.invoke.*;48import static java.lang.invoke.MethodHandles.*;49import static java.lang.invoke.MethodType.*;50public class Test7157574 {51interface Intf { void ig1(); void ig2(); void ig3(); void ig4(); void m1(); }52static abstract class Super implements Intf { public abstract void m2(); public int f2; }53static abstract class Sub extends Super { }54public static void main(String... av) throws Throwable {55MethodHandle m1 = lookup().findVirtual(Sub.class, "m1", methodType(void.class));56System.out.println(m1);57MethodHandle m2 = lookup().findVirtual(Sub.class, "m2", methodType(void.class));58System.out.println(m2);59MethodHandle f2 = lookup().findGetter(Sub.class, "f2", int.class);60System.out.println(f2);61MethodHandle f2s = lookup().findSetter(Sub.class, "f2", int.class);62System.out.println(f2s);63MethodHandle chc = lookup().findVirtual(Sub.class, "hashCode", methodType(int.class));64System.out.println(chc);65MethodHandle ihc = lookup().findVirtual(Intf.class, "hashCode", methodType(int.class));66System.out.println(ihc);67assertEquals(Sub.class, m1.type().parameterType(0));68assertEquals(Sub.class, m2.type().parameterType(0));69assertEquals(Sub.class, f2.type().parameterType(0));70assertEquals(Sub.class, f2s.type().parameterType(0));71assertEquals(Sub.class, chc.type().parameterType(0));72assertEquals(Intf.class, ihc.type().parameterType(0));73// test the MHs on a concrete version of Sub74class C extends Sub {75public void m1() { this.f2 = -1; }76public void m2() { this.f2 = -2; }77// Pack the vtable of Intf with leading junk:78private void ig() { throw new RuntimeException(); }79public void ig1() { ig(); }80public void ig2() { ig(); }81public void ig3() { ig(); }82public void ig4() { ig(); }83}84testConcrete(new C(), m1, m2, f2, f2s, chc, ihc);85}86private static void testConcrete(Sub s,87MethodHandle m1, MethodHandle m2,88MethodHandle f2, MethodHandle f2s,89MethodHandle chc, MethodHandle ihc90) throws Throwable {91s.f2 = 0;92m1.invokeExact(s);93assertEquals(-1, s.f2);94m2.invokeExact(s);95assertEquals(-2, s.f2);96s.f2 = 2;97assertEquals(2, (int) f2.invokeExact(s));98f2s.invokeExact(s, 0);99assertEquals(0, s.f2);100assertEquals(s.hashCode(), (int) chc.invokeExact(s));101assertEquals(s.hashCode(), (int) ihc.invokeExact((Intf)s));102}103104private static void assertEquals(Object expect, Object observe) {105if (java.util.Objects.equals(expect, observe)) return;106String msg = ("expected "+expect+" but observed "+observe);107System.out.println("FAILED: "+msg);108throw new AssertionError(msg);109}110}111112113