44
44
45
45
import static org .junit .Assert .*;
46
46
47
- public class MethodHandlesPermuteArgumentsTest extends MethodHandlesTest {
47
+ public class MethodHandlesPermuteArgumentsTest extends test . java . lang . invoke . MethodHandlesTest {
48
48
49
49
@ Test // SLOW
50
50
public void testPermuteArguments () throws Throwable {
@@ -58,6 +58,11 @@ public void testPermuteArguments0() throws Throwable {
58
58
if (CAN_TEST_LIGHTLY ) return ;
59
59
testPermuteArguments (4 , Integer .class , 2 , String .class , 0 );
60
60
testPermuteArguments (6 , Integer .class , 0 , null , 30 );
61
+
62
+ testBadReorderArrayLength ();
63
+ testBadReorderIndex ();
64
+ testReturnTypeMismatch ();
65
+ testReorderTypeMismatch ();
61
66
}
62
67
63
68
public void testPermuteArguments (int max , Class <?> type1 , int t2c , Class <?> type2 , int dilution ) throws Throwable {
@@ -191,4 +196,57 @@ void testPermuteArguments(Object[] args, Class<?>[] types, int[] reorder) throws
191
196
}
192
197
assertEquals (expected , result );
193
198
}
199
+
200
+ public void testBadReorderArrayLength () throws Throwable {
201
+ MethodHandle mh = MethodHandles .empty (MethodType .methodType (void .class , int .class , int .class , String .class ));
202
+ MethodType newType = MethodType .methodType (void .class , int .class , String .class );
203
+ assertThrows (() -> MethodHandles .permuteArguments (mh , newType , 0 , 1 ),
204
+ IllegalArgumentException .class , ".*old type parameter count and reorder array length do not match.*" );
205
+ }
206
+
207
+ public void testBadReorderIndex () throws Throwable {
208
+ MethodHandle mh = MethodHandles .empty (MethodType .methodType (void .class , int .class , int .class , String .class ));
209
+ MethodType newType = MethodType .methodType (void .class , int .class , String .class );
210
+ assertThrows (() -> MethodHandles .permuteArguments (mh , newType , 0 , 0 , 2 ),
211
+ IllegalArgumentException .class , ".*index is out of bounds for new type.*" );
212
+ assertThrows (() -> MethodHandles .permuteArguments (mh , newType , 0 , 0 , -1 ),
213
+ IllegalArgumentException .class , ".*index is out of bounds for new type.*" );
214
+ }
215
+
216
+ public void testReturnTypeMismatch () throws Throwable {
217
+ MethodHandle mh = MethodHandles .empty (MethodType .methodType (void .class , int .class , int .class , String .class ));
218
+ MethodType newType = MethodType .methodType (int .class , int .class , String .class );
219
+ assertThrows (() -> MethodHandles .permuteArguments (mh , newType , 0 , 0 , 1 ),
220
+ IllegalArgumentException .class , ".*return types do not match.*" );
221
+ }
222
+
223
+ public void testReorderTypeMismatch () throws Throwable {
224
+ MethodHandle mh = MethodHandles .empty (MethodType .methodType (void .class , int .class , int .class , String .class ));
225
+ MethodType newType = MethodType .methodType (void .class , double .class , String .class );
226
+ assertThrows (() -> MethodHandles .permuteArguments (mh , newType , 0 , 0 , 1 ),
227
+ IllegalArgumentException .class , ".*parameter types do not match after reorder.*" );
228
+ }
229
+
230
+ private interface RunnableX {
231
+ void run () throws Throwable ;
232
+ }
233
+
234
+ private static void assertThrows (RunnableX r , Class <?> exceptionClass , String messagePattern ) throws Throwable {
235
+ try {
236
+ r .run ();
237
+ fail ("Exception expected" );
238
+ } catch (Throwable e ) {
239
+ if (exceptionClass .isInstance (e )) {
240
+ assertMatches (e .getMessage (), messagePattern );
241
+ } else {
242
+ throw e ;
243
+ }
244
+ }
245
+ }
246
+
247
+ private static void assertMatches (String str , String pattern ) {
248
+ if (!str .matches (pattern )) {
249
+ throw new AssertionError ("'" + str + "' did not match the pattern '" + pattern + "'." );
250
+ }
251
+ }
194
252
}
0 commit comments