2525import java .lang .constant .ClassDesc ;
2626import java .lang .constant .MethodTypeDesc ;
2727import java .util .Arrays ;
28+ import java .util .Collections ;
2829import java .util .List ;
2930import java .util .stream .IntStream ;
3031import java .util .stream .Stream ;
3637import static java .util .stream .Collectors .joining ;
3738import static java .util .stream .Collectors .toList ;
3839import static org .testng .Assert .assertEquals ;
40+ import static org .testng .Assert .assertThrows ;
3941import 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