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 boolean CDS_TRACE_RESOLVE;
  52     static final int COMPILE_THRESHOLD;
  53     static final boolean LOG_LF_COMPILATION_FAILURE;
  54     static final int DONT_INLINE_THRESHOLD;
  55     static final int PROFILE_LEVEL;
  56     static final boolean PROFILE_GWT;
  57     static final int CUSTOMIZE_THRESHOLD;
  58     static final boolean VAR_HANDLE_GUARDS;
  59     static final int MAX_ARITY;
  60     static final boolean VAR_HANDLE_IDENTITY_ADAPT;
  61 
  62     static {
  63         Properties props = GetPropertyAction.privilegedGetProperties();
  64         DEBUG_METHOD_HANDLE_NAMES = Boolean.parseBoolean(
  65                 props.getProperty("java.lang.invoke.MethodHandle.DEBUG_NAMES"));
  66         DUMP_CLASS_FILES = Boolean.parseBoolean(
  67                 props.getProperty("java.lang.invoke.MethodHandle.DUMP_CLASS_FILES"));
  68         TRACE_INTERPRETER = Boolean.parseBoolean(
  69                 props.getProperty("java.lang.invoke.MethodHandle.TRACE_INTERPRETER"));
  70         TRACE_METHOD_LINKAGE = Boolean.parseBoolean(
  71                 props.getProperty("java.lang.invoke.MethodHandle.TRACE_METHOD_LINKAGE"));
  72         TRACE_RESOLVE = Boolean.parseBoolean(
  73                 props.getProperty("java.lang.invoke.MethodHandle.TRACE_RESOLVE"));
  74         CDS_TRACE_RESOLVE = Boolean.parseBoolean(
  75                 props.getProperty("java.lang.invoke.MethodHandle.CDS_TRACE_RESOLVE"));
  76         COMPILE_THRESHOLD = Integer.parseInt(
  77                 props.getProperty("java.lang.invoke.MethodHandle.COMPILE_THRESHOLD", "0"));
  78         LOG_LF_COMPILATION_FAILURE = Boolean.parseBoolean(
  79                 props.getProperty("java.lang.invoke.MethodHandle.LOG_LF_COMPILATION_FAILURE", "false"));
  80         DONT_INLINE_THRESHOLD = Integer.parseInt(
  81                 props.getProperty("java.lang.invoke.MethodHandle.DONT_INLINE_THRESHOLD", "30"));
  82         PROFILE_LEVEL = Integer.parseInt(
  83                 props.getProperty("java.lang.invoke.MethodHandle.PROFILE_LEVEL", "0"));
  84         PROFILE_GWT = Boolean.parseBoolean(
  85                 props.getProperty("java.lang.invoke.MethodHandle.PROFILE_GWT", "true"));
  86         CUSTOMIZE_THRESHOLD = Integer.parseInt(
  87                 props.getProperty("java.lang.invoke.MethodHandle.CUSTOMIZE_THRESHOLD", "127"));
  88         VAR_HANDLE_GUARDS = Boolean.parseBoolean(
  89                 props.getProperty("java.lang.invoke.VarHandle.VAR_HANDLE_GUARDS", "true"));
  90         VAR_HANDLE_IDENTITY_ADAPT = Boolean.parseBoolean(
  91                 props.getProperty("java.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT", "false"));
  92 
  93         // Do not adjust this except for special platforms:
  94         MAX_ARITY = Integer.parseInt(
  95                 props.getProperty("java.lang.invoke.MethodHandleImpl.MAX_ARITY", "255"));
  96 
  97         if (CUSTOMIZE_THRESHOLD < -1 || CUSTOMIZE_THRESHOLD > 127) {
  98             throw newInternalError("CUSTOMIZE_THRESHOLD should be in [-1...127] range");
  99         }
 100     }
 101 
 102     /** Tell if any of the debugging switches are turned on.
 103      *  If this is the case, it is reasonable to perform extra checks or save extra information.
 104      */
 105     /*non-public*/
 106     static boolean debugEnabled() {
 107         return (DEBUG_METHOD_HANDLE_NAMES |
 108                 DUMP_CLASS_FILES |
 109                 TRACE_INTERPRETER |
 110                 TRACE_METHOD_LINKAGE |
 111                 LOG_LF_COMPILATION_FAILURE);
 112     }
 113 
 114     // handy shared exception makers (they simplify the common case code)
 115     /*non-public*/
 116     static InternalError newInternalError(String message) {
 117         return new InternalError(message);
 118     }
 119     /*non-public*/
 120     static InternalError newInternalError(String message, Exception cause) {
 121         return new InternalError(message, cause);
 122     }
 123     /*non-public*/
 124     static InternalError newInternalError(Exception cause) {
 125         return new InternalError(cause);
 126     }
 127     /*non-public*/
 128     static RuntimeException newIllegalStateException(String message) {
 129         return new IllegalStateException(message);
 130     }
 131     /*non-public*/
 132     static RuntimeException newIllegalStateException(String message, Object obj) {
 133         return new IllegalStateException(message(message, obj));
 134     }
 135     /*non-public*/
 136     static RuntimeException newIllegalArgumentException(String message) {
 137         return new IllegalArgumentException(message);
 138     }
 139     /*non-public*/
 140     static RuntimeException newIllegalArgumentException(String message, Object obj) {
 141         return new IllegalArgumentException(message(message, obj));
 142     }
 143     /*non-public*/
 144     static RuntimeException newIllegalArgumentException(String message, Object obj, Object obj2) {
 145         return new IllegalArgumentException(message(message, obj, obj2));
 146     }
 147     /** Propagate unchecked exceptions and errors, but wrap anything checked and throw that instead. */
 148     /*non-public*/
 149     static Error uncaughtException(Throwable ex) {
 150         if (ex instanceof Error)  throw (Error) ex;
 151         if (ex instanceof RuntimeException)  throw (RuntimeException) ex;
 152         throw new InternalError("uncaught exception", ex);
 153     }
 154     private static String message(String message, Object obj) {
 155         if (obj != null)  message = message + ": " + obj;
 156         return message;
 157     }
 158     private static String message(String message, Object obj, Object obj2) {
 159         if (obj != null || obj2 != null)  message = message + ": " + obj + ", " + obj2;
 160         return message;
 161     }
 162     /*non-public*/
 163     static void rangeCheck2(int start, int end, int size) {
 164         if (0 > start || start > end || end > size)
 165             throw new IndexOutOfBoundsException(start+".."+end);
 166     }
 167     /*non-public*/
 168     static int rangeCheck1(int index, int size) {
 169         if (0 > index || index >= size)
 170             throw new IndexOutOfBoundsException(index);
 171         return index;
 172     }
 173 }