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 sun.misc.Unsafe;
29 
30 /**
31  * This class consists exclusively of static names internal to the
32  * method handle implementation.
33  * Usage:  {@code import static java.lang.invoke.MethodHandleStatics.*}
34  * @author John Rose, JSR 292 EG
35  */
36 /*non-public*/ class MethodHandleStatics {
37 
MethodHandleStatics()38     private MethodHandleStatics() { }  // do not instantiate
39 
40     static final Unsafe UNSAFE = Unsafe.getUnsafe();
41 
42     // Android-changed: Remove debugging related static fields.
43     // They are unused and have no equivalent on Android.
44     /*
45     static final boolean DEBUG_METHOD_HANDLE_NAMES;
46     static final boolean DUMP_CLASS_FILES;
47     static final boolean TRACE_INTERPRETER;
48     static final boolean TRACE_METHOD_LINKAGE;
49     static final int COMPILE_THRESHOLD;
50     static final int DONT_INLINE_THRESHOLD;
51     static final int PROFILE_LEVEL;
52     static final boolean PROFILE_GWT;
53     static final int CUSTOMIZE_THRESHOLD;
54 
55     static {
56         final Object[] values = new Object[9];
57         AccessController.doPrivileged(new PrivilegedAction<Void>() {
58                 public Void run() {
59                     values[0] = Boolean.getBoolean("java.lang.invoke.MethodHandle.DEBUG_NAMES");
60                     values[1] = Boolean.getBoolean("java.lang.invoke.MethodHandle.DUMP_CLASS_FILES");
61                     values[2] = Boolean.getBoolean("java.lang.invoke.MethodHandle.TRACE_INTERPRETER");
62                     values[3] = Boolean.getBoolean("java.lang.invoke.MethodHandle.TRACE_METHOD_LINKAGE");
63                     values[4] = Integer.getInteger("java.lang.invoke.MethodHandle.COMPILE_THRESHOLD", 0);
64                     values[5] = Integer.getInteger("java.lang.invoke.MethodHandle.DONT_INLINE_THRESHOLD", 30);
65                     values[6] = Integer.getInteger("java.lang.invoke.MethodHandle.PROFILE_LEVEL", 0);
66                     values[7] = Boolean.parseBoolean(System.getProperty("java.lang.invoke.MethodHandle.PROFILE_GWT", "true"));
67                     values[8] = Integer.getInteger("java.lang.invoke.MethodHandle.CUSTOMIZE_THRESHOLD", 127);
68                     return null;
69                 }
70             });
71         DEBUG_METHOD_HANDLE_NAMES = (Boolean) values[0];
72         DUMP_CLASS_FILES          = (Boolean) values[1];
73         TRACE_INTERPRETER         = (Boolean) values[2];
74         TRACE_METHOD_LINKAGE      = (Boolean) values[3];
75         COMPILE_THRESHOLD         = (Integer) values[4];
76         DONT_INLINE_THRESHOLD     = (Integer) values[5];
77         PROFILE_LEVEL             = (Integer) values[6];
78         PROFILE_GWT               = (Boolean) values[7];
79         CUSTOMIZE_THRESHOLD       = (Integer) values[8];
80 
81         if (CUSTOMIZE_THRESHOLD < -1 || CUSTOMIZE_THRESHOLD > 127) {
82             throw newInternalError("CUSTOMIZE_THRESHOLD should be in [-1...127] range");
83         }
84     }
85 
86     /** Tell if any of the debugging switches are turned on.
87      *  If this is the case, it is reasonable to perform extra checks or save extra information.
88      *
89     /*non-public* static boolean debugEnabled() {
90         return (DEBUG_METHOD_HANDLE_NAMES |
91                 DUMP_CLASS_FILES |
92                 TRACE_INTERPRETER |
93                 TRACE_METHOD_LINKAGE);
94     }
95     */
96 
97     // Android-removed: Methods operating on MethodHandles that are currently unused on Android.
98     /*
99     /*non-public* static String getNameString(MethodHandle target, MethodType type) {
100         if (type == null)
101             type = target.type();
102         MemberName name = null;
103         if (target != null)
104             name = target.internalMemberName();
105         if (name == null)
106             return "invoke" + type;
107         return name.getName() + type;
108     }
109 
110     /*non-public* static String getNameString(MethodHandle target, MethodHandle typeHolder) {
111         return getNameString(target, typeHolder == null ? (MethodType) null : typeHolder.type());
112     }
113 
114     /*non-public* static String getNameString(MethodHandle target) {
115         return getNameString(target, (MethodType) null);
116     }
117 
118     /*non-public* static String addTypeString(Object obj, MethodHandle target) {
119         String str = String.valueOf(obj);
120         if (target == null)  return str;
121         int paren = str.indexOf('(');
122         if (paren >= 0) str = str.substring(0, paren);
123         return str + target.type();
124     }
125     */
126 
127     // handy shared exception makers (they simplify the common case code)
newInternalError(String message)128     /*non-public*/ static InternalError newInternalError(String message) {
129         return new InternalError(message);
130     }
newInternalError(String message, Throwable cause)131     /*non-public*/ static InternalError newInternalError(String message, Throwable cause) {
132         return new InternalError(message, cause);
133     }
newInternalError(Throwable cause)134     /*non-public*/ static InternalError newInternalError(Throwable cause) {
135         return new InternalError(cause);
136     }
newIllegalStateException(String message)137     /*non-public*/ static RuntimeException newIllegalStateException(String message) {
138         return new IllegalStateException(message);
139     }
newIllegalStateException(String message, Object obj)140     /*non-public*/ static RuntimeException newIllegalStateException(String message, Object obj) {
141         return new IllegalStateException(message(message, obj));
142     }
newIllegalArgumentException(String message)143     /*non-public*/ static RuntimeException newIllegalArgumentException(String message) {
144         return new IllegalArgumentException(message);
145     }
newIllegalArgumentException(String message, Object obj)146     /*non-public*/ static RuntimeException newIllegalArgumentException(String message, Object obj) {
147         return new IllegalArgumentException(message(message, obj));
148     }
newIllegalArgumentException(String message, Object obj, Object obj2)149     /*non-public*/ static RuntimeException newIllegalArgumentException(String message, Object obj, Object obj2) {
150         return new IllegalArgumentException(message(message, obj, obj2));
151     }
152     /** Propagate unchecked exceptions and errors, but wrap anything checked and throw that instead. */
uncaughtException(Throwable ex)153     /*non-public*/ static Error uncaughtException(Throwable ex) {
154         if (ex instanceof Error)  throw (Error) ex;
155         if (ex instanceof RuntimeException)  throw (RuntimeException) ex;
156         throw newInternalError("uncaught exception", ex);
157     }
NYI()158     static Error NYI() {
159         throw new AssertionError("NYI");
160     }
message(String message, Object obj)161     private static String message(String message, Object obj) {
162         if (obj != null)  message = message + ": " + obj;
163         return message;
164     }
message(String message, Object obj, Object obj2)165     private static String message(String message, Object obj, Object obj2) {
166         if (obj != null || obj2 != null)  message = message + ": " + obj + ", " + obj2;
167         return message;
168     }
169 }
170