jdk/test/java/lang/invoke/7157574/Test7157574.java
changeset 13423 17843fff200d
child 32649 2ee9017c7597
equal deleted inserted replaced
13422:72b63ee33f57 13423:17843fff200d
       
     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 
       
    25 /*
       
    26 7157574 method handles returned by reflective lookup API sometimes have wrong receiver type
       
    27 
       
    28 When 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).
       
    29 
       
    30 But 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.
       
    31 
       
    32 The relevant javadoc (and 292 spec.) is as follows:
       
    33     * The formal parameter {@code this} stands for the self-reference of type {@code C};
       
    34     * if it is present, it is always the leading argument to the method handle invocation.
       
    35     * (In the case of some {@code protected} members, {@code this} may be
       
    36     * restricted in type to the lookup class; see below.)
       
    37 
       
    38 Because of this bug, all of the assertions fail in the following example:
       
    39 */
       
    40 
       
    41 /* @test
       
    42  * @bug 7157574
       
    43  * @summary method handles returned by reflective lookup API sometimes have wrong receiver type
       
    44  *
       
    45  * @run main Test7157574
       
    46  */
       
    47 
       
    48 import java.lang.invoke.*;
       
    49 import static java.lang.invoke.MethodHandles.*;
       
    50 import static java.lang.invoke.MethodType.*;
       
    51 public class Test7157574 {
       
    52     interface Intf { void ig1(); void ig2(); void ig3(); void ig4(); void m1(); }
       
    53     static abstract class Super implements Intf { public abstract void m2(); public int f2; }
       
    54     static abstract class Sub extends Super { }
       
    55     public static void main(String... av) throws Throwable {
       
    56         MethodHandle m1 = lookup().findVirtual(Sub.class, "m1", methodType(void.class));
       
    57         System.out.println(m1);
       
    58         MethodHandle m2 = lookup().findVirtual(Sub.class, "m2", methodType(void.class));
       
    59         System.out.println(m2);
       
    60         MethodHandle f2 = lookup().findGetter(Sub.class, "f2", int.class);
       
    61         System.out.println(f2);
       
    62         MethodHandle f2s = lookup().findSetter(Sub.class, "f2", int.class);
       
    63         System.out.println(f2s);
       
    64         MethodHandle chc = lookup().findVirtual(Sub.class,  "hashCode", methodType(int.class));
       
    65         System.out.println(chc);
       
    66         MethodHandle ihc = lookup().findVirtual(Intf.class, "hashCode", methodType(int.class));
       
    67         System.out.println(ihc);
       
    68         assertEquals(Sub.class, m1.type().parameterType(0));
       
    69         assertEquals(Sub.class, m2.type().parameterType(0));
       
    70         assertEquals(Sub.class, f2.type().parameterType(0));
       
    71         assertEquals(Sub.class, f2s.type().parameterType(0));
       
    72         assertEquals(Sub.class, chc.type().parameterType(0));
       
    73         assertEquals(Intf.class, ihc.type().parameterType(0));
       
    74         // test the MHs on a concrete version of Sub
       
    75         class C extends Sub {
       
    76             public void m1() { this.f2 = -1; }
       
    77             public void m2() { this.f2 = -2; }
       
    78             // Pack the vtable of Intf with leading junk:
       
    79             private void ig() { throw new RuntimeException(); }
       
    80             public void ig1() { ig(); }
       
    81             public void ig2() { ig(); }
       
    82             public void ig3() { ig(); }
       
    83             public void ig4() { ig(); }
       
    84         }
       
    85         testConcrete(new C(), m1, m2, f2, f2s, chc, ihc);
       
    86     }
       
    87     private static void testConcrete(Sub s,
       
    88                                      MethodHandle m1, MethodHandle m2,
       
    89                                      MethodHandle f2, MethodHandle f2s,
       
    90                                      MethodHandle chc, MethodHandle ihc
       
    91                                      ) throws Throwable {
       
    92         s.f2 = 0;
       
    93         m1.invokeExact(s);
       
    94         assertEquals(-1, s.f2);
       
    95         m2.invokeExact(s);
       
    96         assertEquals(-2, s.f2);
       
    97         s.f2 = 2;
       
    98         assertEquals(2, (int) f2.invokeExact(s));
       
    99         f2s.invokeExact(s, 0);
       
   100         assertEquals(0, s.f2);
       
   101         assertEquals(s.hashCode(), (int) chc.invokeExact(s));
       
   102         assertEquals(s.hashCode(), (int) ihc.invokeExact((Intf)s));
       
   103     }
       
   104 
       
   105     private static void assertEquals(Object expect, Object observe) {
       
   106         if (java.util.Objects.equals(expect, observe))  return;
       
   107         String msg = ("expected "+expect+" but observed "+observe);
       
   108         System.out.println("FAILED: "+msg);
       
   109         throw new AssertionError(msg);
       
   110     }
       
   111 }