test/jdk/java/lang/invoke/findSpecial/m1/test/FindSpecial.java
changeset 57735 7ba5e49258de
equal deleted inserted replaced
57734:18f4d3d46d54 57735:7ba5e49258de
       
     1 /*
       
     2  * Copyright (c) 2019, 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 package test;
       
    25 
       
    26 import java.lang.invoke.MethodHandle;
       
    27 import java.lang.invoke.MethodHandles;
       
    28 import java.lang.invoke.MethodHandles.Lookup;
       
    29 import java.lang.invoke.MethodType;
       
    30 import java.lang.reflect.InvocationTargetException;
       
    31 import java.lang.reflect.Method;
       
    32 import java.util.Comparator;
       
    33 
       
    34 /*
       
    35  * java.util.Comparator is in java.base.  MyComparator is a Comparator
       
    36  * subclass and it's in a different module.
       
    37  *
       
    38  * Test findSpecial and unreflectSpecial with Comparator and MyComparator
       
    39  * as the special caller.
       
    40  */
       
    41 public class FindSpecial {
       
    42     private static final Lookup LOOKUP = MethodHandles.lookup();
       
    43 
       
    44     public static void main(String... args) throws Throwable {
       
    45         findSpecialTest();
       
    46         unreflectSpecialTest();
       
    47         reflectMethodInvoke();
       
    48     }
       
    49 
       
    50     static void findSpecialTest() throws Throwable {
       
    51         Method m = Comparator.class.getMethod("reversed");
       
    52         MethodType mt = MethodType.methodType(m.getReturnType(), m.getParameterTypes());
       
    53         // refc and specialCaller are both in java.base
       
    54         MethodHandle mh = LOOKUP.findSpecial(Comparator.class, m.getName(), mt, Comparator.class);
       
    55         // refc in java.base, specialCaller in this module
       
    56         MethodHandle mh1 = LOOKUP.findSpecial(m.getDeclaringClass(), m.getName(), mt,
       
    57                                                 MyComparator.class);
       
    58         Comparator<Object> cmp = new MyComparator();
       
    59         // invokespecial to invoke Comparator::reversed.
       
    60         Object o = mh.invoke(cmp);
       
    61         Object o1 = mh1.invoke(cmp);
       
    62     }
       
    63 
       
    64     static void unreflectSpecialTest() throws Throwable {
       
    65         Method m = Comparator.class.getMethod("reversed");
       
    66         // refc and specialCaller are both in java.base
       
    67         MethodHandle mh = LOOKUP.unreflectSpecial(m, Comparator.class);
       
    68         // refc in java.base, specialCaller in this module
       
    69         MethodHandle mh1 = LOOKUP.unreflectSpecial(m, MyComparator.class);
       
    70         Comparator<Object> cmp = new MyComparator();
       
    71         // invokespecial to invoke Comparator::reversed.
       
    72         Object o = mh.invoke(cmp);
       
    73         Object o1 = mh1.invoke(cmp);
       
    74     }
       
    75 
       
    76     static void reflectMethodInvoke() throws Throwable {
       
    77         Method m = Comparator.class.getMethod("reversed");
       
    78         try {
       
    79             // invokevirtual dispatch
       
    80             Object o = m.invoke(new MyComparator());
       
    81             throw new RuntimeException("should throw an exception");
       
    82         } catch (InvocationTargetException e) {
       
    83             if (!(e.getCause() instanceof Error &&
       
    84                   e.getCause().getMessage().equals("should not reach here"))) {
       
    85                 throw e.getCause();
       
    86             }
       
    87         }
       
    88     }
       
    89 
       
    90     static class MyComparator implements Comparator<Object> {
       
    91         public int compare(Object o1, Object o2) {
       
    92             return 0;
       
    93         }
       
    94 
       
    95         @Override
       
    96         public Comparator<Object> reversed() {
       
    97             throw new Error("should not reach here");
       
    98         }
       
    99     }
       
   100 }
       
   101