1 /*
   2  * Copyright (c) 2011, 2016, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import jdk.internal.misc.Unsafe;
  29 import sun.security.action.GetPropertyAction;
  30 
  31 import java.util.Properties;
  32 
  33 /**
  34  * This class consists exclusively of static names internal to the
  35  * method handle implementation.
  36  * Usage:  {@code import static java.lang.invoke.MethodHandleStatics.*}
  37  * @author John Rose, JSR 292 EG
  38  */
  39 /*non-public*/
  40 class MethodHandleStatics {
  41 
  42     private MethodHandleStatics() { }  // do not instantiate
  43 
  44     static final Unsafe UNSAFE = Unsafe.getUnsafe();
  45 
  46     static final boolean DEBUG_METHOD_HANDLE_NAMES;
  47     static final boolean DUMP_CLASS_FILES;
  48     static final boolean TRACE_INTERPRETER;
  49     static final boolean TRACE_METHOD_LINKAGE;
  50     static final boolean TRACE_RESOLVE;
  51     static final int COMPILE_THRESHOLD;
  52     static final boolean LOG_LF_COMPILATION_FAILURE;
  53     static final int DONT_INLINE_THRESHOLD;
  54     static final int PROFILE_LEVEL;
  55     static final boolean PROFILE_GWT;
  56     static final int CUSTOMIZE_THRESHOLD;
  57     static final boolean VAR_HANDLE_GUARDS;
  58     static final int MAX_ARITY;
  59     static final boolean VAR_HANDLE_IDENTITY_ADAPT;
  60 
  61     static {
  62         Properties props = GetPropertyAction.privilegedGetProperties();
  63         DEBUG_METHOD_HANDLE_NAMES = Boolean.parseBoolean(
  64                 props.getProperty("java.lang.invoke.MethodHandle.DEBUG_NAMES"));
  65         DUMP_CLASS_FILES = Boolean.parseBoolean(
  66                 props.getProperty("java.lang.invoke.MethodHandle.DUMP_CLASS_FILES"));
  67         TRACE_INTERPRETER = Boolean.parseBoolean(
  68                 props.getProperty("java.lang.invoke.MethodHandle.TRACE_INTERPRETER"));
  69         TRACE_METHOD_LINKAGE = Boolean.parseBoolean(
  70                 props.getProperty("java.lang.invoke.MethodHandle.TRACE_METHOD_LINKAGE"));
  71         TRACE_RESOLVE = Boolean.parseBoolean(
  72                 props.getProperty("java.lang.invoke.MethodHandle.TRACE_RESOLVE"));
  73         COMPILE_THRESHOLD = Integer.parseInt(
  74                 props.getProperty("java.lang.invoke.MethodHandle.COMPILE_THRESHOLD", "0"));
  75         LOG_LF_COMPILATION_FAILURE = Boolean.parseBoolean(
  76                 props.getProperty("java.lang.invoke.MethodHandle.LOG_LF_COMPILATION_FAILURE", "false"));
  77         DONT_INLINE_THRESHOLD = Integer.parseInt(
  78                 props.getProperty("java.lang.invoke.MethodHandle.DONT_INLINE_THRESHOLD", "30"));
  79         PROFILE_LEVEL = Integer.parseInt(
  80                 props.getProperty("java.lang.invoke.MethodHandle.PROFILE_LEVEL", "0"));
  81         PROFILE_GWT = Boolean.parseBoolean(
  82                 props.getProperty("java.lang.invoke.MethodHandle.PROFILE_GWT", "true"));
  83         CUSTOMIZE_THRESHOLD = Integer.parseInt(
  84                 props.getProperty("java.lang.invoke.MethodHandle.CUSTOMIZE_THRESHOLD", "127"));
  85         VAR_HANDLE_GUARDS = Boolean.parseBoolean(
  86                 props.getProperty("java.lang.invoke.VarHandle.VAR_HANDLE_GUARDS", "true"));
  87         VAR_HANDLE_IDENTITY_ADAPT = Boolean.parseBoolean(
  88                 props.getProperty("java.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT", "false"));
  89 
  90         // Do not adjust this except for special platforms:
  91         MAX_ARITY = Integer.parseInt(
  92                 props.getProperty("java.lang.invoke.MethodHandleImpl.MAX_ARITY", "255"));
  93 
  94         if (CUSTOMIZE_THRESHOLD < -1 || CUSTOMIZE_THRESHOLD > 127) {
  95             throw newInternalError("CUSTOMIZE_THRESHOLD should be in [-1...127] range");
  96         }
  97     }
  98 
  99     /** Tell if any of the debugging switches are turned on.
 100      *  If this is the case, it is reasonable to perform extra checks or save extra information.
 101      */
 102     /*non-public*/
 103     static boolean debugEnabled() {
 104         return (DEBUG_METHOD_HANDLE_NAMES |
 105                 DUMP_CLASS_FILES |
 106                 TRACE_INTERPRETER |
 107                 TRACE_METHOD_LINKAGE |
 108                 LOG_LF_COMPILATION_FAILURE);
 109     }
 110 
 111     // handy shared exception makers (they simplify the common case code)
 112     /*non-public*/
 113     static InternalError newInternalError(String message) {
 114         return new InternalError(message);
 115     }
 116     /*non-public*/
 117     static InternalError newInternalError(String message, Exception cause) {
 118         return new InternalError(message, cause);
 119     }
 120     /*non-public*/
 121     static InternalError newInternalError(Exception cause) {
 122         return new InternalError(cause);
 123     }
 124     /*non-public*/
 125     static RuntimeException newIllegalStateException(String message) {
 126         return new IllegalStateException(message);
 127     }
 128     /*non-public*/
 129     static RuntimeException newIllegalStateException(String message, Object obj) {
 130         return new IllegalStateException(message(message, obj));
 131     }
 132     /*non-public*/
 133     static RuntimeException newIllegalArgumentException(String message) {
 134         return new IllegalArgumentException(message);
 135     }
 136     /*non-public*/
 137     static RuntimeException newIllegalArgumentException(String message, Object obj) {
 138         return new IllegalArgumentException(message(message, obj));
 139     }
 140     /*non-public*/
 141     static RuntimeException newIllegalArgumentException(String message, Object obj, Object obj2) {
 142         return new IllegalArgumentException(message(message, obj, obj2));
 143     }
 144     /** Propagate unchecked exceptions and errors, but wrap anything checked and throw that instead. */
 145     /*non-public*/
 146     static Error uncaughtException(Throwable ex) {
 147         if (ex instanceof Error)  throw (Error) ex;
 148         if (ex instanceof RuntimeException)  throw (RuntimeException) ex;
 149         throw new InternalError("uncaught exception", ex);
 150     }
 151     private static String message(String message, Object obj) {
 152         if (obj != null)  message = message + ": " + obj;
 153         return message;
 154     }
 155     private static String message(String message, Object obj, Object obj2) {
 156         if (obj != null || obj2 != null)  message = message + ": " + obj + ", " + obj2;
 157         return message;
 158     }
 159     /*non-public*/
 160     static void rangeCheck2(int start, int end, int size) {
 161         if (0 > start || start > end || end > size)
 162             throw new IndexOutOfBoundsException(start+".."+end);
 163     }
 164     /*non-public*/
 165     static int rangeCheck1(int index, int size) {
 166         if (0 > index || index >= size)
 167             throw new IndexOutOfBoundsException(index);
 168         return index;
 169     }
 170 }