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.org.objectweb.asm.ClassWriter;
29 import jdk.internal.org.objectweb.asm.Label;
30 import jdk.internal.org.objectweb.asm.MethodVisitor;
31 import jdk.internal.org.objectweb.asm.Opcodes;
32 import jdk.internal.vm.annotation.ForceInline;
33 import jdk.internal.misc.Unsafe;
34
35 import java.lang.invoke.MethodHandles.Lookup;
36 import java.security.AccessController;
37 import java.security.PrivilegedAction;
38 import java.util.*;
39 import java.util.concurrent.ConcurrentHashMap;
40 import java.util.concurrent.ConcurrentMap;
41 import java.util.function.Function;
42
43 import static jdk.internal.org.objectweb.asm.Opcodes.*;
44
45 /**
46 * <p>Methods to facilitate the creation of String concatenation methods, that
47 * can be used to efficiently concatenate a known number of arguments of known
48 * types, possibly after type adaptation and partial evaluation of arguments.
49 * These methods are typically used as <em>bootstrap methods</em> for {@code
50 * invokedynamic} call sites, to support the <em>string concatenation</em>
51 * feature of the Java Programming Language.
52 *
53 * <p>Indirect access to the behavior specified by the provided {@code
54 * MethodHandle} proceeds in order through two phases:
55 *
56 * <ol>
57 * <li><em>Linkage</em> occurs when the methods in this class are invoked.
58 * They take as arguments a method type describing the concatenated arguments
59 * count and types, and optionally the String <em>recipe</em>, plus the
60 * constants that participate in the String concatenation. The details on
61 * accepted recipe shapes are described further below. Linkage may involve
171 /**
172 * Enables debugging: this may print debugging messages, perform additional (non-neutral for performance)
173 * checks, etc.
174 */
175 private static final boolean DEBUG;
176
177 /**
178 * Enables caching of strategy stubs. This may improve the linkage time by reusing the generated
179 * code, at the expense of contaminating the profiles.
180 */
181 private static final boolean CACHE_ENABLE;
182
183 private static final ConcurrentMap<Key, MethodHandle> CACHE;
184
185 /**
186 * Dump generated classes to disk, for debugging purposes.
187 */
188 private static final ProxyClassesDumper DUMPER;
189
190 static {
191 // Poke the privileged block once, taking everything we need:
192 final Object[] values = new Object[4];
193 AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
194 values[0] = System.getProperty("java.lang.invoke.stringConcat");
195 values[1] = Boolean.getBoolean("java.lang.invoke.stringConcat.cache");
196 values[2] = Boolean.getBoolean("java.lang.invoke.stringConcat.debug");
197 values[3] = System.getProperty("java.lang.invoke.stringConcat.dumpClasses");
198 return null;
199 });
200
201 final String strategy = (String) values[0];
202 CACHE_ENABLE = (Boolean) values[1];
203 DEBUG = (Boolean) values[2];
204 final String dumpPath = (String) values[3];
205
206 STRATEGY = (strategy == null) ? DEFAULT_STRATEGY : Strategy.valueOf(strategy);
207 CACHE = CACHE_ENABLE ? new ConcurrentHashMap<>() : null;
208 DUMPER = (dumpPath == null) ? null : ProxyClassesDumper.getInstance(dumpPath);
209 }
210
211 /**
212 * Cache key is a composite of:
213 * - class name, that lets to disambiguate stubs, to avoid excess sharing
214 * - method type, describing the dynamic arguments for concatenation
215 * - concat recipe, describing the constants and concat shape
216 */
217 private static final class Key {
218 final String className;
219 final MethodType mt;
220 final Recipe recipe;
221
222 public Key(String className, MethodType mt, Recipe recipe) {
223 this.className = className;
224 this.mt = mt;
|
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.org.objectweb.asm.ClassWriter;
29 import jdk.internal.org.objectweb.asm.Label;
30 import jdk.internal.org.objectweb.asm.MethodVisitor;
31 import jdk.internal.org.objectweb.asm.Opcodes;
32 import jdk.internal.vm.annotation.ForceInline;
33 import jdk.internal.misc.Unsafe;
34
35 import java.lang.invoke.MethodHandles.Lookup;
36 import java.security.AccessController;
37 import java.util.*;
38 import java.util.concurrent.ConcurrentHashMap;
39 import java.util.concurrent.ConcurrentMap;
40 import java.util.function.Function;
41 import sun.security.action.GetPropertyAction;
42
43 import static jdk.internal.org.objectweb.asm.Opcodes.*;
44
45 /**
46 * <p>Methods to facilitate the creation of String concatenation methods, that
47 * can be used to efficiently concatenate a known number of arguments of known
48 * types, possibly after type adaptation and partial evaluation of arguments.
49 * These methods are typically used as <em>bootstrap methods</em> for {@code
50 * invokedynamic} call sites, to support the <em>string concatenation</em>
51 * feature of the Java Programming Language.
52 *
53 * <p>Indirect access to the behavior specified by the provided {@code
54 * MethodHandle} proceeds in order through two phases:
55 *
56 * <ol>
57 * <li><em>Linkage</em> occurs when the methods in this class are invoked.
58 * They take as arguments a method type describing the concatenated arguments
59 * count and types, and optionally the String <em>recipe</em>, plus the
60 * constants that participate in the String concatenation. The details on
61 * accepted recipe shapes are described further below. Linkage may involve
171 /**
172 * Enables debugging: this may print debugging messages, perform additional (non-neutral for performance)
173 * checks, etc.
174 */
175 private static final boolean DEBUG;
176
177 /**
178 * Enables caching of strategy stubs. This may improve the linkage time by reusing the generated
179 * code, at the expense of contaminating the profiles.
180 */
181 private static final boolean CACHE_ENABLE;
182
183 private static final ConcurrentMap<Key, MethodHandle> CACHE;
184
185 /**
186 * Dump generated classes to disk, for debugging purposes.
187 */
188 private static final ProxyClassesDumper DUMPER;
189
190 static {
191 final String strategy = AccessController.doPrivileged(
192 new GetPropertyAction("java.lang.invoke.stringConcat"));
193 CACHE_ENABLE = Boolean.parseBoolean(AccessController.doPrivileged(
194 new GetPropertyAction("java.lang.invoke.stringConcat.cache")));
195 DEBUG = Boolean.parseBoolean(AccessController.doPrivileged(
196 new GetPropertyAction("java.lang.invoke.stringConcat.debug")));
197 final String dumpPath = AccessController.doPrivileged(
198 new GetPropertyAction("java.lang.invoke.stringConcat.dumpClasses"));
199
200 STRATEGY = (strategy == null) ? DEFAULT_STRATEGY : Strategy.valueOf(strategy);
201 CACHE = CACHE_ENABLE ? new ConcurrentHashMap<>() : null;
202 DUMPER = (dumpPath == null) ? null : ProxyClassesDumper.getInstance(dumpPath);
203 }
204
205 /**
206 * Cache key is a composite of:
207 * - class name, that lets to disambiguate stubs, to avoid excess sharing
208 * - method type, describing the dynamic arguments for concatenation
209 * - concat recipe, describing the constants and concat shape
210 */
211 private static final class Key {
212 final String className;
213 final MethodType mt;
214 final Recipe recipe;
215
216 public Key(String className, MethodType mt, Recipe recipe) {
217 this.className = className;
218 this.mt = mt;
|