1 /*
   2  * Copyright (c) 2014, 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.share;
  25 
  26 import java.lang.invoke.MethodHandle;
  27 import java.lang.invoke.MethodHandles;
  28 import java.lang.invoke.MethodType;
  29 import java.lang.reflect.InvocationTargetException;
  30 
  31 /**
  32  * WhiteBoxHelper class obtains a MethodHandle to a method defined in sun.hotspot.WhiteBox class.
  33  * If WhiteBox is not available or cannot be initialized, or the method is not available,
  34  * it throws an appropriate exception.
  35  *
  36  * This class was introduced to avoid "hard link" to WhiteBox.
  37  *
  38  */
  39 public class WhiteBoxHelper {
  40 
  41     /**
  42      * Obtains {@link java.lang.invoke.MethodHandle} to a method in sun.hotspot.WhiteBox class.
  43      * If there is an error obtaining method handle, an exception is thrown.
  44      *
  45      * @param name Method name
  46      * @type Method type
  47      * @return {@link java.lang.invoke.MethodHandle} to the method. You can call it directly, WhiteBox instance is already bound to the handle.
  48      * @throws IllegalAccessException if method cannot be accessed (see {@link java.lang.invoke.MethodHandles.Lookup#findStatic()} for details)
  49      * @throws NoSuchMethodException if method cannot be found (see {@link java.lang.invoke.MethodHandles.Lookup#findStatic()} for details)
  50      * @throws ClassNotFoundException if WhiteBox class cannot be loaded
  51      * @throws InvocationTargetException if WhiteBox.getWhiteBox() method throws exception for some reason
  52      */
  53     public static MethodHandle getMethod(String name, MethodType type)
  54             throws IllegalAccessException, NoSuchMethodException, ClassNotFoundException, InvocationTargetException {
  55 
  56         Class<?> wbClass = Class.forName("sun.hotspot.WhiteBox");
  57         MethodHandles.Lookup lu = MethodHandles.lookup();
  58         MethodHandle getWB = lu.findStatic(wbClass, "getWhiteBox", MethodType.methodType(wbClass));
  59         Object wb;
  60         try {
  61             wb = getWB.invoke();
  62         } catch (Throwable e) {
  63             throw new InvocationTargetException(e, "Can't obtain WhiteBox instance");
  64         }
  65         return lu.findVirtual(wbClass, name, type).bindTo(wb);
  66     }
  67 
  68 }