Skip to content

Commit 7f3d652

Browse files
author
JimLaskey
committed
Requested changes #5
1 parent 2cf00ca commit 7f3d652

File tree

5 files changed

+12
-165
lines changed

5 files changed

+12
-165
lines changed

src/java.base/share/classes/java/lang/invoke/StringConcatFactory.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,30 +1113,37 @@ public static MethodHandle makeConcatWithTemplate(
11131113
}
11141114

11151115
Class<?> ttype = ttypes[pos];
1116+
// (long,byte[],ttype) -> long
11161117
MethodHandle prepender = prepender(lastFragment.isEmpty() ? null : fragment, ttype);
11171118
initialLengthCoder = JLA.stringConcatMix(initialLengthCoder, fragment);
1119+
// (byte[],long,ttypes...) -> String (unchanged)
11181120
mh = MethodHandles.filterArgumentsWithCombiner(mh, 1, prepender,1, 0, 2 + pos);
11191121

11201122
pos++;
11211123
}
11221124

11231125
MethodHandle newArrayCombinator = lastFragment.isEmpty() ? newArray() :
11241126
newArrayWithSuffix(lastFragment);
1127+
// (long,ttypes...) -> String
11251128
mh = MethodHandles.foldArgumentsWithCombiner(mh, 0, newArrayCombinator,
11261129
1 // index
11271130
);
11281131

11291132
pos = 0;
11301133
for (Class<?> ttype : ttypes) {
1134+
// (long,ttype) -> long
11311135
MethodHandle mix = mixer(ttypes[pos]);
11321136
boolean lastPType = pos == ttypes.length - 1;
11331137

11341138
if (lastPType) {
1139+
// (ttype) -> long
11351140
mix = MethodHandles.insertArguments(mix, 0, initialLengthCoder);
1141+
// (ttypes...) -> String
11361142
mh = MethodHandles.foldArgumentsWithCombiner(mh, 0, mix,
11371143
1 + pos // selected argument
11381144
);
11391145
} else {
1146+
// (long,ttypes...) -> String
11401147
mh = MethodHandles.filterArgumentsWithCombiner(mh, 0, mix,
11411148
0, // old-index
11421149
1 + pos // selected argument

src/java.base/share/classes/java/lang/template/StringTemplate.java

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
* List<Object> values = st.values();
101101
* // check or manipulate the fragments and/or values
102102
* ...
103-
* String result = StringTemplate.interpolate(fragments, values);;
103+
* String result = StringTemplate.interpolate(fragments, values);
104104
* }
105105
*
106106
* @implSpec An instance of {@link StringTemplate} is immutatble. Also, the
@@ -185,33 +185,6 @@ default String interpolate() {
185185
return processor.process(this);
186186
}
187187

188-
/**
189-
* Return the types of this {@link StringTemplate StringTemplate's} values.
190-
*
191-
* @return list of value types
192-
*
193-
* @implNote The default method determines if the {@link StringTemplate}
194-
* was synthesized by the compiler, then the types are precisely those of the
195-
* embedded expressions, otherwise this method returns the values list types.
196-
*/
197-
default List<Class<?>> valueTypes() {
198-
return TemplateRuntime.valueTypes(this);
199-
}
200-
201-
/**
202-
* Return {@link MethodHandle MethodHandles} to access this
203-
* {@link StringTemplate StringTemplate's} values.
204-
*
205-
* @return list of value types
206-
*
207-
* @implNote The default method determines if the {@link java.lang.template.StringTemplate}
208-
* was synthesized by the compiler, then the MethodHandles are precisely those of the
209-
* embedded expressions fields, otherwise this method returns getters for the values list.
210-
*/
211-
default List<MethodHandle> valueAccessors() {
212-
return TemplateRuntime.valueAccessors(this);
213-
}
214-
215188
/**
216189
* Produces a diagnostic string representing the supplied
217190
* {@link StringTemplate}.
@@ -245,8 +218,7 @@ public static String toString(StringTemplate stringTemplate) {
245218
*/
246219
public static int hashCode(StringTemplate stringTemplate) {
247220
Objects.requireNonNull(stringTemplate, "stringTemplate should not be null");
248-
return Objects.hashCode(stringTemplate.fragments()) ^
249-
Objects.hashCode(stringTemplate.values());
221+
return Objects.hash(stringTemplate.fragments(), stringTemplate.values());
250222
}
251223

252224
/**

src/java.base/share/classes/java/lang/template/TemplateRuntime.java

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,6 @@
5353
@PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES)
5454
final class TemplateRuntime {
5555

56-
/**
57-
* {@link MethodHandle} to {@link TemplateRuntime#getValue(int, StringTemplate)}.
58-
*/
59-
private static final MethodHandle GET_VALUE_MH;
60-
61-
/**
62-
* Initialize {@link MethodHandle MethodHandles}.
63-
*/
64-
static {
65-
try {
66-
MethodHandles.Lookup lookup = MethodHandles.lookup();
67-
68-
MethodType mt = MethodType.methodType(Object.class, int.class, StringTemplate.class);
69-
GET_VALUE_MH = lookup.findStatic(TemplateRuntime.class, "getValue", mt);
70-
} catch (ReflectiveOperationException | SecurityException ex) {
71-
throw new AssertionError("template runtime init fail", ex);
72-
}
73-
}
74-
7556
/**
7657
* Private constructor.
7758
*/
@@ -81,8 +62,6 @@ private TemplateRuntime() {
8162

8263
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
8364

84-
private static final JavaLangInvokeAccess JLIA = SharedSecrets.getJavaLangInvokeAccess();
85-
8665
private static final JavaUtilCollectionAccess JUCA = SharedSecrets.getJavaUtilCollectionAccess();
8766

8867
/**
@@ -100,101 +79,6 @@ static <E> List<E> toList(E... elements) {
10079
return JUCA.listFromTrustedArrayNullsAllowed(elements);
10180
}
10281

103-
/**
104-
* Return the types of a {@link StringTemplate StringTemplate's} values.
105-
*
106-
* @param st StringTemplate to examine
107-
*
108-
* @return list of value types
109-
*
110-
* @throws NullPointerException if st is null
111-
*
112-
* @implNote The default method determines if the {@link StringTemplate}
113-
* was synthesized by the compiler, then the types are precisely those of the
114-
* embedded expressions, otherwise this method returns a list of
115-
* {@code Object.class}.
116-
*/
117-
static List<Class<?>> valueTypes(StringTemplate st) {
118-
Objects.requireNonNull(st, "st must not be null");
119-
Class<?> tsClass = st.getClass();
120-
if (tsClass.isSynthetic()) {
121-
List<Class<?>> result = new ArrayList<>();
122-
try {
123-
for (int i = 0; ; i++) {
124-
Field field = tsClass.getDeclaredField("x" + i);
125-
result.add(field.getType());
126-
}
127-
} catch (NoSuchFieldException ex) {
128-
// End of fields
129-
} catch (SecurityException ex) {
130-
throw new InternalError(ex);
131-
}
132-
return result;
133-
}
134-
int size = st.values().size();
135-
Class<?>[] types = new Class<?>[size];
136-
Arrays.fill(types, Object.class);
137-
return List.of(types);
138-
}
139-
140-
/**
141-
* Return {@link MethodHandle MethodHandles} to access a
142-
* {@link StringTemplate StringTemplate's} values.
143-
*
144-
* @param st StringTemplate to examine
145-
*
146-
* @return list of {@link MethodHandle MethodHandles}
147-
*
148-
* @throws NullPointerException if st is null
149-
*
150-
* @implNote The default method determines if the {@link StringTemplate}
151-
* was synthesized by the compiler, then the MethodHandles are precisely those of the
152-
* embedded expressions fields, otherwise this method returns getters for the values list.
153-
*/
154-
static List<MethodHandle> valueAccessors(StringTemplate st) {
155-
Objects.requireNonNull(st, "st must not be null");
156-
List<MethodHandle> result = new ArrayList<>();
157-
Class<?> tsClass = st.getClass();
158-
if (tsClass.isSynthetic()) {
159-
try {
160-
for (int i = 0; ; i++) {
161-
Field field = tsClass.getDeclaredField("x" + i);
162-
MethodHandle mh = JLIA.unreflectField(field, false);
163-
mh = mh.asType(mh.type().changeParameterType(0, StringTemplate.class));
164-
result.add(mh);
165-
}
166-
} catch (NoSuchFieldException ex) {
167-
// End of fields
168-
} catch (ReflectiveOperationException | SecurityException ex) {
169-
throw new InternalError(ex);
170-
}
171-
172-
return result;
173-
}
174-
try {
175-
int size = st.values().size();
176-
for (int index = 0; index < size; index++) {
177-
result.add(MethodHandles.insertArguments(GET_VALUE_MH, 0, index));
178-
}
179-
return result;
180-
} catch (SecurityException ex) {
181-
throw new InternalError(ex);
182-
}
183-
}
184-
185-
/**
186-
* Private method used by {@link TemplateRuntime#valueAccessors(StringTemplate)}
187-
* to access values.
188-
*
189-
* @param index values index
190-
* @param st the {@link StringTemplate}
191-
*
192-
* @return value at index
193-
*/
194-
private static Object getValue(int index, StringTemplate st) {
195-
return st.values().get(index);
196-
}
197-
19882
/**
19983
* Generic StringTemplate.
20084
*

src/java.base/share/classes/jdk/internal/template/TemplateSupport.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public final class TemplateSupport {
7373
private final List<String> fragments;
7474

7575
/**
76-
* Static final processor that triggered the BSM generation.
76+
* The static final processor that triggered the BSM generation.
7777
*/
7878
private final ValidatingProcessor<?, ?> processor;
7979

@@ -138,9 +138,7 @@ public static CallSite stringTemplateBSM(
138138
Objects.requireNonNull(processorGetter, "processorGetter is null");
139139
Objects.requireNonNull(fragments, "fragments is null");
140140

141-
MethodType processorGetterType = MethodType.methodType(ValidatingProcessor.class);
142-
ValidatingProcessor<?, ?> processor =
143-
(ValidatingProcessor<?, ?>)processorGetter.asType(processorGetterType).invokeExact();
141+
ValidatingProcessor<?, ?> processor = (ValidatingProcessor<?, ?>)processorGetter.invoke();
144142
TemplateSupport support = new TemplateSupport(lookup, name, type, List.of(fragments), processor);
145143

146144
return support.processWithProcessor();
@@ -182,10 +180,7 @@ private static Object defaultProcess(List<String> fragments,
182180
*/
183181
private MethodHandle defaultProcessMethodHandle() {
184182
MethodHandle mh = MethodHandles.insertArguments(DEFAULT_PROCESS_MH, 0, fragments, processor);
185-
mh = mh.withVarargs(true);
186-
mh = mh.asType(type);
187-
188-
return mh;
183+
return mh.asCollector(Object[].class, type.parameterCount()).asType(type);
189184
}
190185

191186
/**

test/jdk/java/lang/template/Basic.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -424,17 +424,6 @@ static void stringTemplateCoverage() {
424424
ASSERT(tsValues.interpolate(), x + " + " + y + " = " + (x + y));
425425
ASSERT(StringTemplate.combine(src, src).interpolate(),
426426
RAW."\{x} + \{y} = \{x + y}\{x} + \{y} = \{x + y}".interpolate());
427-
ASSERT(src.valueTypes().get(0), int.class);
428-
ASSERT(src.valueTypes().get(1), int.class);
429-
ASSERT(src.valueTypes().get(2), int.class);
430-
431-
try {
432-
ASSERT(src.valueAccessors().get(0).invoke(src), 10);
433-
ASSERT(src.valueAccessors().get(1).invoke(src), 20);
434-
ASSERT(src.valueAccessors().get(2).invoke(src), 30);
435-
} catch (Throwable ex) {
436-
throw new RuntimeException(ex);
437-
}
438427
}
439428

440429
/*

0 commit comments

Comments
 (0)