@@ -210,7 +210,7 @@ static FieldAccessorImpl newFieldAccessor(Field field, boolean isReadOnly) {
210
210
}
211
211
212
212
private static MethodHandle getDirectMethod (Method method , boolean callerSensitive ) throws IllegalAccessException {
213
- var mtype = methodType (method .getReturnType (), method . getParameterTypes ( ));
213
+ var mtype = methodType (method .getReturnType (), reflectionFactory . getExecutableSharedParameterTypes ( method ));
214
214
var isStatic = Modifier .isStatic (method .getModifiers ());
215
215
var dmh = isStatic ? JLIA .findStatic (method .getDeclaringClass (), method .getName (), mtype )
216
216
: JLIA .findVirtual (method .getDeclaringClass (), method .getName (), mtype );
@@ -232,7 +232,7 @@ private static MethodHandle getDirectMethod(Method method, boolean callerSensiti
232
232
private static MethodHandle findCallerSensitiveAdapter (Method method ) throws IllegalAccessException {
233
233
String name = method .getName ();
234
234
// append a Class parameter
235
- MethodType mtype = methodType (method .getReturnType (), method . getParameterTypes ( ))
235
+ MethodType mtype = methodType (method .getReturnType (), reflectionFactory . getExecutableSharedParameterTypes ( method ))
236
236
.appendParameterTypes (Class .class );
237
237
boolean isStatic = Modifier .isStatic (method .getModifiers ());
238
238
@@ -370,11 +370,15 @@ private static boolean useNativeAccessor(Executable member) {
370
370
if (member instanceof Method method && isSignaturePolymorphicMethod (method ))
371
371
return true ;
372
372
373
- // java.lang.invoke fails to create MH for bad ACC_VARARGS methods with no
374
- // trailing array, but core reflection ignores ACC_VARARGS flag like the JVM does.
375
- // Fall back to use the native implementation instead.
376
- if (isInvalidVarArgs (member ))
373
+ // Lookup always calls MethodHandle::setVarargs on a member with ACC_VARARGS
374
+ // bit set, which verifies that the last parameter of the member must be
375
+ // an array type. Such restriction does not exist in core reflection
376
+ // and the JVM. Fall back to use the native implementation instead.
377
+ int paramCount = member .getParameterCount ();
378
+ if (member .isVarArgs () &&
379
+ (paramCount == 0 || !(reflectionFactory .getExecutableSharedParameterTypes (member )[paramCount -1 ].isArray ()))) {
377
380
return true ;
381
+ }
378
382
379
383
// A method handle cannot be created if its type has an arity >= 255
380
384
// as the method handle's invoke method consumes an extra argument
@@ -395,7 +399,7 @@ private static boolean useNativeAccessor(Executable member) {
395
399
*/
396
400
private static int slotCount (Executable member ) {
397
401
int slots = 0 ;
398
- Class <?>[] ptypes = member . getParameterTypes ( );
402
+ Class <?>[] ptypes = reflectionFactory . getExecutableSharedParameterTypes ( member );
399
403
for (Class <?> ptype : ptypes ) {
400
404
if (ptype == double .class || ptype == long .class ) {
401
405
slots ++;
@@ -430,24 +434,6 @@ public static boolean isSignaturePolymorphicMethod(Method method) {
430
434
return parameters .length == 1 && parameters [0 ] == Object [].class ;
431
435
}
432
436
433
- /**
434
- * Lookup always calls MethodHandle::setVarargs on a member with varargs modifier
435
- * bit set, which verifies that the last parameter of the member must be an array type.
436
- * Thus, Lookup cannot create MethodHandle for such methods or constructors.
437
- * The JVMS does not require that the last parameter descriptor of the method descriptor
438
- * is an array type if the ACC_VARARGS flag is set in the access_flags item.
439
- * Core reflection also has no variable arity support and ignores the ACC_VARARGS flag,
440
- * treating them as regular arguments.
441
- */
442
- private static boolean isInvalidVarArgs (Executable member ) {
443
- if (!member .isVarArgs ())
444
- return false ;
445
-
446
- Class <?>[] parameters = reflectionFactory .getExecutableSharedParameterTypes (member );
447
- var count = parameters .length ;
448
- return count == 0 || !parameters [count - 1 ].isArray ();
449
- }
450
-
451
437
/*
452
438
* Delay initializing these static fields until java.lang.invoke is fully initialized.
453
439
*/
0 commit comments