Skip to content

Commit 8ffa264

Browse files
liachMandy Chung
authored andcommitted
8306698: Add overloads to MethodTypeDesc::of
Reviewed-by: mchung
1 parent 6b27dad commit 8ffa264

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

src/java.base/share/classes/java/lang/constant/ConstantUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
class ConstantUtils {
3737
/** an empty constant descriptor */
3838
public static final ConstantDesc[] EMPTY_CONSTANTDESC = new ConstantDesc[0];
39+
static final ClassDesc[] EMPTY_CLASSDESC = new ClassDesc[0];
3940
static final Constable[] EMPTY_CONSTABLE = new Constable[0];
4041
static final int MAX_ARRAY_TYPE_DESC_DIMENSIONS = 255;
4142

src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,34 @@ static MethodTypeDesc ofDescriptor(String descriptor) {
5555
return MethodTypeDescImpl.ofDescriptor(descriptor);
5656
}
5757

58+
/**
59+
* {@return a {@linkplain MethodTypeDesc} with the given return type and no
60+
* parameter types}
61+
*
62+
* @param returnDesc a {@linkplain ClassDesc} describing the return type
63+
* @throws NullPointerException if {@code returnDesc} is {@code null}
64+
* @since 21
65+
*/
66+
static MethodTypeDesc of(ClassDesc returnDesc) {
67+
return new MethodTypeDescImpl(returnDesc, ConstantUtils.EMPTY_CLASSDESC);
68+
}
69+
70+
/**
71+
* {@return a {@linkplain MethodTypeDesc} given the return type and a list of
72+
* parameter types}
73+
*
74+
* @param returnDesc a {@linkplain ClassDesc} describing the return type
75+
* @param paramDescs a {@linkplain List} of {@linkplain ClassDesc}s
76+
* describing the parameter types
77+
* @throws NullPointerException if any argument or its contents are {@code null}
78+
* @throws IllegalArgumentException if any element of {@code paramDescs} is a
79+
* {@link ClassDesc} for {@code void}
80+
* @since 21
81+
*/
82+
static MethodTypeDesc of(ClassDesc returnDesc, List<ClassDesc> paramDescs) {
83+
return of(returnDesc, paramDescs.toArray(ConstantUtils.EMPTY_CLASSDESC));
84+
}
85+
5886
/**
5987
* Returns a {@linkplain MethodTypeDesc} given the return type and parameter
6088
* types.

test/jdk/java/lang/constant/MethodTypeDescTest.java

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.lang.constant.ClassDesc;
2626
import java.lang.constant.MethodTypeDesc;
2727
import java.util.Arrays;
28+
import java.util.Collections;
2829
import java.util.List;
2930
import java.util.stream.IntStream;
3031
import java.util.stream.Stream;
@@ -36,6 +37,7 @@
3637
import static java.util.stream.Collectors.joining;
3738
import static java.util.stream.Collectors.toList;
3839
import static org.testng.Assert.assertEquals;
40+
import static org.testng.Assert.assertThrows;
3941
import static org.testng.Assert.fail;
4042

4143
/**
@@ -52,13 +54,22 @@ private void testMethodTypeDesc(MethodTypeDesc r) throws ReflectiveOperationExce
5254

5355
// Tests accessors (rType, pType, pCount, pList, pArray, descriptorString),
5456
// factories (ofDescriptor, of), equals
57+
if (r.parameterCount() == 0) {
58+
assertEquals(r, MethodTypeDesc.of(r.returnType()));
59+
}
5560
assertEquals(r, MethodTypeDesc.ofDescriptor(r.descriptorString()));
5661
assertEquals(r, MethodTypeDesc.of(r.returnType(), r.parameterArray()));
5762
assertEquals(r, MethodTypeDesc.of(r.returnType(), r.parameterList().toArray(new ClassDesc[0])));
5863
assertEquals(r, MethodTypeDesc.of(r.returnType(), r.parameterList().stream().toArray(ClassDesc[]::new)));
5964
assertEquals(r, MethodTypeDesc.of(r.returnType(), IntStream.range(0, r.parameterCount())
6065
.mapToObj(r::parameterType)
6166
.toArray(ClassDesc[]::new)));
67+
assertEquals(r, MethodTypeDesc.of(r.returnType(), r.parameterList()));
68+
assertEquals(r, MethodTypeDesc.of(r.returnType(), List.copyOf(r.parameterList())));
69+
assertEquals(r, MethodTypeDesc.of(r.returnType(), r.parameterList().stream().toList()));
70+
assertEquals(r, MethodTypeDesc.of(r.returnType(), IntStream.range(0, r.parameterCount())
71+
.mapToObj(r::parameterType)
72+
.toList()));
6273
}
6374

6475
private void testMethodTypeDesc(MethodTypeDesc r, MethodType mt) throws ReflectiveOperationException {
@@ -255,54 +266,29 @@ public void testMethodTypeDesc() throws ReflectiveOperationException {
255266
}
256267

257268
public void testBadMethodTypeRefs() {
269+
// ofDescriptor
258270
List<String> badDescriptors = List.of("()II", "()I;", "(I;)", "(I)", "()L", "(V)V",
259271
"(java.lang.String)V", "()[]", "(Ljava/lang/String)V",
260272
"(Ljava.lang.String;)V", "(java/lang/String)V");
261273

262274
for (String d : badDescriptors) {
263-
try {
264-
MethodTypeDesc r = MethodTypeDesc.ofDescriptor(d);
265-
fail(d);
266-
}
267-
catch (IllegalArgumentException e) {
268-
// good
269-
}
275+
assertThrows(IllegalArgumentException.class, () -> MethodTypeDesc.ofDescriptor(d));
270276
}
277+
assertThrows(NullPointerException.class, () -> MethodTypeDesc.ofDescriptor(null));
271278

272-
// try with null argument
273-
try {
274-
MethodTypeDesc r = MethodTypeDesc.ofDescriptor(null);
275-
fail("should fail with NPE");
276-
} catch (NullPointerException ex) {
277-
// good
278-
}
279+
// of(ClassDesc)
280+
assertThrows(NullPointerException.class, () -> MethodTypeDesc.of(null));
279281

282+
// of(ClassDesc, ClassDesc...)
283+
assertThrows(NullPointerException.class, () -> MethodTypeDesc.of(CD_int, (ClassDesc[]) null));
284+
assertThrows(NullPointerException.class, () -> MethodTypeDesc.of(CD_int, new ClassDesc[] {null}));
280285
// try with void arguments, this will stress another code path in particular
281286
// ConstantMethodTypeDesc::init
282-
try {
283-
MethodTypeDesc r = MethodTypeDesc.of(CD_int, CD_void);
284-
fail("can't reach here");
285-
}
286-
catch (IllegalArgumentException e) {
287-
// good
288-
}
287+
assertThrows(IllegalArgumentException.class, () -> MethodTypeDesc.of(CD_int, CD_void));
289288

290-
try {
291-
MethodTypeDesc r = MethodTypeDesc.of(CD_int, null);
292-
fail("ClassDesc array should not be null");
293-
}
294-
catch (NullPointerException e) {
295-
// good
296-
}
297-
298-
try {
299-
ClassDesc[] paramDescs = new ClassDesc[1];
300-
paramDescs[0] = null;
301-
MethodTypeDesc r = MethodTypeDesc.of(CD_int, paramDescs);
302-
fail("ClassDesc should not be null");
303-
}
304-
catch (NullPointerException e) {
305-
// good
306-
}
289+
// of(ClassDesc, List<ClassDesc>)
290+
assertThrows(NullPointerException.class, () -> MethodTypeDesc.of(CD_int, (List<ClassDesc>) null));
291+
assertThrows(NullPointerException.class, () -> MethodTypeDesc.of(CD_int, Collections.singletonList(null)));
292+
assertThrows(IllegalArgumentException.class, () -> MethodTypeDesc.of(CD_int, List.of(CD_void)));
307293
}
308294
}

0 commit comments

Comments
 (0)