jdk/src/java.base/share/classes/java/lang/invoke/StringConcatFactory.java
author shade
Wed, 10 Feb 2016 16:36:25 +0300
changeset 35776 dd5df83fcc0d
parent 35700 b933119b8f84
child 36001 5f0acf0668e0
permissions -rw-r--r--
8149459: StringConcatFactory should be synced up with LambdaMetafactory Reviewed-by: psandoz, vlivanov, forax
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     1
/*
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     4
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    10
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    15
 * accompanied this code).
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    16
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    20
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    23
 * questions.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    24
 */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    25
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    26
package java.lang.invoke;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    27
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    28
import jdk.internal.org.objectweb.asm.ClassWriter;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    29
import jdk.internal.org.objectweb.asm.Label;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    30
import jdk.internal.org.objectweb.asm.MethodVisitor;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    31
import jdk.internal.org.objectweb.asm.Opcodes;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    32
import jdk.internal.vm.annotation.ForceInline;
35700
b933119b8f84 8148869: StringConcatFactory MH_INLINE_SIZED_EXACT strategy does not work with -XX:-CompactStrings
shade
parents: 35639
diff changeset
    33
import jdk.internal.misc.Unsafe;
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    34
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    35
import java.lang.invoke.MethodHandles.Lookup;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    36
import java.security.AccessController;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    37
import java.security.PrivilegedAction;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    38
import java.util.*;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    39
import java.util.concurrent.ConcurrentHashMap;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    40
import java.util.concurrent.ConcurrentMap;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    41
import java.util.function.Function;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    42
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    43
import static jdk.internal.org.objectweb.asm.Opcodes.*;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    44
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    45
/**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    46
 * <p>Methods to facilitate the creation of String concatenation methods, that
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    47
 * can be used to efficiently concatenate a known number of arguments of known
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    48
 * types, possibly after type adaptation and partial evaluation of arguments.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    49
 * These methods are typically used as <em>bootstrap methods</em> for {@code
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    50
 * invokedynamic} call sites, to support the <em>string concatenation</em>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    51
 * feature of the Java Programming Language.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    52
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    53
 * <p>Indirect access to the behavior specified by the provided {@code
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    54
 * MethodHandle} proceeds in order through two phases:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    55
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    56
 * <ol>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    57
 *     <li><em>Linkage</em> occurs when the methods in this class are invoked.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    58
 * They take as arguments a method type describing the concatenated arguments
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    59
 * count and types, and optionally the String <em>recipe</em>, plus the
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    60
 * constants that participate in the String concatenation. The details on
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    61
 * accepted recipe shapes are described further below. Linkage may involve
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    62
 * dynamically loading a new class that implements the expected concatenation
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    63
 * behavior. The {@code CallSite} holds the {@code MethodHandle} pointing to the
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    64
 * exact concatenation method. The concatenation methods may be shared among
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    65
 * different {@code CallSite}s, e.g. if linkage methods produce them as pure
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    66
 * functions.</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    67
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    68
 * <li><em>Invocation</em> occurs when a generated concatenation method is
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    69
 * invoked with the exact dynamic arguments. This may occur many times for a
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    70
 * single concatenation method. The method referenced by the behavior {@code
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    71
 * MethodHandle} is invoked with the static arguments and any additional dynamic
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    72
 * arguments provided on invocation, as if by {@link MethodHandle#invoke(Object...)}.</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    73
 * </ol>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    74
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    75
 * <p> This class provides two forms of linkage methods: a simple version
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    76
 * ({@link #makeConcat(java.lang.invoke.MethodHandles.Lookup, String,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    77
 * MethodType)}) using only the dynamic arguments, and an advanced version
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    78
 * ({@link #makeConcatWithConstants(java.lang.invoke.MethodHandles.Lookup,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    79
 * String, MethodType, String, Object...)} using the advanced forms of capturing
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    80
 * the constant arguments. The advanced strategy can produce marginally better
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    81
 * invocation bytecode, at the expense of exploding the number of shapes of
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    82
 * string concatenation methods present at runtime, because those shapes would
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    83
 * include constant static arguments as well.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    84
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    85
 * @author Aleksey Shipilev
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    86
 * @author Remi Forax
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    87
 * @author Peter Levart
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    88
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    89
 * @apiNote
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    90
 * <p>There is a JVM limit (classfile structural constraint): no method
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    91
 * can call with more than 255 slots. This limits the number of static and
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    92
 * dynamic arguments one can pass to bootstrap method. Since there are potential
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    93
 * concatenation strategies that use {@code MethodHandle} combinators, we need
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    94
 * to reserve a few empty slots on the parameter lists to to capture the
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    95
 * temporal results. This is why bootstrap methods in this factory do not accept
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    96
 * more than 200 argument slots. Users requiring more than 200 argument slots in
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    97
 * concatenation are expected to split the large concatenation in smaller
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    98
 * expressions.
35401
f57a51dbcc50 8148730: Add @since tags in new String concat APIs
shade
parents: 35388
diff changeset
    99
 *
f57a51dbcc50 8148730: Add @since tags in new String concat APIs
shade
parents: 35388
diff changeset
   100
 * @since 9
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   101
 */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   102
public final class StringConcatFactory {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   103
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   104
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   105
     * Tag used to demarcate an ordinary argument.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   106
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   107
    private static final char TAG_ARG = '\u0001';
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   108
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   109
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   110
     * Tag used to demarcate a constant.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   111
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   112
    private static final char TAG_CONST = '\u0002';
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   113
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   114
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   115
     * Maximum number of argument slots in String Concat call.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   116
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   117
     * While the maximum number of argument slots that indy call can handle is 253,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   118
     * we do not use all those slots, to let the strategies with MethodHandle
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   119
     * combinators to use some arguments.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   120
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   121
    private static final int MAX_INDY_CONCAT_ARG_SLOTS = 200;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   122
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   123
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   124
     * Concatenation strategy to use. See {@link Strategy} for possible options.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   125
     * This option is controllable with -Djava.lang.invoke.stringConcat JDK option.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   126
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   127
    private static final Strategy STRATEGY;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   128
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   129
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   130
     * Default strategy to use for concatenation.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   131
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   132
    private static final Strategy DEFAULT_STRATEGY = Strategy.BC_SB;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   133
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   134
    private enum Strategy {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   135
        /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   136
         * Bytecode generator, calling into {@link java.lang.StringBuilder}.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   137
         */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   138
        BC_SB,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   139
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   140
        /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   141
         * Bytecode generator, calling into {@link java.lang.StringBuilder};
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   142
         * but trying to estimate the required storage.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   143
         */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   144
        BC_SB_SIZED,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   145
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   146
        /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   147
         * Bytecode generator, calling into {@link java.lang.StringBuilder};
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   148
         * but computing the required storage exactly.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   149
         */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   150
        BC_SB_SIZED_EXACT,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   151
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   152
        /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   153
         * MethodHandle-based generator, that in the end calls into {@link java.lang.StringBuilder}.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   154
         * This strategy also tries to estimate the required storage.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   155
         */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   156
        MH_SB_SIZED,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   157
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   158
        /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   159
         * MethodHandle-based generator, that in the end calls into {@link java.lang.StringBuilder}.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   160
         * This strategy also estimate the required storage exactly.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   161
         */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   162
        MH_SB_SIZED_EXACT,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   163
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   164
        /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   165
         * MethodHandle-based generator, that constructs its own byte[] array from
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   166
         * the arguments. It computes the required storage exactly.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   167
         */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   168
        MH_INLINE_SIZED_EXACT
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   169
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   170
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   171
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   172
     * Enables debugging: this may print debugging messages, perform additional (non-neutral for performance)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   173
     * checks, etc.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   174
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   175
    private static final boolean DEBUG;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   176
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   177
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   178
     * Enables caching of strategy stubs. This may improve the linkage time by reusing the generated
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   179
     * code, at the expense of contaminating the profiles.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   180
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   181
    private static final boolean CACHE_ENABLE;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   182
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   183
    private static final ConcurrentMap<Key, MethodHandle> CACHE;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   184
35776
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   185
    /**
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   186
     * Dump generated classes to disk, for debugging purposes.
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   187
     */
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   188
    private static final ProxyClassesDumper DUMPER;
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   189
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   190
    static {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   191
        // Poke the privileged block once, taking everything we need:
35776
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   192
        final Object[] values = new Object[4];
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   193
        AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   194
            values[0] = System.getProperty("java.lang.invoke.stringConcat");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   195
            values[1] = Boolean.getBoolean("java.lang.invoke.stringConcat.cache");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   196
            values[2] = Boolean.getBoolean("java.lang.invoke.stringConcat.debug");
35776
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   197
            values[3] = System.getProperty("java.lang.invoke.stringConcat.dumpClasses");
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   198
            return null;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   199
        });
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   200
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   201
        final String strategy = (String)  values[0];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   202
        CACHE_ENABLE          = (Boolean) values[1];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   203
        DEBUG                 = (Boolean) values[2];
35776
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   204
        final String dumpPath = (String)  values[3];
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   205
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   206
        STRATEGY = (strategy == null) ? DEFAULT_STRATEGY : Strategy.valueOf(strategy);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   207
        CACHE = CACHE_ENABLE ? new ConcurrentHashMap<>() : null;
35776
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   208
        DUMPER = (dumpPath == null) ? null : ProxyClassesDumper.getInstance(dumpPath);
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   209
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   210
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   211
    private static final class Key {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   212
        final MethodType mt;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   213
        final Recipe recipe;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   214
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   215
        public Key(MethodType mt, Recipe recipe) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   216
            this.mt = mt;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   217
            this.recipe = recipe;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   218
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   219
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   220
        @Override
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   221
        public boolean equals(Object o) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   222
            if (this == o) return true;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   223
            if (o == null || getClass() != o.getClass()) return false;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   224
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   225
            Key key = (Key) o;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   226
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   227
            if (!mt.equals(key.mt)) return false;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   228
            if (!recipe.equals(key.recipe)) return false;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   229
            return true;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   230
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   231
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   232
        @Override
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   233
        public int hashCode() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   234
            int result = mt.hashCode();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   235
            result = 31 * result + recipe.hashCode();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   236
            return result;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   237
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   238
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   239
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   240
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   241
     * Parses the recipe string, and produces the traversable collection of
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   242
     * {@link java.lang.invoke.StringConcatFactory.RecipeElement}-s for generator
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   243
     * strategies. Notably, this class parses out the constants from the recipe
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   244
     * and from other static arguments.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   245
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   246
    private static final class Recipe {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   247
        private final List<RecipeElement> elements;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   248
        private final List<RecipeElement> elementsRev;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   249
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   250
        public Recipe(String src, Object[] constants) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   251
            List<RecipeElement> el = new ArrayList<>();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   252
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   253
            int constC = 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   254
            int argC = 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   255
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   256
            StringBuilder acc = new StringBuilder();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   257
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   258
            for (int i = 0; i < src.length(); i++) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   259
                char c = src.charAt(i);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   260
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   261
                if (c == TAG_CONST || c == TAG_ARG) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   262
                    // Detected a special tag, flush all accumulated characters
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   263
                    // as a constant first:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   264
                    if (acc.length() > 0) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   265
                        el.add(new RecipeElement(acc.toString()));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   266
                        acc.setLength(0);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   267
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   268
                    if (c == TAG_CONST) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   269
                        Object cnst = constants[constC++];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   270
                        el.add(new RecipeElement(cnst));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   271
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   272
                    if (c == TAG_ARG) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   273
                        el.add(new RecipeElement(argC++));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   274
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   275
                } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   276
                    // Not a special characters, this is a constant embedded into
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   277
                    // the recipe itself.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   278
                    acc.append(c);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   279
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   280
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   281
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   282
            // Flush the remaining characters as constant:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   283
            if (acc.length() > 0) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   284
                el.add(new RecipeElement(acc.toString()));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   285
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   286
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   287
            elements = new ArrayList<>(el);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   288
            Collections.reverse(el);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   289
            elementsRev = el;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   290
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   291
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   292
        public Collection<RecipeElement> getElements() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   293
            return elements;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   294
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   295
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   296
        public Collection<RecipeElement> getElementsReversed() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   297
            return elementsRev;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   298
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   299
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   300
        @Override
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   301
        public boolean equals(Object o) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   302
            if (this == o) return true;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   303
            if (o == null || getClass() != o.getClass()) return false;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   304
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   305
            Recipe recipe = (Recipe) o;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   306
            return elements.equals(recipe.elements);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   307
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   308
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   309
        @Override
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   310
        public int hashCode() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   311
            return elements.hashCode();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   312
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   313
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   314
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   315
    private static final class RecipeElement {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   316
        private final Object value;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   317
        private final int argPos;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   318
        private final Tag tag;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   319
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   320
        public RecipeElement(Object cnst) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   321
            this.value = Objects.requireNonNull(cnst);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   322
            this.argPos = -1;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   323
            this.tag = Tag.CONST;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   324
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   325
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   326
        public RecipeElement(int arg) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   327
            this.value = null;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   328
            this.argPos = arg;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   329
            this.tag = Tag.ARG;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   330
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   331
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   332
        public Object getValue() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   333
            assert (tag == Tag.CONST);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   334
            return value;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   335
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   336
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   337
        public int getArgPos() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   338
            assert (tag == Tag.ARG);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   339
            return argPos;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   340
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   341
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   342
        public Tag getTag() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   343
            return tag;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   344
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   345
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   346
        @Override
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   347
        public boolean equals(Object o) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   348
            if (this == o) return true;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   349
            if (o == null || getClass() != o.getClass()) return false;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   350
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   351
            RecipeElement that = (RecipeElement) o;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   352
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   353
            if (tag != that.tag) return false;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   354
            if (tag == Tag.CONST && (!value.equals(that.value))) return false;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   355
            if (tag == Tag.ARG && (argPos != that.argPos)) return false;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   356
            return true;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   357
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   358
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   359
        @Override
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   360
        public int hashCode() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   361
            return tag.hashCode();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   362
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   363
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   364
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   365
    private enum Tag {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   366
        CONST, ARG
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   367
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   368
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   369
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   370
     * Facilitates the creation of optimized String concatenation methods, that
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   371
     * can be used to efficiently concatenate a known number of arguments of
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   372
     * known types, possibly after type adaptation and partial evaluation of
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   373
     * arguments. Typically used as a <em>bootstrap method</em> for {@code
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   374
     * invokedynamic} call sites, to support the <em>string concatenation</em>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   375
     * feature of the Java Programming Language.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   376
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   377
     * <p>When the target of the {@code CallSite} returned from this method is
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   378
     * invoked, it returns the result of String concatenation, taking all
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   379
     * function arguments passed to the linkage method as inputs for
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   380
     * concatenation. The target signature is given by {@code concatType}.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   381
     * The arguments are concatenated as per requirements stated in JLS 15.18.1
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   382
     * "String Concatenation Operator +". Notably, the inputs are converted as
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   383
     * per JLS 5.1.11 "String Conversion", and combined from left to right.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   384
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   385
     * <p>Assume the linkage arguments are as follows:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   386
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   387
     * <ul>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   388
     *     <li>{@code concatType}, describing the {@code CallSite} signature</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   389
     * </ul>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   390
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   391
     * <p>Then the following linkage invariants must hold:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   392
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   393
     * <ul>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   394
     *     <li>The parameter count in {@code concatType} is less than or equal to 200</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   395
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   396
     *     <li>The return type in {@code concatType} is assignable from {@link java.lang.String}</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   397
     * </ul>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   398
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   399
     * @param lookup   Represents a lookup context with the accessibility
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   400
     *                 privileges of the caller.  When used with {@code
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   401
     *                 invokedynamic}, this is stacked automatically by the VM.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   402
     * @param name     The name of the method to implement. This name is
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   403
     *                 arbitrary, and has no meaning for this linkage method.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   404
     *                 When used with {@code invokedynamic}, this is provided by
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   405
     *                 the {@code NameAndType} of the {@code InvokeDynamic}
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   406
     *                 structure and is stacked automatically by the VM.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   407
     * @param concatType The expected signature of the {@code CallSite}.  The
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   408
     *                   parameter types represent the types of concatenation
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   409
     *                   arguments; the return type is always assignable from {@link
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   410
     *                   java.lang.String}.  When used with {@code invokedynamic},
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   411
     *                   this is provided by the {@code NameAndType} of the {@code
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   412
     *                   InvokeDynamic} structure and is stacked automatically by
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   413
     *                   the VM.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   414
     * @return a CallSite whose target can be used to perform String
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   415
     * concatenation, with dynamic concatenation arguments described by the given
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   416
     * {@code concatType}.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   417
     * @throws StringConcatException If any of the linkage invariants described
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   418
     *                               here are violated.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   419
     * @throws NullPointerException If any of the incoming arguments is null.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   420
     *                              This will never happen when a bootstrap method
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   421
     *                              is called with invokedynamic.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   422
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   423
     * @jls  5.1.11 String Conversion
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   424
     * @jls 15.18.1 String Concatenation Operator +
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   425
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   426
    public static CallSite makeConcat(MethodHandles.Lookup lookup,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   427
                                      String name,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   428
                                      MethodType concatType) throws StringConcatException {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   429
        if (DEBUG) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   430
            System.out.println("StringConcatFactory " + STRATEGY + " is here for " + concatType);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   431
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   432
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   433
        return doStringConcat(lookup, name, concatType, true, null);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   434
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   435
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   436
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   437
     * Facilitates the creation of optimized String concatenation methods, that
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   438
     * can be used to efficiently concatenate a known number of arguments of
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   439
     * known types, possibly after type adaptation and partial evaluation of
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   440
     * arguments. Typically used as a <em>bootstrap method</em> for {@code
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   441
     * invokedynamic} call sites, to support the <em>string concatenation</em>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   442
     * feature of the Java Programming Language.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   443
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   444
     * <p>When the target of the {@code CallSite} returned from this method is
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   445
     * invoked, it returns the result of String concatenation, taking all
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   446
     * function arguments and constants passed to the linkage method as inputs for
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   447
     * concatenation. The target signature is given by {@code concatType}, and
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   448
     * does not include constants. The arguments are concatenated as per requirements
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   449
     * stated in JLS 15.18.1 "String Concatenation Operator +". Notably, the inputs
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   450
     * are converted as per JLS 5.1.11 "String Conversion", and combined from left
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   451
     * to right.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   452
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   453
     * <p>The concatenation <em>recipe</em> is a String description for the way to
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   454
     * construct a concatenated String from the arguments and constants. The
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   455
     * recipe is processed from left to right, and each character represents an
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   456
     * input to concatenation. Recipe characters mean:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   457
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   458
     * <ul>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   459
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   460
     *   <li><em>\1 (Unicode point 0001)</em>: an ordinary argument. This
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   461
     *   input is passed through dynamic argument, and is provided during the
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   462
     *   concatenation method invocation. This input can be null.</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   463
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   464
     *   <li><em>\2 (Unicode point 0002):</em> a constant. This input passed
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   465
     *   through static bootstrap argument. This constant can be any value
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   466
     *   representable in constant pool. If necessary, the factory would call
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   467
     *   {@code toString} to perform a one-time String conversion.</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   468
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   469
     *   <li><em>Any other char value:</em> a single character constant.</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   470
     * </ul>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   471
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   472
     * <p>Assume the linkage arguments are as follows:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   473
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   474
     * <ul>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   475
     *   <li>{@code concatType}, describing the {@code CallSite} signature</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   476
     *   <li>{@code recipe}, describing the String recipe</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   477
     *   <li>{@code constants}, the vararg array of constants</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   478
     * </ul>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   479
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   480
     * <p>Then the following linkage invariants must hold:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   481
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   482
     * <ul>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   483
     *   <li>The parameter count in {@code concatType} is less than or equal to
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   484
     *   200</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   485
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   486
     *   <li>The parameter count in {@code concatType} equals to number of \1 tags
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   487
     *   in {@code recipe}</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   488
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   489
     *   <li>The return type in {@code concatType} is assignable
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   490
     *   from {@link java.lang.String}, and matches the return type of the
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   491
     *   returned {@link MethodHandle}</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   492
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   493
     *   <li>The number of elements in {@code constants} equals to number of \2
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   494
     *   tags in {@code recipe}</li>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   495
     * </ul>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   496
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   497
     * @param lookup    Represents a lookup context with the accessibility
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   498
     *                  privileges of the caller. When used with {@code
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   499
     *                  invokedynamic}, this is stacked automatically by the
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   500
     *                  VM.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   501
     * @param name      The name of the method to implement. This name is
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   502
     *                  arbitrary, and has no meaning for this linkage method.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   503
     *                  When used with {@code invokedynamic}, this is provided
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   504
     *                  by the {@code NameAndType} of the {@code InvokeDynamic}
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   505
     *                  structure and is stacked automatically by the VM.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   506
     * @param concatType The expected signature of the {@code CallSite}.  The
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   507
     *                  parameter types represent the types of dynamic concatenation
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   508
     *                  arguments; the return type is always assignable from {@link
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   509
     *                  java.lang.String}.  When used with {@code
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   510
     *                  invokedynamic}, this is provided by the {@code
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   511
     *                  NameAndType} of the {@code InvokeDynamic} structure and
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   512
     *                  is stacked automatically by the VM.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   513
     * @param recipe    Concatenation recipe, described above.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   514
     * @param constants A vararg parameter representing the constants passed to
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   515
     *                  the linkage method.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   516
     * @return a CallSite whose target can be used to perform String
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   517
     * concatenation, with dynamic concatenation arguments described by the given
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   518
     * {@code concatType}.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   519
     * @throws StringConcatException If any of the linkage invariants described
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   520
     *                               here are violated.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   521
     * @throws NullPointerException If any of the incoming arguments is null, or
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   522
     *                              any constant in {@code recipe} is null.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   523
     *                              This will never happen when a bootstrap method
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   524
     *                              is called with invokedynamic.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   525
     * @apiNote Code generators have three distinct ways to process a constant
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   526
     * string operand S in a string concatenation expression.  First, S can be
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   527
     * materialized as a reference (using ldc) and passed as an ordinary argument
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   528
     * (recipe '\1'). Or, S can be stored in the constant pool and passed as a
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   529
     * constant (recipe '\2') . Finally, if S contains neither of the recipe
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   530
     * tag characters ('\1', '\2') then S can be interpolated into the recipe
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   531
     * itself, causing its characters to be inserted into the result.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   532
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   533
     * @jls  5.1.11 String Conversion
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   534
     * @jls 15.18.1 String Concatenation Operator +
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   535
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   536
    public static CallSite makeConcatWithConstants(MethodHandles.Lookup lookup,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   537
                                                   String name,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   538
                                                   MethodType concatType,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   539
                                                   String recipe,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   540
                                                   Object... constants) throws StringConcatException {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   541
        if (DEBUG) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   542
            System.out.println("StringConcatFactory " + STRATEGY + " is here for " + concatType + ", {" + recipe + "}, " + Arrays.toString(constants));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   543
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   544
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   545
        return doStringConcat(lookup, name, concatType, false, recipe, constants);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   546
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   547
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   548
    private static CallSite doStringConcat(MethodHandles.Lookup lookup,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   549
                                           String name,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   550
                                           MethodType concatType,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   551
                                           boolean generateRecipe,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   552
                                           String recipe,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   553
                                           Object... constants) throws StringConcatException {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   554
        Objects.requireNonNull(lookup, "Lookup is null");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   555
        Objects.requireNonNull(name, "Name is null");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   556
        Objects.requireNonNull(concatType, "Concat type is null");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   557
        Objects.requireNonNull(constants, "Constants are null");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   558
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   559
        for (Object o : constants) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   560
            Objects.requireNonNull(o, "Cannot accept null constants");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   561
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   562
35776
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   563
        if ((lookup.lookupModes() & MethodHandles.Lookup.PRIVATE) == 0) {
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   564
            throw new StringConcatException(String.format(
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   565
                    "Invalid caller: %s",
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   566
                    lookup.lookupClass().getName()));
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   567
        }
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
   568
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   569
        int cCount = 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   570
        int oCount = 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   571
        if (generateRecipe) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   572
            // Mock the recipe to reuse the concat generator code
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   573
            char[] value = new char[concatType.parameterCount()];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   574
            Arrays.fill(value, TAG_ARG);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   575
            recipe = new String(value);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   576
            oCount = concatType.parameterCount();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   577
        } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   578
            Objects.requireNonNull(recipe, "Recipe is null");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   579
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   580
            for (int i = 0; i < recipe.length(); i++) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   581
                char c = recipe.charAt(i);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   582
                if (c == TAG_CONST) cCount++;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   583
                if (c == TAG_ARG)   oCount++;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   584
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   585
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   586
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   587
        if (oCount != concatType.parameterCount()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   588
            throw new StringConcatException(
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   589
                    "Mismatched number of concat arguments: recipe wants " +
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   590
                            oCount +
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   591
                            " arguments, but signature provides " +
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   592
                            concatType.parameterCount());
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   593
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   594
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   595
        if (cCount != constants.length) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   596
            throw new StringConcatException(
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   597
                    "Mismatched number of concat constants: recipe wants " +
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   598
                            cCount +
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   599
                            " constants, but only " +
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   600
                            constants.length +
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   601
                            " are passed");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   602
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   603
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   604
        if (!concatType.returnType().isAssignableFrom(String.class)) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   605
            throw new StringConcatException(
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   606
                    "The return type should be compatible with String, but it is " +
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   607
                            concatType.returnType());
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   608
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   609
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   610
        if (concatType.parameterCount() > MAX_INDY_CONCAT_ARG_SLOTS) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   611
            throw new StringConcatException("Too many concat argument slots: " +
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   612
                    concatType.parameterCount() +
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   613
                    ", can only accept " +
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   614
                    MAX_INDY_CONCAT_ARG_SLOTS);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   615
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   616
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   617
        MethodType mt = adaptType(concatType);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   618
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   619
        Recipe rec = new Recipe(recipe, constants);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   620
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   621
        MethodHandle mh;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   622
        if (CACHE_ENABLE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   623
            Key key = new Key(mt, rec);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   624
            mh = CACHE.get(key);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   625
            if (mh == null) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   626
                mh = generate(lookup, mt, rec);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   627
                CACHE.put(key, mh);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   628
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   629
        } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   630
            mh = generate(lookup, mt, rec);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   631
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   632
        return new ConstantCallSite(mh.asType(concatType));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   633
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   634
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   635
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   636
     * Adapt method type to an API we are going to use.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   637
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   638
     * This strips the concrete classes from the signatures, thus preventing
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   639
     * class leakage when we cache the concatenation stubs.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   640
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   641
     * @param args actual argument types
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   642
     * @return argument types the strategy is going to use
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   643
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   644
    private static MethodType adaptType(MethodType args) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   645
        Class<?>[] ptypes = args.parameterArray();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   646
        boolean changed = false;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   647
        for (int i = 0; i < ptypes.length; i++) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   648
            Class<?> ptype = ptypes[i];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   649
            if (!ptype.isPrimitive() &&
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   650
                    ptype != String.class &&
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   651
                    ptype != Object.class) { // truncate to Object
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   652
                ptypes[i] = Object.class;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   653
                changed = true;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   654
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   655
            // else other primitives or String or Object (unchanged)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   656
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   657
        return changed
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   658
                ? MethodType.methodType(args.returnType(), ptypes)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   659
                : args;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   660
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   661
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   662
    private static MethodHandle generate(Lookup lookup, MethodType mt, Recipe recipe) throws StringConcatException {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   663
        try {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   664
            switch (STRATEGY) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   665
                case BC_SB:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   666
                    return BytecodeStringBuilderStrategy.generate(lookup, mt, recipe, Mode.DEFAULT);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   667
                case BC_SB_SIZED:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   668
                    return BytecodeStringBuilderStrategy.generate(lookup, mt, recipe, Mode.SIZED);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   669
                case BC_SB_SIZED_EXACT:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   670
                    return BytecodeStringBuilderStrategy.generate(lookup, mt, recipe, Mode.SIZED_EXACT);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   671
                case MH_SB_SIZED:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   672
                    return MethodHandleStringBuilderStrategy.generate(mt, recipe, Mode.SIZED);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   673
                case MH_SB_SIZED_EXACT:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   674
                    return MethodHandleStringBuilderStrategy.generate(mt, recipe, Mode.SIZED_EXACT);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   675
                case MH_INLINE_SIZED_EXACT:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   676
                    return MethodHandleInlineCopyStrategy.generate(mt, recipe);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   677
                default:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   678
                    throw new StringConcatException("Concatenation strategy " + STRATEGY + " is not implemented");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   679
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   680
        } catch (Throwable t) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   681
            throw new StringConcatException("Generator failed", t);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   682
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   683
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   684
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   685
    private enum Mode {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   686
        DEFAULT(false, false),
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   687
        SIZED(true, false),
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   688
        SIZED_EXACT(true, true);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   689
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   690
        private final boolean sized;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   691
        private final boolean exact;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   692
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   693
        Mode(boolean sized, boolean exact) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   694
            this.sized = sized;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   695
            this.exact = exact;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   696
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   697
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   698
        boolean isSized() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   699
            return sized;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   700
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   701
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   702
        boolean isExact() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   703
            return exact;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   704
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   705
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   706
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   707
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   708
     * Bytecode StringBuilder strategy.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   709
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   710
     * <p>This strategy operates in three modes, gated by {@link Mode}.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   711
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   712
     * <p><b>{@link Strategy#BC_SB}: "bytecode StringBuilder".</b>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   713
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   714
     * <p>This strategy spins up the bytecode that has the same StringBuilder
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   715
     * chain javac would otherwise emit. This strategy uses only the public API,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   716
     * and comes as the baseline for the current JDK behavior. On other words,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   717
     * this strategy moves the javac generated bytecode to runtime. The
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   718
     * generated bytecode is loaded via Unsafe.defineAnonymousClass, but with
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   719
     * the caller class coming from the BSM -- in other words, the protection
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   720
     * guarantees are inherited from the method where invokedynamic was
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   721
     * originally called. This means, among other things, that the bytecode is
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   722
     * verified for all non-JDK uses.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   723
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   724
     * <p><b>{@link Strategy#BC_SB_SIZED}: "bytecode StringBuilder, but
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   725
     * sized".</b>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   726
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   727
     * <p>This strategy acts similarly to {@link Strategy#BC_SB}, but it also
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   728
     * tries to guess the capacity required for StringBuilder to accept all
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   729
     * arguments without resizing. This strategy only makes an educated guess:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   730
     * it only guesses the space required for known types (e.g. primitives and
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   731
     * Strings), but does not otherwise convert arguments. Therefore, the
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   732
     * capacity estimate may be wrong, and StringBuilder may have to
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   733
     * transparently resize or trim when doing the actual concatenation. While
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   734
     * this does not constitute a correctness issue (in the end, that what BC_SB
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   735
     * has to do anyway), this does pose a potential performance problem.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   736
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   737
     * <p><b>{@link Strategy#BC_SB_SIZED_EXACT}: "bytecode StringBuilder, but
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   738
     * sized exactly".</b>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   739
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   740
     * <p>This strategy improves on @link Strategy#BC_SB_SIZED}, by first
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   741
     * converting all arguments to String in order to get the exact capacity
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   742
     * StringBuilder should have. The conversion is done via the public
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   743
     * String.valueOf and/or Object.toString methods, and does not touch any
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   744
     * private String API.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   745
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   746
    private static final class BytecodeStringBuilderStrategy {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   747
        static final Unsafe UNSAFE = Unsafe.getUnsafe();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   748
        static final int CLASSFILE_VERSION = 52;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   749
        static final String NAME_FACTORY = "concat";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   750
        static final String CLASS_NAME = "java/lang/String$Concat";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   751
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   752
        private BytecodeStringBuilderStrategy() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   753
            // no instantiation
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   754
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   755
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   756
        private static MethodHandle generate(MethodHandles.Lookup lookup, MethodType args, Recipe recipe, Mode mode) throws Exception {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   757
            ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   758
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   759
            cw.visit(CLASSFILE_VERSION,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   760
                    ACC_SUPER + ACC_PUBLIC + ACC_FINAL + ACC_SYNTHETIC,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   761
                    CLASS_NAME,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   762
                    null,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   763
                    "java/lang/Object",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   764
                    null
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   765
            );
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   766
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   767
            MethodVisitor mv = cw.visitMethod(
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   768
                    ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   769
                    NAME_FACTORY,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   770
                    args.toMethodDescriptorString(),
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   771
                    null,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   772
                    null);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   773
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   774
            mv.visitAnnotation("Ljdk/internal/vm/annotation/ForceInline;", true);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   775
            mv.visitCode();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   776
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   777
            Class<?>[] arr = args.parameterArray();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   778
            boolean[] guaranteedNonNull = new boolean[arr.length];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   779
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   780
            if (mode.isExact()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   781
                /*
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   782
                    In exact mode, we need to convert all arguments to their String representations,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   783
                    as this allows to compute their String sizes exactly. We cannot use private
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   784
                    methods for primitives in here, therefore we need to convert those as well.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   785
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   786
                    We also record what arguments are guaranteed to be non-null as the result
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   787
                    of the conversion. String.valueOf does the null checks for us. The only
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   788
                    corner case to take care of is String.valueOf(Object) returning null itself.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   789
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   790
                    Also, if any conversion happened, then the slot indices in the incoming
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   791
                    arguments are not equal to the final local maps. The only case this may break
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   792
                    is when converting 2-slot long/double argument to 1-slot String. Therefore,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   793
                    we get away with tracking modified offset, since no conversion can overwrite
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   794
                    the upcoming the argument.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   795
                 */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   796
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   797
                int off = 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   798
                int modOff = 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   799
                for (int c = 0; c < arr.length; c++) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   800
                    Class<?> cl = arr[c];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   801
                    if (cl == String.class) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   802
                        if (off != modOff) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   803
                            mv.visitIntInsn(getLoadOpcode(cl), off);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   804
                            mv.visitIntInsn(ASTORE, modOff);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   805
                        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   806
                    } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   807
                        mv.visitIntInsn(getLoadOpcode(cl), off);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   808
                        mv.visitMethodInsn(
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   809
                                INVOKESTATIC,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   810
                                "java/lang/String",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   811
                                "valueOf",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   812
                                getStringValueOfDesc(cl),
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   813
                                false
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   814
                        );
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   815
                        mv.visitIntInsn(ASTORE, modOff);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   816
                        arr[c] = String.class;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   817
                        guaranteedNonNull[c] = cl.isPrimitive();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   818
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   819
                    off += getParameterSize(cl);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   820
                    modOff += getParameterSize(String.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   821
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   822
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   823
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   824
            if (mode.isSized()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   825
                /*
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   826
                    When operating in sized mode (this includes exact mode), it makes sense to make
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   827
                    StringBuilder append chains look familiar to OptimizeStringConcat. For that, we
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   828
                    need to do null-checks early, not make the append chain shape simpler.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   829
                 */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   830
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   831
                int off = 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   832
                for (RecipeElement el : recipe.getElements()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   833
                    switch (el.getTag()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   834
                        case CONST: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   835
                            // Guaranteed non-null, no null check required.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   836
                            break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   837
                        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   838
                        case ARG: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   839
                            // Null-checks are needed only for String arguments, and when a previous stage
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   840
                            // did not do implicit null-checks. If a String is null, we eagerly replace it
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   841
                            // with "null" constant. Note, we omit Objects here, because we don't call
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   842
                            // .length() on them down below.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   843
                            int ac = el.getArgPos();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   844
                            Class<?> cl = arr[ac];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   845
                            if (cl == String.class && !guaranteedNonNull[ac]) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   846
                                Label l0 = new Label();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   847
                                mv.visitIntInsn(ALOAD, off);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   848
                                mv.visitJumpInsn(IFNONNULL, l0);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   849
                                mv.visitLdcInsn("null");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   850
                                mv.visitIntInsn(ASTORE, off);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   851
                                mv.visitLabel(l0);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   852
                            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   853
                            off += getParameterSize(cl);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   854
                            break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   855
                        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   856
                        default:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   857
                            throw new StringConcatException("Unhandled tag: " + el.getTag());
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   858
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   859
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   860
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   861
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   862
            // Prepare StringBuilder instance
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   863
            mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   864
            mv.visitInsn(DUP);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   865
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   866
            if (mode.isSized()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   867
                /*
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   868
                    Sized mode requires us to walk through the arguments, and estimate the final length.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   869
                    In exact mode, this will operate on Strings only. This code would accumulate the
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   870
                    final length on stack.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   871
                 */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   872
                int len = 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   873
                int off = 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   874
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   875
                mv.visitInsn(ICONST_0);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   876
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   877
                for (RecipeElement el : recipe.getElements()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   878
                    switch (el.getTag()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   879
                        case CONST: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   880
                            Object cnst = el.getValue();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   881
                            len += cnst.toString().length();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   882
                            break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   883
                        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   884
                        case ARG: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   885
                            /*
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   886
                                If an argument is String, then we can call .length() on it. Sized/Exact modes have
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   887
                                converted arguments for us. If an argument is primitive, we can provide a guess
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   888
                                for its String representation size.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   889
                            */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   890
                            Class<?> cl = arr[el.getArgPos()];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   891
                            if (cl == String.class) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   892
                                mv.visitIntInsn(ALOAD, off);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   893
                                mv.visitMethodInsn(
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   894
                                        INVOKEVIRTUAL,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   895
                                        "java/lang/String",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   896
                                        "length",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   897
                                        "()I",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   898
                                        false
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   899
                                );
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   900
                                mv.visitInsn(IADD);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   901
                            } else if (cl.isPrimitive()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   902
                                len += estimateSize(cl);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   903
                            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   904
                            off += getParameterSize(cl);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   905
                            break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   906
                        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   907
                        default:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   908
                            throw new StringConcatException("Unhandled tag: " + el.getTag());
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   909
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   910
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   911
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   912
                // Constants have non-zero length, mix in
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   913
                if (len > 0) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   914
                    iconst(mv, len);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   915
                    mv.visitInsn(IADD);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   916
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   917
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   918
                mv.visitMethodInsn(
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   919
                        INVOKESPECIAL,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   920
                        "java/lang/StringBuilder",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   921
                        "<init>",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   922
                        "(I)V",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   923
                        false
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   924
                );
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   925
            } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   926
                mv.visitMethodInsn(
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   927
                        INVOKESPECIAL,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   928
                        "java/lang/StringBuilder",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   929
                        "<init>",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   930
                        "()V",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   931
                        false
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   932
                );
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   933
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   934
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   935
            // At this point, we have a blank StringBuilder on stack, fill it in with .append calls.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   936
            {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   937
                int off = 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   938
                for (RecipeElement el : recipe.getElements()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   939
                    String desc;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   940
                    switch (el.getTag()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   941
                        case CONST: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   942
                            Object cnst = el.getValue();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   943
                            mv.visitLdcInsn(cnst);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   944
                            desc = getSBAppendDesc(cnst.getClass());
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   945
                            break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   946
                        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   947
                        case ARG: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   948
                            Class<?> cl = arr[el.getArgPos()];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   949
                            mv.visitVarInsn(getLoadOpcode(cl), off);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   950
                            off += getParameterSize(cl);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   951
                            desc = getSBAppendDesc(cl);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   952
                            break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   953
                        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   954
                        default:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   955
                            throw new StringConcatException("Unhandled tag: " + el.getTag());
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   956
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   957
                    mv.visitMethodInsn(
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   958
                            INVOKEVIRTUAL,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   959
                            "java/lang/StringBuilder",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   960
                            "append",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   961
                            desc,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   962
                            false
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   963
                    );
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   964
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   965
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   966
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   967
            if (DEBUG && mode.isExact()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   968
                /*
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   969
                    Exactness checks compare the final StringBuilder.capacity() with a resulting
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   970
                    String.length(). If these values disagree, that means StringBuilder had to perform
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   971
                    storage trimming, which defeats the purpose of exact strategies.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   972
                 */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   973
35639
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   974
                /*
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   975
                   The logic for this check is as follows:
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   976
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   977
                     Stack before:     Op:
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   978
                      (SB)              dup, dup
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   979
                      (SB, SB, SB)      capacity()
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   980
                      (int, SB, SB)     swap
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   981
                      (SB, int, SB)     toString()
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   982
                      (S, int, SB)      length()
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   983
                      (int, int, SB)    if_icmpeq
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   984
                      (SB)              <end>
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   985
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   986
                   Note that it leaves the same StringBuilder on exit, like the one on enter.
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   987
                 */
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   988
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
   989
                mv.visitInsn(DUP);
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   990
                mv.visitInsn(DUP);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   991
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   992
                mv.visitMethodInsn(
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   993
                        INVOKEVIRTUAL,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   994
                        "java/lang/StringBuilder",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   995
                        "capacity",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   996
                        "()I",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   997
                        false
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   998
                );
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   999
35639
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
  1000
                mv.visitInsn(SWAP);
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1001
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1002
                mv.visitMethodInsn(
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1003
                        INVOKEVIRTUAL,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1004
                        "java/lang/StringBuilder",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1005
                        "toString",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1006
                        "()Ljava/lang/String;",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1007
                        false
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1008
                );
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1009
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1010
                mv.visitMethodInsn(
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1011
                        INVOKEVIRTUAL,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1012
                        "java/lang/String",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1013
                        "length",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1014
                        "()I",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1015
                        false
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1016
                );
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1017
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1018
                Label l0 = new Label();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1019
                mv.visitJumpInsn(IF_ICMPEQ, l0);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1020
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1021
                mv.visitTypeInsn(NEW, "java/lang/AssertionError");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1022
                mv.visitInsn(DUP);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1023
                mv.visitLdcInsn("Failed exactness check");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1024
                mv.visitMethodInsn(INVOKESPECIAL,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1025
                        "java/lang/AssertionError",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1026
                        "<init>",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1027
                        "(Ljava/lang/Object;)V",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1028
                        false);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1029
                mv.visitInsn(ATHROW);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1030
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1031
                mv.visitLabel(l0);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1032
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1033
35639
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
  1034
            mv.visitMethodInsn(
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
  1035
                    INVOKEVIRTUAL,
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
  1036
                    "java/lang/StringBuilder",
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
  1037
                    "toString",
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
  1038
                    "()Ljava/lang/String;",
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
  1039
                    false
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
  1040
            );
f34e7e8b4eac 8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested
shade
parents: 35401
diff changeset
  1041
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1042
            mv.visitInsn(ARETURN);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1043
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1044
            mv.visitMaxs(-1, -1);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1045
            mv.visitEnd();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1046
            cw.visitEnd();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1047
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1048
            Class<?> targetClass = lookup.lookupClass();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1049
            final byte[] classBytes = cw.toByteArray();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1050
            final Class<?> innerClass = UNSAFE.defineAnonymousClass(targetClass, classBytes, null);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1051
35776
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
  1052
            if (DUMPER != null) {
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
  1053
                DUMPER.dumpClass(innerClass.getName(), classBytes);
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
  1054
            }
dd5df83fcc0d 8149459: StringConcatFactory should be synced up with LambdaMetafactory
shade
parents: 35700
diff changeset
  1055
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1056
            try {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1057
                UNSAFE.ensureClassInitialized(innerClass);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1058
                return lookup.findStatic(innerClass, NAME_FACTORY, args);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1059
            } catch (ReflectiveOperationException e) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1060
                throw new StringConcatException("Exception finding constructor", e);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1061
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1062
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1063
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1064
        private static String getSBAppendDesc(Class<?> cl) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1065
            if (cl.isPrimitive()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1066
                if (cl == Integer.TYPE || cl == Byte.TYPE || cl == Short.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1067
                    return "(I)Ljava/lang/StringBuilder;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1068
                } else if (cl == Boolean.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1069
                    return "(Z)Ljava/lang/StringBuilder;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1070
                } else if (cl == Character.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1071
                    return "(C)Ljava/lang/StringBuilder;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1072
                } else if (cl == Double.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1073
                    return "(D)Ljava/lang/StringBuilder;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1074
                } else if (cl == Float.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1075
                    return "(F)Ljava/lang/StringBuilder;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1076
                } else if (cl == Long.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1077
                    return "(J)Ljava/lang/StringBuilder;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1078
                } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1079
                    throw new IllegalStateException("Unhandled primitive StringBuilder.append: " + cl);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1080
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1081
            } else if (cl == String.class) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1082
                return "(Ljava/lang/String;)Ljava/lang/StringBuilder;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1083
            } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1084
                return "(Ljava/lang/Object;)Ljava/lang/StringBuilder;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1085
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1086
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1087
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1088
        private static String getStringValueOfDesc(Class<?> cl) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1089
            if (cl.isPrimitive()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1090
                if (cl == Integer.TYPE || cl == Byte.TYPE || cl == Short.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1091
                    return "(I)Ljava/lang/String;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1092
                } else if (cl == Boolean.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1093
                    return "(Z)Ljava/lang/String;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1094
                } else if (cl == Character.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1095
                    return "(C)Ljava/lang/String;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1096
                } else if (cl == Double.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1097
                    return "(D)Ljava/lang/String;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1098
                } else if (cl == Float.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1099
                    return "(F)Ljava/lang/String;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1100
                } else if (cl == Long.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1101
                    return "(J)Ljava/lang/String;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1102
                } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1103
                    throw new IllegalStateException("Unhandled String.valueOf: " + cl);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1104
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1105
            } else if (cl == String.class) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1106
                return "(Ljava/lang/String;)Ljava/lang/String;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1107
            } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1108
                return "(Ljava/lang/Object;)Ljava/lang/String;";
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1109
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1110
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1111
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1112
        /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1113
         * The following method is copied from
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1114
         * org.objectweb.asm.commons.InstructionAdapter. Part of ASM: a very small
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1115
         * and fast Java bytecode manipulation framework.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1116
         * Copyright (c) 2000-2005 INRIA, France Telecom All rights reserved.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1117
         */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1118
        private static void iconst(MethodVisitor mv, final int cst) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1119
            if (cst >= -1 && cst <= 5) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1120
                mv.visitInsn(Opcodes.ICONST_0 + cst);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1121
            } else if (cst >= Byte.MIN_VALUE && cst <= Byte.MAX_VALUE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1122
                mv.visitIntInsn(Opcodes.BIPUSH, cst);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1123
            } else if (cst >= Short.MIN_VALUE && cst <= Short.MAX_VALUE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1124
                mv.visitIntInsn(Opcodes.SIPUSH, cst);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1125
            } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1126
                mv.visitLdcInsn(cst);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1127
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1128
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1129
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1130
        private static int getLoadOpcode(Class<?> c) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1131
            if (c == Void.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1132
                throw new InternalError("Unexpected void type of load opcode");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1133
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1134
            return ILOAD + getOpcodeOffset(c);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1135
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1136
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1137
        private static int getOpcodeOffset(Class<?> c) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1138
            if (c.isPrimitive()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1139
                if (c == Long.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1140
                    return 1;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1141
                } else if (c == Float.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1142
                    return 2;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1143
                } else if (c == Double.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1144
                    return 3;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1145
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1146
                return 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1147
            } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1148
                return 4;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1149
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1150
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1151
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1152
        private static int getParameterSize(Class<?> c) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1153
            if (c == Void.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1154
                return 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1155
            } else if (c == Long.TYPE || c == Double.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1156
                return 2;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1157
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1158
            return 1;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1159
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1160
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1161
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1162
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1163
     * MethodHandle StringBuilder strategy.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1164
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1165
     * <p>This strategy operates in two modes, gated by {@link Mode}.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1166
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1167
     * <p><b>{@link Strategy#MH_SB_SIZED}: "MethodHandles StringBuilder,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1168
     * sized".</b>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1169
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1170
     * <p>This strategy avoids spinning up the bytecode by building the
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1171
     * computation on MethodHandle combinators. The computation is built with
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1172
     * public MethodHandle APIs, resolved from a public Lookup sequence, and
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1173
     * ends up calling the public StringBuilder API. Therefore, this strategy
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1174
     * does not use any private API at all, even the Unsafe.defineAnonymousClass,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1175
     * since everything is handled under cover by java.lang.invoke APIs.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1176
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1177
     * <p><b>{@link Strategy#MH_SB_SIZED_EXACT}: "MethodHandles StringBuilder,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1178
     * sized exactly".</b>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1179
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1180
     * <p>This strategy improves on @link Strategy#MH_SB_SIZED}, by first
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1181
     * converting all arguments to String in order to get the exact capacity
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1182
     * StringBuilder should have. The conversion is done via the public
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1183
     * String.valueOf and/or Object.toString methods, and does not touch any
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1184
     * private String API.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1185
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1186
    private static final class MethodHandleStringBuilderStrategy {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1187
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1188
        private MethodHandleStringBuilderStrategy() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1189
            // no instantiation
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1190
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1191
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1192
        private static MethodHandle generate(MethodType mt, Recipe recipe, Mode mode) throws Exception {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1193
            int pc = mt.parameterCount();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1194
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1195
            Class<?>[] ptypes = mt.parameterArray();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1196
            MethodHandle[] filters = new MethodHandle[ptypes.length];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1197
            for (int i = 0; i < ptypes.length; i++) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1198
                MethodHandle filter;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1199
                switch (mode) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1200
                    case SIZED:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1201
                        // In sized mode, we convert all references and floats/doubles
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1202
                        // to String: there is no specialization for different
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1203
                        // classes in StringBuilder API, and it will convert to
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1204
                        // String internally anyhow.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1205
                        filter = Stringifiers.forMost(ptypes[i]);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1206
                        break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1207
                    case SIZED_EXACT:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1208
                        // In exact mode, we convert everything to String:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1209
                        // this helps to compute the storage exactly.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1210
                        filter = Stringifiers.forAny(ptypes[i]);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1211
                        break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1212
                    default:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1213
                        throw new StringConcatException("Not supported");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1214
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1215
                if (filter != null) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1216
                    filters[i] = filter;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1217
                    ptypes[i] = filter.type().returnType();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1218
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1219
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1220
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1221
            List<Class<?>> ptypesList = Arrays.asList(ptypes);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1222
            MethodHandle[] lengthers = new MethodHandle[pc];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1223
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1224
            // Figure out lengths: constants' lengths can be deduced on the spot.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1225
            // All reference arguments were filtered to String in the combinators below, so we can
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1226
            // call the usual String.length(). Primitive values string sizes can be estimated.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1227
            int initial = 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1228
            for (RecipeElement el : recipe.getElements()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1229
                switch (el.getTag()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1230
                    case CONST: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1231
                        Object cnst = el.getValue();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1232
                        initial += cnst.toString().length();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1233
                        break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1234
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1235
                    case ARG: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1236
                        final int i = el.getArgPos();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1237
                        Class<?> type = ptypesList.get(i);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1238
                        if (type.isPrimitive()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1239
                            MethodHandle est = MethodHandles.constant(int.class, estimateSize(type));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1240
                            est = MethodHandles.dropArguments(est, 0, type);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1241
                            lengthers[i] = est;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1242
                        } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1243
                            lengthers[i] = STRING_LENGTH;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1244
                        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1245
                        break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1246
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1247
                    default:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1248
                        throw new StringConcatException("Unhandled tag: " + el.getTag());
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1249
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1250
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1251
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1252
            // Create (StringBuilder, <args>) shape for appending:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1253
            MethodHandle builder = MethodHandles.dropArguments(MethodHandles.identity(StringBuilder.class), 1, ptypesList);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1254
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1255
            // Compose append calls. This is done in reverse because the application order is
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1256
            // reverse as well.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1257
            for (RecipeElement el : recipe.getElementsReversed()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1258
                MethodHandle appender;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1259
                switch (el.getTag()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1260
                    case CONST: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1261
                        Object constant = el.getValue();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1262
                        MethodHandle mh = appender(adaptToStringBuilder(constant.getClass()));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1263
                        appender = MethodHandles.insertArguments(mh, 1, constant);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1264
                        break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1265
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1266
                    case ARG: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1267
                        int ac = el.getArgPos();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1268
                        appender = appender(ptypesList.get(ac));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1269
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1270
                        // Insert dummy arguments to match the prefix in the signature.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1271
                        // The actual appender argument will be the ac-ith argument.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1272
                        if (ac != 0) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1273
                            appender = MethodHandles.dropArguments(appender, 1, ptypesList.subList(0, ac));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1274
                        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1275
                        break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1276
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1277
                    default:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1278
                        throw new StringConcatException("Unhandled tag: " + el.getTag());
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1279
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1280
                builder = MethodHandles.foldArguments(builder, appender);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1281
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1282
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1283
            // Build the sub-tree that adds the sizes and produces a StringBuilder:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1284
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1285
            // a) Start with the reducer that accepts all arguments, plus one
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1286
            //    slot for the initial value. Inject the initial value right away.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1287
            //    This produces (<ints>)int shape:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1288
            MethodHandle sum = getReducerFor(pc + 1);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1289
            MethodHandle adder = MethodHandles.insertArguments(sum, 0, initial);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1290
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1291
            // b) Apply lengthers to transform arguments to lengths, producing (<args>)int
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1292
            adder = MethodHandles.filterArguments(adder, 0, lengthers);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1293
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1294
            // c) Instantiate StringBuilder (<args>)int -> (<args>)StringBuilder
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1295
            MethodHandle newBuilder = MethodHandles.filterReturnValue(adder, NEW_STRING_BUILDER);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1296
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1297
            // d) Fold in StringBuilder constructor, this produces (<args>)StringBuilder
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1298
            MethodHandle mh = MethodHandles.foldArguments(builder, newBuilder);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1299
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1300
            // Convert non-primitive arguments to Strings
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1301
            mh = MethodHandles.filterArguments(mh, 0, filters);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1302
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1303
            // Convert (<args>)StringBuilder to (<args>)String
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1304
            if (DEBUG && mode.isExact()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1305
                mh = MethodHandles.filterReturnValue(mh, BUILDER_TO_STRING_CHECKED);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1306
            } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1307
                mh = MethodHandles.filterReturnValue(mh, BUILDER_TO_STRING);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1308
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1309
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1310
            return mh;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1311
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1312
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1313
        private static MethodHandle getReducerFor(int cnt) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1314
            return SUMMERS.computeIfAbsent(cnt, SUMMER);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1315
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1316
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1317
        private static MethodHandle appender(Class<?> appendType) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1318
            MethodHandle appender = lookupVirtual(MethodHandles.publicLookup(), StringBuilder.class, "append",
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1319
                    StringBuilder.class, adaptToStringBuilder(appendType));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1320
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1321
            // appenders should return void, this would not modify the target signature during folding
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1322
            MethodType nt = MethodType.methodType(void.class, StringBuilder.class, appendType);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1323
            return appender.asType(nt);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1324
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1325
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1326
        private static String toStringChecked(StringBuilder sb) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1327
            String s = sb.toString();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1328
            if (s.length() != sb.capacity()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1329
                throw new AssertionError("Exactness check failed: result length = " + s.length() + ", buffer capacity = " + sb.capacity());
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1330
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1331
            return s;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1332
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1333
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1334
        private static int sum(int v1, int v2) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1335
            return v1 + v2;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1336
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1337
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1338
        private static int sum(int v1, int v2, int v3) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1339
            return v1 + v2 + v3;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1340
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1341
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1342
        private static int sum(int v1, int v2, int v3, int v4) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1343
            return v1 + v2 + v3 + v4;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1344
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1345
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1346
        private static int sum(int v1, int v2, int v3, int v4, int v5) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1347
            return v1 + v2 + v3 + v4 + v5;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1348
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1349
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1350
        private static int sum(int v1, int v2, int v3, int v4, int v5, int v6) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1351
            return v1 + v2 + v3 + v4 + v5 + v6;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1352
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1353
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1354
        private static int sum(int v1, int v2, int v3, int v4, int v5, int v6, int v7) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1355
            return v1 + v2 + v3 + v4 + v5 + v6 + v7;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1356
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1357
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1358
        private static int sum(int v1, int v2, int v3, int v4, int v5, int v6, int v7, int v8) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1359
            return v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1360
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1361
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1362
        private static int sum(int initial, int[] vs) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1363
            int sum = initial;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1364
            for (int v : vs) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1365
                sum += v;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1366
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1367
            return sum;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1368
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1369
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1370
        private static final ConcurrentMap<Integer, MethodHandle> SUMMERS;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1371
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1372
        // This one is deliberately non-lambdified to optimize startup time:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1373
        private static final Function<Integer, MethodHandle> SUMMER = new Function<Integer, MethodHandle>() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1374
            @Override
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1375
            public MethodHandle apply(Integer cnt) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1376
                if (cnt == 1) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1377
                    return MethodHandles.identity(int.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1378
                } else if (cnt <= 8) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1379
                    // Variable-arity collectors are not as efficient as small-count methods,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1380
                    // unroll some initial sizes.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1381
                    Class<?>[] cls = new Class<?>[cnt];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1382
                    Arrays.fill(cls, int.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1383
                    return lookupStatic(Lookup.IMPL_LOOKUP, MethodHandleStringBuilderStrategy.class, "sum", int.class, cls);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1384
                } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1385
                    return lookupStatic(Lookup.IMPL_LOOKUP, MethodHandleStringBuilderStrategy.class, "sum", int.class, int.class, int[].class)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1386
                            .asCollector(int[].class, cnt - 1);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1387
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1388
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1389
        };
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1390
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1391
        private static final MethodHandle NEW_STRING_BUILDER, STRING_LENGTH, BUILDER_TO_STRING, BUILDER_TO_STRING_CHECKED;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1392
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1393
        static {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1394
            SUMMERS = new ConcurrentHashMap<>();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1395
            Lookup publicLookup = MethodHandles.publicLookup();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1396
            NEW_STRING_BUILDER = lookupConstructor(publicLookup, StringBuilder.class, int.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1397
            STRING_LENGTH = lookupVirtual(publicLookup, String.class, "length", int.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1398
            BUILDER_TO_STRING = lookupVirtual(publicLookup, StringBuilder.class, "toString", String.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1399
            if (DEBUG) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1400
                BUILDER_TO_STRING_CHECKED = lookupStatic(MethodHandles.Lookup.IMPL_LOOKUP,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1401
                        MethodHandleStringBuilderStrategy.class, "toStringChecked", String.class, StringBuilder.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1402
            } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1403
                BUILDER_TO_STRING_CHECKED = null;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1404
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1405
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1406
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1407
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1408
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1409
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1410
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1411
     * <p><b>{@link Strategy#MH_INLINE_SIZED_EXACT}: "MethodHandles inline,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1412
     * sized exactly".</b>
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1413
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1414
     * <p>This strategy replicates what StringBuilders are doing: it builds the
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1415
     * byte[] array on its own and passes that byte[] array to String
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1416
     * constructor. This strategy requires access to some private APIs in JDK,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1417
     * most notably, the read-only Integer/Long.stringSize methods that measure
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1418
     * the character length of the integers, and the private String constructor
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1419
     * that accepts byte[] arrays without copying. While this strategy assumes a
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1420
     * particular implementation details for String, this opens the door for
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1421
     * building a very optimal concatenation sequence. This is the only strategy
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1422
     * that requires porting if there are private JDK changes occur.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1423
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1424
    private static final class MethodHandleInlineCopyStrategy {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1425
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1426
        private MethodHandleInlineCopyStrategy() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1427
            // no instantiation
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1428
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1429
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1430
        static MethodHandle generate(MethodType mt, Recipe recipe) throws Throwable {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1431
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1432
            // Create filters and obtain filtered parameter types. Filters would be used in the beginning
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1433
            // to convert the incoming arguments into the arguments we can process (e.g. Objects -> Strings).
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1434
            // The filtered argument type list is used all over in the combinators below.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1435
            Class<?>[] ptypes = mt.parameterArray();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1436
            MethodHandle[] filters = null;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1437
            for (int i = 0; i < ptypes.length; i++) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1438
                MethodHandle filter = Stringifiers.forMost(ptypes[i]);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1439
                if (filter != null) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1440
                    if (filters == null) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1441
                        filters = new MethodHandle[ptypes.length];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1442
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1443
                    filters[i] = filter;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1444
                    ptypes[i] = filter.type().returnType();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1445
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1446
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1447
            List<Class<?>> ptypesList = Arrays.asList(ptypes);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1448
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1449
            // Start building the combinator tree. The tree "starts" with (<parameters>)String, and "finishes"
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1450
            // with the (int, byte[], byte)String in String helper. The combinators are assembled bottom-up,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1451
            // which makes the code arguably hard to read.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1452
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1453
            // Drop all remaining parameter types, leave only helper arguments:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1454
            MethodHandle mh;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1455
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1456
            mh = MethodHandles.dropArguments(NEW_STRING, 2, ptypes);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1457
            mh = MethodHandles.dropArguments(mh, 0, int.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1458
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1459
            // In debug mode, check that remaining index is zero.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1460
            if (DEBUG) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1461
                mh = MethodHandles.filterArgument(mh, 0, CHECK_INDEX);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1462
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1463
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1464
            // Mix in prependers. This happens when (int, byte[], byte) = (index, storage, coder) is already
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1465
            // known from the combinators below. We are assembling the string backwards, so "index" is the
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1466
            // *ending* index.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1467
            for (RecipeElement el : recipe.getElements()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1468
                MethodHandle prepender;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1469
                switch (el.getTag()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1470
                    case CONST: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1471
                        Object cnst = el.getValue();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1472
                        prepender = MethodHandles.insertArguments(prepender(cnst.getClass()), 3, cnst);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1473
                        break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1474
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1475
                    case ARG: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1476
                        int pos = el.getArgPos();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1477
                        prepender = selectArgument(prepender(ptypesList.get(pos)), 3, ptypesList, pos);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1478
                        break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1479
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1480
                    default:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1481
                        throw new StringConcatException("Unhandled tag: " + el.getTag());
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1482
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1483
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1484
                // Remove "old" index from arguments
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1485
                mh = MethodHandles.dropArguments(mh, 1, int.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1486
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1487
                // Do the prepend, and put "new" index at index 0
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1488
                mh = MethodHandles.foldArguments(mh, prepender);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1489
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1490
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1491
            // Prepare the argument list for prepending. The tree below would instantiate
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1492
            // the storage byte[] into argument 0, so we need to swap "storage" and "index".
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1493
            // The index at this point equals to "size", and resides at argument 1.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1494
            {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1495
                MethodType nmt = mh.type()
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1496
                        .changeParameterType(0, byte[].class)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1497
                        .changeParameterType(1, int.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1498
                mh = MethodHandles.permuteArguments(mh, nmt, swap10(nmt.parameterCount()));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1499
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1500
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1501
            // Fold in byte[] instantiation at argument 0.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1502
            MethodHandle combiner = MethodHandles.dropArguments(NEW_ARRAY, 2, ptypesList);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1503
            mh = MethodHandles.foldArguments(mh, combiner);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1504
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1505
            // Start combining length and coder mixers.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1506
            //
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1507
            // Length is easy: constant lengths can be computed on the spot, and all non-constant
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1508
            // shapes have been either converted to Strings, or explicit methods for getting the
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1509
            // string length out of primitives are provided.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1510
            //
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1511
            // Coders are more interesting. Only Object, String and char arguments (and constants)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1512
            // can have non-Latin1 encoding. It is easier to blindly convert constants to String,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1513
            // and deduce the coder from there. Arguments would be either converted to Strings
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1514
            // during the initial filtering, or handled by primitive specializations in CODER_MIXERS.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1515
            //
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1516
            // The method handle shape after all length and coder mixers is:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1517
            //   (int, byte, <args>)String = ("index", "coder", <args>)
35700
b933119b8f84 8148869: StringConcatFactory MH_INLINE_SIZED_EXACT strategy does not work with -XX:-CompactStrings
shade
parents: 35639
diff changeset
  1518
            byte initialCoder = INITIAL_CODER;
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1519
            int initialLen = 0;    // initial length, in characters
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1520
            for (RecipeElement el : recipe.getElements()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1521
                switch (el.getTag()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1522
                    case CONST: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1523
                        Object constant = el.getValue();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1524
                        String s = constant.toString();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1525
                        initialCoder = (byte) coderMixer(String.class).invoke(initialCoder, s);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1526
                        initialLen += s.length();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1527
                        break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1528
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1529
                    case ARG: {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1530
                        int ac = el.getArgPos();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1531
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1532
                        Class<?> argClass = ptypesList.get(ac);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1533
                        MethodHandle lm = selectArgument(lengthMixer(argClass), 1, ptypesList, ac);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1534
                        lm = MethodHandles.dropArguments(lm, 0, byte.class); // (*)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1535
                        lm = MethodHandles.dropArguments(lm, 2, byte.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1536
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1537
                        MethodHandle cm = selectArgument(coderMixer(argClass),  1, ptypesList, ac);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1538
                        cm = MethodHandles.dropArguments(cm, 0, int.class);  // (**)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1539
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1540
                        // Read this bottom up:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1541
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1542
                        // 4. Drop old index and coder, producing ("new-index", "new-coder", <args>)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1543
                        mh = MethodHandles.dropArguments(mh, 2, int.class, byte.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1544
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1545
                        // 3. Compute "new-index", producing ("new-index", "new-coder", "old-index", "old-coder", <args>)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1546
                        //    Length mixer ignores both "new-coder" and "old-coder" due to dropArguments above (*)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1547
                        mh = MethodHandles.foldArguments(mh, lm);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1548
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1549
                        // 2. Compute "new-coder", producing ("new-coder", "old-index", "old-coder", <args>)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1550
                        //    Coder mixer ignores the "old-index" arg due to dropArguments above (**)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1551
                        mh = MethodHandles.foldArguments(mh, cm);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1552
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1553
                        // 1. The mh shape here is ("old-index", "old-coder", <args>)
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1554
                        break;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1555
                    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1556
                    default:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1557
                        throw new StringConcatException("Unhandled tag: " + el.getTag());
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1558
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1559
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1560
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1561
            // Insert initial lengths and coders here.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1562
            // The method handle shape here is (<args>).
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1563
            mh = MethodHandles.insertArguments(mh, 0, initialLen, initialCoder);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1564
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1565
            // Apply filters, converting the arguments:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1566
            if (filters != null) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1567
                mh = MethodHandles.filterArguments(mh, 0, filters);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1568
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1569
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1570
            return mh;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1571
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1572
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1573
        private static int[] swap10(int count) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1574
            int[] perm = new int[count];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1575
            perm[0] = 1;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1576
            perm[1] = 0;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1577
            for (int i = 2; i < count; i++) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1578
                perm[i] = i;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1579
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1580
            return perm;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1581
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1582
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1583
        // Adapts: (...prefix..., parameter[pos])R -> (...prefix..., ...parameters...)R
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1584
        private static MethodHandle selectArgument(MethodHandle mh, int prefix, List<Class<?>> ptypes, int pos) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1585
            if (pos == 0) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1586
                return MethodHandles.dropArguments(mh, prefix + 1, ptypes.subList(1, ptypes.size()));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1587
            } else if (pos == ptypes.size() - 1) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1588
                return MethodHandles.dropArguments(mh, prefix, ptypes.subList(0, ptypes.size() - 1));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1589
            } else { // 0 < pos < ptypes.size() - 1
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1590
                MethodHandle t = MethodHandles.dropArguments(mh, prefix, ptypes.subList(0, pos));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1591
                return MethodHandles.dropArguments(t, prefix + 1 + pos, ptypes.subList(pos + 1, ptypes.size()));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1592
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1593
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1594
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1595
        @ForceInline
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1596
        private static byte[] newArray(int length, byte coder) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1597
            return new byte[length << coder];
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1598
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1599
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1600
        @ForceInline
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1601
        private static int checkIndex(int index) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1602
            if (index != 0) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1603
                throw new AssertionError("Exactness check failed: " + index + " characters left in the buffer.");
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1604
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1605
            return index;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1606
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1607
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1608
        private static MethodHandle prepender(Class<?> cl) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1609
            return PREPENDERS.computeIfAbsent(cl, PREPEND);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1610
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1611
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1612
        private static MethodHandle coderMixer(Class<?> cl) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1613
            return CODER_MIXERS.computeIfAbsent(cl, CODER_MIX);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1614
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1615
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1616
        private static MethodHandle lengthMixer(Class<?> cl) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1617
            return LENGTH_MIXERS.computeIfAbsent(cl, LENGTH_MIX);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1618
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1619
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1620
        // This one is deliberately non-lambdified to optimize startup time:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1621
        private static final Function<Class<?>, MethodHandle> PREPEND = new Function<Class<?>, MethodHandle>() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1622
            @Override
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1623
            public MethodHandle apply(Class<?> c) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1624
                return lookupStatic(Lookup.IMPL_LOOKUP, STRING_HELPER, "prepend", int.class, int.class, byte[].class, byte.class, c);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1625
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1626
        };
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1627
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1628
        // This one is deliberately non-lambdified to optimize startup time:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1629
        private static final Function<Class<?>, MethodHandle> CODER_MIX = new Function<Class<?>, MethodHandle>() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1630
            @Override
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1631
            public MethodHandle apply(Class<?> c) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1632
                return lookupStatic(Lookup.IMPL_LOOKUP, STRING_HELPER, "mixCoder", byte.class, byte.class, c);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1633
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1634
        };
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1635
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1636
        // This one is deliberately non-lambdified to optimize startup time:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1637
        private static final Function<Class<?>, MethodHandle> LENGTH_MIX = new Function<Class<?>, MethodHandle>() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1638
            @Override
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1639
            public MethodHandle apply(Class<?> c) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1640
                return lookupStatic(Lookup.IMPL_LOOKUP, STRING_HELPER, "mixLen", int.class, int.class, c);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1641
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1642
        };
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1643
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1644
        private static final MethodHandle NEW_STRING;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1645
        private static final MethodHandle CHECK_INDEX;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1646
        private static final MethodHandle NEW_ARRAY;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1647
        private static final ConcurrentMap<Class<?>, MethodHandle> PREPENDERS;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1648
        private static final ConcurrentMap<Class<?>, MethodHandle> LENGTH_MIXERS;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1649
        private static final ConcurrentMap<Class<?>, MethodHandle> CODER_MIXERS;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1650
        private static final Class<?> STRING_HELPER;
35700
b933119b8f84 8148869: StringConcatFactory MH_INLINE_SIZED_EXACT strategy does not work with -XX:-CompactStrings
shade
parents: 35639
diff changeset
  1651
        private static final byte INITIAL_CODER;
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1652
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1653
        static {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1654
            try {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1655
                STRING_HELPER = Class.forName("java.lang.StringConcatHelper");
35700
b933119b8f84 8148869: StringConcatFactory MH_INLINE_SIZED_EXACT strategy does not work with -XX:-CompactStrings
shade
parents: 35639
diff changeset
  1656
                MethodHandle initCoder = lookupStatic(Lookup.IMPL_LOOKUP, STRING_HELPER, "initialCoder", byte.class);
b933119b8f84 8148869: StringConcatFactory MH_INLINE_SIZED_EXACT strategy does not work with -XX:-CompactStrings
shade
parents: 35639
diff changeset
  1657
                INITIAL_CODER = (byte) initCoder.invoke();
b933119b8f84 8148869: StringConcatFactory MH_INLINE_SIZED_EXACT strategy does not work with -XX:-CompactStrings
shade
parents: 35639
diff changeset
  1658
            } catch (Throwable e) {
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1659
                throw new AssertionError(e);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1660
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1661
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1662
            PREPENDERS = new ConcurrentHashMap<>();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1663
            LENGTH_MIXERS = new ConcurrentHashMap<>();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1664
            CODER_MIXERS = new ConcurrentHashMap<>();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1665
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1666
            NEW_STRING = lookupStatic(Lookup.IMPL_LOOKUP, STRING_HELPER, "newString", String.class, byte[].class, byte.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1667
            NEW_ARRAY  = lookupStatic(Lookup.IMPL_LOOKUP, MethodHandleInlineCopyStrategy.class, "newArray", byte[].class, int.class, byte.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1668
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1669
            if (DEBUG) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1670
                CHECK_INDEX = lookupStatic(Lookup.IMPL_LOOKUP, MethodHandleInlineCopyStrategy.class, "checkIndex", int.class, int.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1671
            } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1672
                CHECK_INDEX = null;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1673
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1674
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1675
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1676
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1677
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1678
     * Public gateways to public "stringify" methods. These methods have the form String apply(T obj), and normally
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1679
     * delegate to {@code String.valueOf}, depending on argument's type.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1680
     */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1681
    private static final class Stringifiers {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1682
        private Stringifiers() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1683
            // no instantiation
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1684
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1685
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1686
        // This one is deliberately non-lambdified to optimize startup time:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1687
        private static final Function<Class<?>, MethodHandle> MOST = new Function<Class<?>, MethodHandle>() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1688
            @Override
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1689
            public MethodHandle apply(Class<?> cl) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1690
                MethodHandle mhObject = lookupStatic(Lookup.PUBLIC_LOOKUP, String.class, "valueOf", String.class, Object.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1691
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1692
                // We need the additional conversion here, because String.valueOf(Object) may return null.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1693
                // String conversion rules in Java state we need to produce "null" String in this case.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1694
                // It can be easily done with applying valueOf the second time.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1695
                MethodHandle mhObjectNoNulls = MethodHandles.filterReturnValue(mhObject,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1696
                        mhObject.asType(MethodType.methodType(String.class, String.class)));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1697
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1698
                if (cl == String.class) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1699
                    return mhObject;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1700
                } else if (cl == float.class) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1701
                    return lookupStatic(Lookup.PUBLIC_LOOKUP, String.class, "valueOf", String.class, float.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1702
                } else if (cl == double.class) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1703
                    return lookupStatic(Lookup.PUBLIC_LOOKUP, String.class, "valueOf", String.class, double.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1704
                } else if (!cl.isPrimitive()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1705
                    return mhObjectNoNulls;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1706
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1707
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1708
                return null;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1709
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1710
        };
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1711
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1712
        // This one is deliberately non-lambdified to optimize startup time:
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1713
        private static final Function<Class<?>, MethodHandle> ANY = new Function<Class<?>, MethodHandle>() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1714
            @Override
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1715
            public MethodHandle apply(Class<?> cl) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1716
                MethodHandle mh = MOST.apply(cl);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1717
                if (mh != null) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1718
                    return mh;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1719
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1720
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1721
                if (cl == byte.class || cl == short.class || cl == int.class) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1722
                    return lookupStatic(Lookup.PUBLIC_LOOKUP, String.class, "valueOf", String.class, int.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1723
                } else if (cl == boolean.class) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1724
                    return lookupStatic(Lookup.PUBLIC_LOOKUP, String.class, "valueOf", String.class, boolean.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1725
                } else if (cl == char.class) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1726
                    return lookupStatic(Lookup.PUBLIC_LOOKUP, String.class, "valueOf", String.class, char.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1727
                } else if (cl == long.class) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1728
                    return lookupStatic(Lookup.PUBLIC_LOOKUP, String.class, "valueOf", String.class, long.class);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1729
                } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1730
                    throw new IllegalStateException("Unknown class: " + cl);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1731
                }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1732
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1733
        };
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1734
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1735
        private static final ConcurrentMap<Class<?>, MethodHandle> STRINGIFIERS_MOST = new ConcurrentHashMap<>();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1736
        private static final ConcurrentMap<Class<?>, MethodHandle> STRINGIFIERS_ANY = new ConcurrentHashMap<>();
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1737
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1738
        /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1739
         * Returns a stringifier for references and floats/doubles only.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1740
         * Always returns null for other primitives.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1741
         *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1742
         * @param t class to stringify
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1743
         * @return stringifier; null, if not available
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1744
         */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1745
        static MethodHandle forMost(Class<?> t) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1746
            return STRINGIFIERS_MOST.computeIfAbsent(t, MOST);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1747
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1748
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1749
        /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1750
         * Returns a stringifier for any type. Never returns null.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1751
         *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1752
         * @param t class to stringify
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1753
         * @return stringifier
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1754
         */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1755
        static MethodHandle forAny(Class<?> t) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1756
            return STRINGIFIERS_ANY.computeIfAbsent(t, ANY);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1757
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1758
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1759
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1760
    /* ------------------------------- Common utilities ------------------------------------ */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1761
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1762
    private static MethodHandle lookupStatic(Lookup lookup, Class<?> refc, String name, Class<?> rtype, Class<?>... ptypes) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1763
        try {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1764
            return lookup.findStatic(refc, name, MethodType.methodType(rtype, ptypes));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1765
        } catch (NoSuchMethodException | IllegalAccessException e) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1766
            throw new AssertionError(e);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1767
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1768
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1769
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1770
    private static MethodHandle lookupVirtual(Lookup lookup, Class<?> refc, String name, Class<?> rtype, Class<?>... ptypes) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1771
        try {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1772
            return lookup.findVirtual(refc, name, MethodType.methodType(rtype, ptypes));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1773
        } catch (NoSuchMethodException | IllegalAccessException e) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1774
            throw new AssertionError(e);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1775
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1776
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1777
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1778
    private static MethodHandle lookupConstructor(Lookup lookup, Class<?> refc, Class<?> ptypes) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1779
        try {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1780
            return lookup.findConstructor(refc, MethodType.methodType(void.class, ptypes));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1781
        } catch (NoSuchMethodException | IllegalAccessException e) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1782
            throw new AssertionError(e);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1783
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1784
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1785
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1786
    private static int estimateSize(Class<?> cl) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1787
        if (cl == Integer.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1788
            return 11; // "-2147483648"
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1789
        } else if (cl == Boolean.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1790
            return 5; // "false"
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1791
        } else if (cl == Byte.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1792
            return 4; // "-128"
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1793
        } else if (cl == Character.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1794
            return 1; // duh
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1795
        } else if (cl == Short.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1796
            return 6; // "-32768"
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1797
        } else if (cl == Double.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1798
            return 26; // apparently, no larger than this, see FloatingDecimal.BinaryToASCIIBuffer.buffer
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1799
        } else if (cl == Float.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1800
            return 26; // apparently, no larger than this, see FloatingDecimal.BinaryToASCIIBuffer.buffer
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1801
        } else if (cl == Long.TYPE)  {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1802
            return 20; // "-9223372036854775808"
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1803
        } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1804
            throw new IllegalArgumentException("Cannot estimate the size for " + cl);
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1805
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1806
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1807
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1808
    private static Class<?> adaptToStringBuilder(Class<?> c) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1809
        if (c.isPrimitive()) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1810
            if (c == Byte.TYPE || c == Short.TYPE) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1811
                return int.class;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1812
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1813
        } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1814
            if (c != String.class) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1815
                return Object.class;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1816
            }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1817
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1818
        return c;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1819
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1820
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1821
    private StringConcatFactory() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1822
        // no instantiation
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1823
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1824
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
  1825
}