Skip to content
Closed
29 changes: 14 additions & 15 deletions src/java.base/share/classes/java/lang/StringConcatHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ static long mix(long lengthCoder, String value) {
@PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES)
static long mix(long lengthCoder, FormatConcatItem value) {
lengthCoder = value.mix(lengthCoder);

return checkOverflow(lengthCoder);
}

Expand All @@ -152,7 +151,7 @@ static long mix(long lengthCoder, FormatConcatItem value) {
* @param value boolean value to encode
* @return updated index (coder value retained)
*/
private static long prepend(long indexCoder, byte[] buf, boolean value) {
static long prepend(long indexCoder, byte[] buf, boolean value) {
int index = (int)indexCoder;
if (indexCoder < UTF16) {
if (value) {
Expand Down Expand Up @@ -198,7 +197,7 @@ private static long prepend(long indexCoder, byte[] buf, boolean value) {
*/
static long prepend(long indexCoder, byte[] buf, boolean value, String prefix) {
indexCoder = prepend(indexCoder, buf, value);
if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
indexCoder = prepend(indexCoder, buf, prefix);
return indexCoder;
}

Expand All @@ -212,7 +211,7 @@ static long prepend(long indexCoder, byte[] buf, boolean value, String prefix) {
* @param value char value to encode
* @return updated index (coder value retained)
*/
private static long prepend(long indexCoder, byte[] buf, char value) {
static long prepend(long indexCoder, byte[] buf, char value) {
if (indexCoder < UTF16) {
buf[(int)(--indexCoder)] = (byte) (value & 0xFF);
} else {
Expand All @@ -234,7 +233,7 @@ private static long prepend(long indexCoder, byte[] buf, char value) {
*/
static long prepend(long indexCoder, byte[] buf, char value, String prefix) {
indexCoder = prepend(indexCoder, buf, value);
if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
indexCoder = prepend(indexCoder, buf, prefix);
return indexCoder;
}

Expand All @@ -248,7 +247,7 @@ static long prepend(long indexCoder, byte[] buf, char value, String prefix) {
* @param value integer value to encode
* @return updated index (coder value retained)
*/
private static long prepend(long indexCoder, byte[] buf, int value) {
static long prepend(long indexCoder, byte[] buf, int value) {
if (indexCoder < UTF16) {
return StringLatin1.getChars(value, (int)indexCoder, buf);
} else {
Expand All @@ -269,7 +268,7 @@ private static long prepend(long indexCoder, byte[] buf, int value) {
*/
static long prepend(long indexCoder, byte[] buf, int value, String prefix) {
indexCoder = prepend(indexCoder, buf, value);
if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
indexCoder = prepend(indexCoder, buf, prefix);
return indexCoder;
}

Expand All @@ -283,7 +282,7 @@ static long prepend(long indexCoder, byte[] buf, int value, String prefix) {
* @param value long value to encode
* @return updated index (coder value retained)
*/
private static long prepend(long indexCoder, byte[] buf, long value) {
static long prepend(long indexCoder, byte[] buf, long value) {
if (indexCoder < UTF16) {
return StringLatin1.getChars(value, (int)indexCoder, buf);
} else {
Expand All @@ -304,7 +303,7 @@ private static long prepend(long indexCoder, byte[] buf, long value) {
*/
static long prepend(long indexCoder, byte[] buf, long value, String prefix) {
indexCoder = prepend(indexCoder, buf, value);
if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
indexCoder = prepend(indexCoder, buf, prefix);
return indexCoder;
}

Expand All @@ -318,7 +317,7 @@ static long prepend(long indexCoder, byte[] buf, long value, String prefix) {
* @param value String value to encode
* @return updated index (coder value retained)
*/
private static long prepend(long indexCoder, byte[] buf, String value) {
static long prepend(long indexCoder, byte[] buf, String value) {
indexCoder -= value.length();
if (indexCoder < UTF16) {
value.getBytes(buf, (int)indexCoder, String.LATIN1);
Expand All @@ -341,7 +340,7 @@ private static long prepend(long indexCoder, byte[] buf, String value) {
*/
static long prepend(long indexCoder, byte[] buf, String value, String prefix) {
indexCoder = prepend(indexCoder, buf, value);
if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
indexCoder = prepend(indexCoder, buf, prefix);
return indexCoder;
}

Expand All @@ -357,8 +356,7 @@ static long prepend(long indexCoder, byte[] buf, String value, String prefix) {
* @since 21
*/
@PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES)
private static long prepend(long indexCoder, byte[] buf,
FormatConcatItem value) {
static long prepend(long indexCoder, byte[] buf, FormatConcatItem value) {
try {
return value.prepend(indexCoder, buf);
} catch (Error ex) {
Expand All @@ -384,7 +382,7 @@ private static long prepend(long indexCoder, byte[] buf,
static long prepend(long indexCoder, byte[] buf,
FormatConcatItem value, String prefix) {
indexCoder = prepend(indexCoder, buf, value);
if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
indexCoder = prepend(indexCoder, buf, prefix);
return indexCoder;
}

Expand Down Expand Up @@ -586,7 +584,8 @@ static MethodHandle selectPutChar(long indexCoder) {

static MethodHandle lookupStatic(String name, MethodType methodType) {
try {
return MethodHandles.lookup().findStatic(StringConcatHelper.class, name, methodType);
return MethodHandles.lookup()
.findStatic(StringConcatHelper.class, name, methodType);
} catch (NoSuchMethodException|IllegalAccessException e) {
throw new AssertionError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,19 +700,12 @@ private static MethodHandle foldInLastMixers(MethodHandle mh, long initialLength
// Simple prependers, single argument. May be used directly or as a
// building block for complex prepender combinators.
private static MethodHandle prepender(String prefix, Class<?> cl) {
MethodHandle prepend;
int idx = classIndex(cl);
if (prefix == null) {
prepend = NULL_PREPENDERS[idx];
if (prepend == null) {
NULL_PREPENDERS[idx] = prepend = MethodHandles.insertArguments(
prepender(cl), 3, (String)null);
}
if (prefix == null || prefix.isEmpty()) {
return noPrefixPrepender(cl);
} else {
prepend = MethodHandles.insertArguments(
return MethodHandles.insertArguments(
prepender(cl), 3, prefix);
}
return prepend;
}

private static MethodHandle prepender(Class<?> cl) {
Expand All @@ -729,6 +722,20 @@ private static MethodHandle prepender(Class<?> cl) {
return prepend;
}

private static MethodHandle noPrefixPrepender(Class<?> cl) {
int idx = classIndex(cl);
MethodHandle prepend = NO_PREFIX_PREPENDERS[idx];
if (prepend == null) {
if (idx == STRING_CONCAT_ITEM) {
cl = FormatConcatItem.class;
}
NO_PREFIX_PREPENDERS[idx] = prepend = JLA.stringConcatHelper("prepend",
methodType(long.class, long.class, byte[].class,
Wrapper.asPrimitiveType(cl))).rebind();
}
return prepend;
}

private static final int INT_IDX = 0,
CHAR_IDX = 1,
LONG_IDX = 2,
Expand Down Expand Up @@ -995,7 +1002,7 @@ private static MethodHandle unaryConcat(Class<?> cl) {
}
}

private static final @Stable MethodHandle[] NULL_PREPENDERS = new MethodHandle[TYPE_COUNT];
private static final @Stable MethodHandle[] NO_PREFIX_PREPENDERS = new MethodHandle[TYPE_COUNT];
private static final @Stable MethodHandle[] PREPENDERS = new MethodHandle[TYPE_COUNT];
private static final @Stable MethodHandle[] MIXERS = new MethodHandle[TYPE_COUNT];
private static final long INITIAL_CODER = JLA.stringConcatInitialCoder();
Expand Down Expand Up @@ -1117,7 +1124,7 @@ public static MethodHandle makeConcatWithTemplate(

Class<?> ttype = ttypes[pos];
// (long,byte[],ttype) -> long
MethodHandle prepender = prepender(fragment.isEmpty() ? null : fragment, ttype);
MethodHandle prepender = prepender(fragment, ttype);
// (byte[],long,ttypes...) -> String (unchanged)
mh = MethodHandles.filterArgumentsWithCombiner(mh, 1, prepender,1, 0, 2 + pos);

Expand Down
5 changes: 2 additions & 3 deletions src/java.base/share/classes/java/util/FormatItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class FormatItem {
private static final MethodHandle STRING_PREPEND =
JLA.stringConcatHelper("prepend",
MethodType.methodType(long.class, long.class, byte[].class,
String.class, String.class));
String.class));

private static final MethodHandle SELECT_GETCHAR_MH =
JLA.stringConcatHelper("selectGetChar",
Expand All @@ -87,8 +87,7 @@ private static long stringMix(long lengthCoder, String value) {

private static long stringPrepend(long lengthCoder, byte[] buffer,
String value) throws Throwable {
return (long)STRING_PREPEND.invokeExact(lengthCoder, buffer, value,
(String)null);
return (long)STRING_PREPEND.invokeExact(lengthCoder, buffer, value);
}

private static MethodHandle selectGetChar(long indexCoder) throws Throwable {
Expand Down