1 /*
   2  * Copyright (c) 2011, 2012, 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 java.security.AccessController;
  29 import java.security.PrivilegedAction;
  30 import sun.misc.Unsafe;
  31 
  32 /**
  33  * This class consists exclusively of static names internal to the
  34  * method handle implementation.
  35  * Usage:  {@code import static java.lang.invoke.MethodHandleStatics.*}
  36  * @author John Rose, JSR 292 EG
  37  */
  38 /*non-public*/ class MethodHandleStatics {
  39 
  40     private MethodHandleStatics() { }  // do not instantiate
  41 
  42     static final Unsafe UNSAFE = Unsafe.getUnsafe();
  43 
  44     static final boolean DEBUG_METHOD_HANDLE_NAMES;
  45     static final boolean DUMP_CLASS_FILES;
  46     static final boolean TRACE_INTERPRETER;
  47     static final boolean TRACE_METHOD_LINKAGE;
  48     static final Integer COMPILE_THRESHOLD;
  49     static final int PROFILE_LEVEL;
  50 
  51     static {
  52         final Object[] values = { false, false, false, false, null, null };
  53         AccessController.doPrivileged(new PrivilegedAction<Void>() {
  54                 public Void run() {
  55                     values[0] = Boolean.getBoolean("java.lang.invoke.MethodHandle.DEBUG_NAMES");
  56                     values[1] = Boolean.getBoolean("java.lang.invoke.MethodHandle.DUMP_CLASS_FILES");
  57                     values[2] = Boolean.getBoolean("java.lang.invoke.MethodHandle.TRACE_INTERPRETER");
  58                     values[3] = Boolean.getBoolean("java.lang.invoke.MethodHandle.TRACE_METHOD_LINKAGE");
  59                     values[4] = Integer.getInteger("java.lang.invoke.MethodHandle.COMPILE_THRESHOLD");
  60                     values[5] = Integer.getInteger("java.lang.invoke.MethodHandle.PROFILE_LEVEL", 0);
  61 
  62                     return null;
  63                 }
  64             });
  65         DEBUG_METHOD_HANDLE_NAMES = (Boolean) values[0];
  66         DUMP_CLASS_FILES          = (Boolean) values[1];
  67         TRACE_INTERPRETER         = (Boolean) values[2];
  68         TRACE_METHOD_LINKAGE      = (Boolean) values[3];
  69         COMPILE_THRESHOLD         = (Integer) values[4];
  70         PROFILE_LEVEL             = (Integer) values[5];
  71     }
  72 
  73     /** Tell if any of the debugging switches are turned on.
  74      *  If this is the case, it is reasonable to perform extra checks or save extra information.
  75      */
  76     /*non-public*/ static boolean debugEnabled() {
  77         return (DEBUG_METHOD_HANDLE_NAMES |
  78                 DUMP_CLASS_FILES |
  79                 TRACE_INTERPRETER |
  80                 TRACE_METHOD_LINKAGE);
  81     }
  82 
  83     /*non-public*/ static String getNameString(MethodHandle target, MethodType type) {
  84         if (type == null)
  85             type = target.type();
  86         MemberName name = null;
  87         if (target != null)
  88             name = target.internalMemberName();
  89         if (name == null)
  90             return "invoke" + type;
  91         return name.getName() + type;
  92     }
  93 
  94     /*non-public*/ static String getNameString(MethodHandle target, MethodHandle typeHolder) {
  95         return getNameString(target, typeHolder == null ? (MethodType) null : typeHolder.type());
  96     }
  97 
  98     /*non-public*/ static String getNameString(MethodHandle target) {
  99         return getNameString(target, (MethodType) null);
 100     }
 101 
 102     /*non-public*/ static String addTypeString(Object obj, MethodHandle target) {
 103         String str = String.valueOf(obj);
 104         if (target == null)  return str;
 105         int paren = str.indexOf('(');
 106         if (paren >= 0) str = str.substring(0, paren);
 107         return str + target.type();
 108     }
 109 
 110     // handy shared exception makers (they simplify the common case code)
 111     /*non-public*/ static InternalError newInternalError(String message, Throwable cause) {
 112         return new InternalError(message, cause);
 113     }
 114     /*non-public*/ static InternalError newInternalError(Throwable cause) {
 115         return new InternalError(cause);
 116     }
 117     /*non-public*/ static RuntimeException newIllegalStateException(String message) {
 118         return new IllegalStateException(message);
 119     }
 120     /*non-public*/ static RuntimeException newIllegalStateException(String message, Object obj) {
 121         return new IllegalStateException(message(message, obj));
 122     }
 123     /*non-public*/ static RuntimeException newIllegalArgumentException(String message) {
 124         return new IllegalArgumentException(message);
 125     }
 126     /*non-public*/ static RuntimeException newIllegalArgumentException(String message, Object obj) {
 127         return new IllegalArgumentException(message(message, obj));
 128     }
 129     /*non-public*/ static RuntimeException newIllegalArgumentException(String message, Object obj, Object obj2) {
 130         return new IllegalArgumentException(message(message, obj, obj2));
 131     }
 132     /*non-public*/ static Error uncaughtException(Throwable ex) {
 133         throw newInternalError("uncaught exception", ex);
 134     }
 135     static Error NYI() {
 136         throw new AssertionError("NYI");
 137     }
 138     private static String message(String message, Object obj) {
 139         if (obj != null)  message = message + ": " + obj;
 140         return message;
 141     }
 142     private static String message(String message, Object obj, Object obj2) {
 143         if (obj != null || obj2 != null)  message = message + ": " + obj + ", " + obj2;
 144         return message;
 145     }
 146 }