1 /* /nodynamiccopyright/ */
   2 
   3 import java.lang.invoke.*;
   4 import java.lang.constant.*;
   5 import static java.lang.invoke.Intrinsics.*;
   6 
   7 /** Tests the use of MethodType.MethodTypeRef.of(Class<?> rtype) in combination with
   8  *  MethodHandle.findVirtual(Class<?> refc, String name, MethodType type)
   9  */
  10 public class FindVirtualTest01 extends ConstantFoldingTest {
  11     String foo() {
  12         return "invoking method FindVirtualTest01.foo()";
  13     }
  14 
  15     String bar(int i) {
  16         return "invoking method FindVirtualTest01.bar() with argument " + i;
  17     }
  18 
  19     public static void main(String[] args) throws Throwable {
  20         new FindVirtualTest01().run();
  21     }
  22 
  23     void run() throws Throwable {
  24         test1(this);
  25         test2(this);
  26         test2_1(this);
  27         test3(this);
  28         test4(this);
  29     }
  30 
  31     @InstructionInfo(bytecodePosition=0, values={"CONSTANT_MethodHandle_info", "REF_invokeVirtual"})
  32     void test1(FindVirtualTest01 f) throws Throwable {
  33         final MethodTypeDesc mt = MethodTypeDesc.of(ClassDesc.ofDescriptor("Ljava/lang/String;"));
  34         MethodHandle mh2 = ldc(MethodHandleDesc.of(DirectMethodHandleDesc.Kind.VIRTUAL, ClassDesc.ofDescriptor("LFindVirtualTest01;"), "foo", mt));
  35         check(mh2.invoke(f).toString().equals("invoking method FindVirtualTest01.foo()"));
  36     }
  37 
  38     @InstructionInfo(bytecodePosition=0, values={"CONSTANT_MethodHandle_info", "REF_invokeVirtual"})
  39     void test2(FindVirtualTest01 f) throws Throwable {
  40         final MethodTypeDesc mt = MethodTypeDesc.of(ClassDesc.ofDescriptor("Ljava/lang/String;"));
  41         final ClassDesc fooClass = ClassDesc.ofDescriptor("LFindVirtualTest01;");
  42         MethodHandle mh2 = ldc(MethodHandleDesc.of(DirectMethodHandleDesc.Kind.VIRTUAL, fooClass, "foo", mt));
  43         check(mh2.invoke(f).toString().equals("invoking method FindVirtualTest01.foo()"));
  44     }
  45 
  46     @InstructionInfo(bytecodePosition=0, values={"CONSTANT_MethodHandle_info", "REF_invokeVirtual"})
  47     void test2_1(FindVirtualTest01 f) throws Throwable {
  48         final MethodTypeDesc mt = MethodTypeDesc.of(ClassDesc.ofDescriptor("Ljava/lang/String;"));
  49         final ClassDesc fooClass = ClassDesc.ofDescriptor("LFindVirtualTest01;");
  50         MethodHandle mh2 = ldc(MethodHandleDesc.of(DirectMethodHandleDesc.Kind.VIRTUAL, fooClass, "foo", mt));
  51         check(mh2.invoke(f).toString().equals("invoking method FindVirtualTest01.foo()"));
  52     }
  53 
  54     @InstructionInfo(bytecodePosition=0, values={"CONSTANT_MethodHandle_info", "REF_invokeVirtual"})
  55     void test3(FindVirtualTest01 f) throws Throwable {
  56         MethodTypeDesc mt = MethodTypeDesc.of(ClassDesc.ofDescriptor("Ljava/lang/String;"));
  57         MethodHandle mh2 = ldc(MethodHandleDesc.of(DirectMethodHandleDesc.Kind.VIRTUAL, ClassDesc.ofDescriptor("LFindVirtualTest01;"), "foo", mt));
  58         check(mh2.invoke(f).toString().equals("invoking method FindVirtualTest01.foo()"));
  59     }
  60 
  61     @InstructionInfo(bytecodePosition=0, values={"CONSTANT_MethodHandle_info", "REF_invokeVirtual"})
  62     void test4(FindVirtualTest01 f) throws Throwable {
  63         MethodHandle mh2 = ldc(MethodHandleDesc.of(DirectMethodHandleDesc.Kind.VIRTUAL, ClassDesc.ofDescriptor("LFindVirtualTest01;"), "foo",
  64                                                    MethodTypeDesc.of(ClassDesc.ofDescriptor("Ljava/lang/String;"))));
  65         check(mh2.invoke(f).toString().equals("invoking method FindVirtualTest01.foo()"));
  66     }
  67 
  68     @InstructionInfo(bytecodePosition=0, values={"CONSTANT_MethodHandle_info", "REF_invokeVirtual"})
  69     void test5(FindVirtualTest01 f) throws Throwable {
  70         MethodHandle mhBar = ldc(MethodHandleDesc.of(DirectMethodHandleDesc.Kind.VIRTUAL, ClassDesc.ofDescriptor("LFindVirtualTest01;"), "bar",
  71                                                      MethodTypeDesc.of(ClassDesc.ofDescriptor("Ljava/lang/String;"), ConstantDescs.CR_int)));
  72         check(mhBar.invoke(f, 3).toString().equals("invoking method FindVirtualTest01.bar() with argument 3"));
  73     }
  74 }