1 /*
   2  * Copyright (c) 2013, 2018, 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 vm.mlvm.indy.func.jvmti.mergeCP_indy2none_b;
  25 
  26 import java.lang.invoke.CallSite;
  27 import java.lang.invoke.ConstantCallSite;
  28 import java.lang.invoke.MethodHandle;
  29 import java.lang.invoke.MethodHandles;
  30 import java.lang.invoke.MethodType;
  31 
  32 import vm.mlvm.share.MlvmTest;
  33 
  34 public class INDIFY_Dummy0 {
  35 
  36     private static MethodType MT_bootstrap() {
  37         return MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class);
  38     }
  39 
  40     private static MethodHandle MH_bootstrap() throws NoSuchMethodException, IllegalAccessException  {
  41         return MethodHandles.lookup().findStatic(
  42                 INDIFY_Dummy0.class,
  43                 "bootstrap",
  44                 MT_bootstrap());
  45     }
  46 
  47     public static CallSite bootstrap(MethodHandles.Lookup l, String name, MethodType mt) throws Throwable {
  48         MlvmTest.getLog().display("Original bootstrap(): Lookup " + l + "; method name = " + name + "; method type = " + mt);
  49         CallSite cs = new ConstantCallSite(l.findStatic(INDIFY_Dummy0.class, "target", mt));
  50         return cs;
  51     }
  52 
  53     public static Boolean target(Object o, String s, int i) {
  54         redefineNow();
  55         throw new RuntimeException("Original target method was called. Test failed.");
  56     }
  57 
  58     public static void redefineNow() {}
  59 
  60     private static MethodHandle INDY_call;
  61     private static MethodHandle INDY_call() throws Throwable {
  62         if (INDY_call != null)
  63             return INDY_call;
  64 
  65         CallSite cs = (CallSite) MH_bootstrap().invokeWithArguments(
  66                 MethodHandles.lookup(),
  67                 "greet",
  68                 MethodType.methodType(Boolean.class, Object.class, String.class, int.class));
  69 
  70         return cs.dynamicInvoker();
  71     }
  72 
  73     public static boolean invokeTarget() throws Throwable {
  74         return invokeTarget0();
  75     }
  76 
  77     private static boolean invokeTarget0() throws Throwable {
  78         Object o = new Object();
  79         String s = "Original";
  80         int i = 456;
  81         return (Boolean) INDY_call().invokeExact(o, s, i);
  82     }
  83 
  84     public static boolean isRedefinedClass() {
  85         return false;
  86     }
  87 }