-
Type:
CSR
-
Resolution: Unresolved
-
Priority:
P4
-
Component/s: core-libs
-
None
-
behavioral
-
minimal
-
-
Java API
-
SE
Summary
Document the null argument (including null array component) handling of all core reflection APIs, which may accept nulls or reject nulls with different exceptions.
Problem
Core reflection APIs often have unclear null argument handling due to less professional standards back in older times. Issues about specifying null handling arises from time to time, such as JDK-8357658.
Luckily, core reflection API surface is limited to java.lang.reflect and java.lang.Class. We can comprehensively check all public arguments in core reflection to ensure they have null handling explicitly specified.
In the examination, I found that all of them without clear null argument handling specified have established consistent null behaviors, except for Class.isNestmateOf(Class), which returns false for int.class.isNestmateOf(null) but throws NullPointerException for Object.class.isNestmateOf(null).
Solution
We specify the null behavior for all APIs in core reflection, so that:
- All arguments that may be or contain null (as an array) are explicitly marked.
- All null rejection are now in the respective exception clauses, which is usually
NullPointerException, and sometimes,IllegalArgumentExceptionorNoSuchMethodException.
For the only unclear null handling in Class::isNestmateOf, I propose to reject null consistently and eagerly to prevent potential errors.
Specification
In java.lang.Class:
forName(String): NPE fornameforName(String, boolean, ClassLoader): NPE forname, null ok forloaderisInstance(Object): null ok for argumentget[|Declared][Method|Constructor]: null ok for the parameter array; NoSuchMethodException for null as an array componentcast(Object): null ok forobjasSubClass(Class): NPE forclazzisNestmateOf(Class): NPE forc(only sometimes previously)
In java.lang.reflect:
AccessibleObject.setAccessible: NPE for the array or its elements- Note: Behaviorally, this method may throw NPE after performing some side effect on some array elements. This is the longstanding behavior; its other exceptions thrown may also happen after some side effect has happened, and this details is not explicitly specified or noted.
Array.newInstance(Class, int[]): NPE for dimensions in addition to existing one for component class argumentArray.getLength(Object): NPE for argumentClassFileFormatVersion.parse(Runtime.Version): NPE for argumentProxy.newProxyInstance: null ok for ClassLoaderInaccessibleObjectException,InvocationTargetException,MalformedParametersException,UndeclaredThrowableException: null ok for all constructor args
See the attached diff for the exact details.
- csr of
-
JDK-8371953 Document null handling in core reflection APIs
-
- In Progress
-
- relates to
-
JDK-8357658 Proxy.getInvocationHandler throws NullPointerException instead of IllegalArgumentException for null
-
- Closed
-
-
JDK-8371961 Missing null check in AnnotatedType annotation accessor methods
-
- Closed
-