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