Test7157574.java revision 15491:6f390eafc676
1/* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 257157574 method handles returned by reflective lookup API sometimes have wrong receiver type 26 27When 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). 28 29But 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. 30 31The 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 be 35 * restricted in type to the lookup class; see below.) 36 37Because of this bug, all of the assertions fail in the following example: 38*/ 39 40/* @test 41 * @bug 7157574 42 * @summary method handles returned by reflective lookup API sometimes have wrong receiver type 43 * 44 * @run main Test7157574 45 */ 46 47import java.lang.invoke.*; 48import static java.lang.invoke.MethodHandles.*; 49import static java.lang.invoke.MethodType.*; 50public class Test7157574 { 51 interface Intf { void ig1(); void ig2(); void ig3(); void ig4(); void m1(); } 52 abstract static class Super implements Intf { public abstract void m2(); public int f2; } 53 abstract static class Sub extends Super { } 54 public static void main(String... av) throws Throwable { 55 MethodHandle m1 = lookup().findVirtual(Sub.class, "m1", methodType(void.class)); 56 System.out.println(m1); 57 MethodHandle m2 = lookup().findVirtual(Sub.class, "m2", methodType(void.class)); 58 System.out.println(m2); 59 MethodHandle f2 = lookup().findGetter(Sub.class, "f2", int.class); 60 System.out.println(f2); 61 MethodHandle f2s = lookup().findSetter(Sub.class, "f2", int.class); 62 System.out.println(f2s); 63 MethodHandle chc = lookup().findVirtual(Sub.class, "hashCode", methodType(int.class)); 64 System.out.println(chc); 65 MethodHandle ihc = lookup().findVirtual(Intf.class, "hashCode", methodType(int.class)); 66 System.out.println(ihc); 67 assertEquals(Sub.class, m1.type().parameterType(0)); 68 assertEquals(Sub.class, m2.type().parameterType(0)); 69 assertEquals(Sub.class, f2.type().parameterType(0)); 70 assertEquals(Sub.class, f2s.type().parameterType(0)); 71 assertEquals(Sub.class, chc.type().parameterType(0)); 72 assertEquals(Intf.class, ihc.type().parameterType(0)); 73 // test the MHs on a concrete version of Sub 74 class C extends Sub { 75 public void m1() { this.f2 = -1; } 76 public void m2() { this.f2 = -2; } 77 // Pack the vtable of Intf with leading junk: 78 private void ig() { throw new RuntimeException(); } 79 public void ig1() { ig(); } 80 public void ig2() { ig(); } 81 public void ig3() { ig(); } 82 public void ig4() { ig(); } 83 } 84 testConcrete(new C(), m1, m2, f2, f2s, chc, ihc); 85 } 86 private static void testConcrete(Sub s, 87 MethodHandle m1, MethodHandle m2, 88 MethodHandle f2, MethodHandle f2s, 89 MethodHandle chc, MethodHandle ihc 90 ) throws Throwable { 91 s.f2 = 0; 92 m1.invokeExact(s); 93 assertEquals(-1, s.f2); 94 m2.invokeExact(s); 95 assertEquals(-2, s.f2); 96 s.f2 = 2; 97 assertEquals(2, (int) f2.invokeExact(s)); 98 f2s.invokeExact(s, 0); 99 assertEquals(0, s.f2); 100 assertEquals(s.hashCode(), (int) chc.invokeExact(s)); 101 assertEquals(s.hashCode(), (int) ihc.invokeExact((Intf)s)); 102 } 103 104 private static void assertEquals(Object expect, Object observe) { 105 if (java.util.Objects.equals(expect, observe)) return; 106 String msg = ("expected "+expect+" but observed "+observe); 107 System.out.println("FAILED: "+msg); 108 throw new AssertionError(msg); 109 } 110} 111