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