1 /*
  2  * Copyright (c) 2011, 2023, 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.CDS;
 29 import jdk.internal.misc.Unsafe;
 30 import jdk.internal.util.ClassFileDumper;
 31 import sun.security.action.GetPropertyAction;
 32 
 33 import java.lang.reflect.ClassFileFormatVersion;
 34 import java.util.Properties;
 35 
 36 import static java.lang.invoke.LambdaForm.basicTypeSignature;
 37 import static java.lang.invoke.LambdaForm.shortenSignature;
 38 
 39 /**
 40  * This class consists exclusively of static names internal to the
 41  * method handle implementation.
 42  * Usage:  {@code import static java.lang.invoke.MethodHandleStatics.*}
 43  * @author John Rose, JSR 292 EG
 44  */
 45 /*non-public*/
 46 class MethodHandleStatics {
 47 
 48     private MethodHandleStatics() { }  // do not instantiate
 49 
 50     static final Unsafe UNSAFE = Unsafe.getUnsafe();
 51     static final int CLASSFILE_VERSION = ClassFileFormatVersion.latest().major();
 52     static final boolean DEBUG_METHOD_HANDLE_NAMES;
 53     static final boolean TRACE_INTERPRETER;
 54     static final boolean TRACE_METHOD_LINKAGE;
 55     static final boolean TRACE_RESOLVE;
 56     static final int COMPILE_THRESHOLD;
 57     static final boolean LOG_LF_COMPILATION_FAILURE;
 58     static final int DONT_INLINE_THRESHOLD;
 59     static final int PROFILE_LEVEL;
 60     static final boolean PROFILE_GWT;
 61     static final int CUSTOMIZE_THRESHOLD;
 62     static final boolean VAR_HANDLE_GUARDS;
 63     static final int MAX_ARITY;
 64     static final boolean VAR_HANDLE_IDENTITY_ADAPT;
 65     static final boolean VAR_HANDLE_SEGMENT_FORCE_EXACT;
 66     static final ClassFileDumper DUMP_CLASS_FILES;
 67 
 68     static {
 69         Properties props = GetPropertyAction.privilegedGetProperties();
 70         DEBUG_METHOD_HANDLE_NAMES = Boolean.parseBoolean(
 71                 props.getProperty("java.lang.invoke.MethodHandle.DEBUG_NAMES"));
 72 
 73         TRACE_INTERPRETER = Boolean.parseBoolean(
 74                 props.getProperty("java.lang.invoke.MethodHandle.TRACE_INTERPRETER"));
 75         TRACE_METHOD_LINKAGE = Boolean.parseBoolean(
 76                 props.getProperty("java.lang.invoke.MethodHandle.TRACE_METHOD_LINKAGE"));
 77         TRACE_RESOLVE = Boolean.parseBoolean(
 78                 props.getProperty("java.lang.invoke.MethodHandle.TRACE_RESOLVE"));
 79         COMPILE_THRESHOLD = Integer.parseInt(
 80                 props.getProperty("java.lang.invoke.MethodHandle.COMPILE_THRESHOLD", "0"));
 81         LOG_LF_COMPILATION_FAILURE = Boolean.parseBoolean(
 82                 props.getProperty("java.lang.invoke.MethodHandle.LOG_LF_COMPILATION_FAILURE", "false"));
 83         DONT_INLINE_THRESHOLD = Integer.parseInt(
 84                 props.getProperty("java.lang.invoke.MethodHandle.DONT_INLINE_THRESHOLD", "30"));
 85         PROFILE_LEVEL = Integer.parseInt(
 86                 props.getProperty("java.lang.invoke.MethodHandle.PROFILE_LEVEL", "0"));
 87         PROFILE_GWT = Boolean.parseBoolean(
 88                 props.getProperty("java.lang.invoke.MethodHandle.PROFILE_GWT", "true"));
 89         CUSTOMIZE_THRESHOLD = Integer.parseInt(
 90                 props.getProperty("java.lang.invoke.MethodHandle.CUSTOMIZE_THRESHOLD", "127"));
 91         VAR_HANDLE_GUARDS = Boolean.parseBoolean(
 92                 props.getProperty("java.lang.invoke.VarHandle.VAR_HANDLE_GUARDS", "true"));
 93         VAR_HANDLE_IDENTITY_ADAPT = Boolean.parseBoolean(
 94                 props.getProperty("java.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT", "false"));
 95         VAR_HANDLE_SEGMENT_FORCE_EXACT = Boolean.parseBoolean(
 96                 props.getProperty("java.lang.invoke.VarHandle.VAR_HANDLE_SEGMENT_FORCE_EXACT", "false"));
 97 
 98         // Do not adjust this except for special platforms:
 99         MAX_ARITY = Integer.parseInt(
100                 props.getProperty("java.lang.invoke.MethodHandleImpl.MAX_ARITY", "255"));
101 
102         DUMP_CLASS_FILES = ClassFileDumper.getInstance("jdk.invoke.MethodHandle.dumpMethodHandleInternals",
103                 "DUMP_METHOD_HANDLE_INTERNALS");
104 
105         if (CUSTOMIZE_THRESHOLD < -1 || CUSTOMIZE_THRESHOLD > 127) {
106             throw newInternalError("CUSTOMIZE_THRESHOLD should be in [-1...127] range");
107         }
108     }
109 
110     /** Tell if any of the debugging switches are turned on.
111      *  If this is the case, it is reasonable to perform extra checks or save extra information.
112      */
113     /*non-public*/
114     static boolean debugEnabled() {
115         return (DEBUG_METHOD_HANDLE_NAMES |
116                 DUMP_CLASS_FILES.isEnabled() |
117                 TRACE_INTERPRETER |
118                 TRACE_METHOD_LINKAGE |
119                 LOG_LF_COMPILATION_FAILURE);
120     }
121 
122     static ClassFileDumper dumper() {
123         return DUMP_CLASS_FILES;
124     }
125 
126     /**
127      * If requested, logs the result of resolving the LambdaForm to stdout
128      * and informs the CDS subsystem about it.
129      */
130     /*non-public*/
131     static void traceLambdaForm(String name, MethodType type, Class<?> holder, MemberName resolvedMember) {
132         if (TRACE_RESOLVE) {
133             System.out.println("[LF_RESOLVE] " + holder.getName() + " " + name + " " +
134                     shortenSignature(basicTypeSignature(type)) +
135                     (resolvedMember != null ? " (success)" : " (fail)"));
136         }
137         if (CDS.isDumpingClassList()) {
138             CDS.traceLambdaFormInvoker("[LF_RESOLVE]", holder.getName(), name, shortenSignature(basicTypeSignature(type)));
139         }
140     }
141 
142     /**
143      * If requested, logs the result of resolving the species type to stdout
144      * and the CDS subsystem.
145      */
146     /*non-public*/
147     static void traceSpeciesType(String cn, Class<?> salvage) {
148         if (TRACE_RESOLVE) {
149             System.out.println("[SPECIES_RESOLVE] " + cn + (salvage != null ? " (salvaged)" : " (generated)"));
150         }
151         if (CDS.isDumpingClassList()) {
152             CDS.traceSpeciesType("[SPECIES_RESOLVE]", cn);
153         }
154     }
155     // handy shared exception makers (they simplify the common case code)
156     /*non-public*/
157     static InternalError newInternalError(String message) {
158         return new InternalError(message);
159     }
160     /*non-public*/
161     static InternalError newInternalError(String message, Exception cause) {
162         return new InternalError(message, cause);
163     }
164     /*non-public*/
165     static InternalError newInternalError(Exception cause) {
166         return new InternalError(cause);
167     }
168     /*non-public*/
169     static RuntimeException newIllegalStateException(String message) {
170         return new IllegalStateException(message);
171     }
172     /*non-public*/
173     static RuntimeException newIllegalStateException(String message, Object obj) {
174         return new IllegalStateException(message(message, obj));
175     }
176     /*non-public*/
177     static RuntimeException newIllegalArgumentException(String message) {
178         return new IllegalArgumentException(message);
179     }
180     /*non-public*/
181     static RuntimeException newIllegalArgumentException(String message, Object obj) {
182         return new IllegalArgumentException(message(message, obj));
183     }
184     /*non-public*/
185     static RuntimeException newIllegalArgumentException(String message, Object obj, Object obj2) {
186         return new IllegalArgumentException(message(message, obj, obj2));
187     }
188     /** Propagate unchecked exceptions and errors, but wrap anything checked and throw that instead. */
189     /*non-public*/
190     static Error uncaughtException(Throwable ex) {
191         if (ex instanceof Error error) throw error;
192         if (ex instanceof RuntimeException re) throw re;
193         throw new InternalError("uncaught exception", ex);
194     }
195     private static String message(String message, Object obj) {
196         if (obj != null)  message = message + ": " + obj;
197         return message;
198     }
199     private static String message(String message, Object obj, Object obj2) {
200         if (obj != null || obj2 != null)  message = message + ": " + obj + ", " + obj2;
201         return message;
202     }
203 }