8304929: MethodTypeDesc throws an unchecked exception than ReflectiveOperationException when a component class cannot be resolved

Reviewed-by: jvernee
This commit is contained in:
Chen Liang 2024-07-22 22:53:52 +00:00
parent d63d6e23d1
commit 96e4a1876a
2 changed files with 25 additions and 10 deletions
src/java.base/share/classes/jdk/internal/constant
test/jdk/java/lang/constant

@ -213,16 +213,25 @@ public final class MethodTypeDescImpl implements MethodTypeDesc {
@Override
public MethodType resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException {
@SuppressWarnings("removal")
MethodType mtype = AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public MethodType run() {
return MethodType.fromMethodDescriptorString(descriptorString(),
lookup.lookupClass().getClassLoader());
}
});
MethodType mtype;
try {
@SuppressWarnings("removal")
MethodType mt = AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public MethodType run() {
return MethodType.fromMethodDescriptorString(descriptorString(),
lookup.lookupClass().getClassLoader());
}
});
mtype = mt;
} catch (TypeNotPresentException ex) {
throw (ClassNotFoundException) ex.getCause();
}
// let's check that the lookup has access to all the types in the method type
// Some method types, like ones containing a package private class not accessible
// to the overriding method, can be valid method descriptors and obtained from
// MethodType.fromMethodDescriptor, but ldc instruction will fail to resolve such
// MethodType constants due to access control (JVMS 5.4.3.1 and 5.4.3.5)
lookup.accessClass(mtype.returnType());
for (Class<?> paramType: mtype.parameterArray()) {
lookup.accessClass(paramType);

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -21,6 +21,7 @@
* questions.
*/
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;
@ -304,4 +305,9 @@ public class MethodTypeDescTest extends SymbolicDescTest {
mtd.parameterList().set(1, CD_void));
assertEquals(mtd, MethodTypeDesc.of(CD_void, CD_Object, CD_int));
}
public void testMissingClass() {
var mtd = MTD_void.insertParameterTypes(0, ClassDesc.of("does.not.exist.DoesNotExist"));
assertThrows(ReflectiveOperationException.class, () -> mtd.resolveConstantDesc(MethodHandles.publicLookup()));
}
}