Skip to content

Commit 2358d40

Browse files
committed
8371953: Document null handling in core reflection APIs
Reviewed-by: alanb
1 parent d887359 commit 2358d40

16 files changed

+440
-51
lines changed

src/java.base/share/classes/java/lang/Class.java

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@
160160
* other members are the classes and interfaces whose declarations are
161161
* enclosed within the top-level class declaration.
162162
*
163+
* <p> Unless otherwise specified, methods in this class throw a
164+
* {@link NullPointerException} when they are called with {@code null}
165+
* or an array that contains {@code null} as an argument.
166+
*
163167
* <h2><a id=hiddenClasses>Hidden Classes</a></h2>
164168
* A class or interface created by the invocation of
165169
* {@link java.lang.invoke.MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)
@@ -530,7 +534,8 @@ private static Class<?> forName(String className, Class<?> caller)
530534
* (which implies linking). See Section {@jls
531535
* 12.4} of <cite>The Java Language
532536
* Specification</cite>.
533-
* @param loader class loader from which the class must be loaded
537+
* @param loader class loader from which the class must be loaded,
538+
* may be {@code null}
534539
* @return class object representing the desired class
535540
*
536541
* @throws LinkageError if the linkage fails
@@ -589,8 +594,6 @@ private static native Class<?> forName0(String name, boolean initialize,
589594
* @return {@code Class} object of the given name defined in the given module;
590595
* {@code null} if not found.
591596
*
592-
* @throws NullPointerException if the given module or name is {@code null}
593-
*
594597
* @throws LinkageError if the linkage fails
595598
*
596599
* @jls 12.2 Loading of Classes and Interfaces
@@ -620,8 +623,6 @@ public static Class<?> forName(Module module, String name) {
620623
*
621624
* @param primitiveName the name of the primitive type to find
622625
*
623-
* @throws NullPointerException if the argument is {@code null}
624-
*
625626
* @jls 4.2 Primitive Types and Values
626627
* @jls 15.8.2 Class Literals
627628
* @since 22
@@ -757,7 +758,7 @@ public T newInstance()
757758
* this {@code Class} object represents a primitive type, this method
758759
* returns {@code false}.
759760
*
760-
* @param obj the object to check
761+
* @param obj the object to check, may be {@code null}
761762
* @return true if {@code obj} is an instance of this class
762763
*
763764
* @since 1.1
@@ -787,8 +788,6 @@ public T newInstance()
787788
* @param cls the {@code Class} object to be checked
788789
* @return the {@code boolean} value indicating whether objects of the
789790
* type {@code cls} can be assigned to objects of this class
790-
* @throws NullPointerException if the specified Class parameter is
791-
* null.
792791
* @since 1.1
793792
*/
794793
@IntrinsicCandidate
@@ -2057,7 +2056,6 @@ public Constructor<?>[] getConstructors() {
20572056
* {@code name}
20582057
* @throws NoSuchFieldException if a field with the specified name is
20592058
* not found.
2060-
* @throws NullPointerException if {@code name} is {@code null}
20612059
*
20622060
* @since 1.1
20632061
* @jls 8.2 Class Members
@@ -2148,13 +2146,13 @@ public Field getField(String name) throws NoSuchFieldException {
21482146
* overriding method as it would have a more specific return type.
21492147
*
21502148
* @param name the name of the method
2151-
* @param parameterTypes the list of parameters
2149+
* @param parameterTypes the list of parameters, may be {@code null}
21522150
* @return the {@code Method} object that matches the specified
21532151
* {@code name} and {@code parameterTypes}
2154-
* @throws NoSuchMethodException if a matching method is not found
2152+
* @throws NoSuchMethodException if a matching method is not found,
2153+
* if {@code parameterTypes} contains {@code null},
21552154
* or if the name is {@value ConstantDescs#INIT_NAME} or
2156-
* {@value ConstantDescs#CLASS_INIT_NAME}.
2157-
* @throws NullPointerException if {@code name} is {@code null}
2155+
* {@value ConstantDescs#CLASS_INIT_NAME}
21582156
*
21592157
* @jls 8.2 Class Members
21602158
* @jls 8.4 Method Declarations
@@ -2185,12 +2183,13 @@ public Method getMethod(String name, Class<?>... parameterTypes)
21852183
* represented by this {@code Class} object whose formal parameter
21862184
* types match those specified by {@code parameterTypes}.
21872185
*
2188-
* @param parameterTypes the parameter array
2186+
* @param parameterTypes the parameter array, may be {@code null}
21892187
* @return the {@code Constructor} object of the public constructor that
21902188
* matches the specified {@code parameterTypes}
21912189
* @throws NoSuchMethodException if a matching constructor is not found,
2192-
* including when this {@code Class} object represents
2193-
* an interface, a primitive type, an array class, or void.
2190+
* if this {@code Class} object represents an interface, a primitive
2191+
* type, an array class, or void, or if {@code parameterTypes}
2192+
* contains {@code null}
21942193
*
21952194
* @see #getDeclaredConstructor(Class[])
21962195
* @since 1.1
@@ -2371,7 +2370,6 @@ public Constructor<?>[] getDeclaredConstructors() {
23712370
* class
23722371
* @throws NoSuchFieldException if a field with the specified name is
23732372
* not found.
2374-
* @throws NullPointerException if {@code name} is {@code null}
23752373
*
23762374
* @since 1.1
23772375
* @jls 8.2 Class Members
@@ -2406,11 +2404,13 @@ public Field getDeclaredField(String name) throws NoSuchFieldException {
24062404
* method does not find the {@code clone()} method.
24072405
*
24082406
* @param name the name of the method
2409-
* @param parameterTypes the parameter array
2410-
* @return the {@code Method} object for the method of this class
2411-
* matching the specified name and parameters
2412-
* @throws NoSuchMethodException if a matching method is not found.
2413-
* @throws NullPointerException if {@code name} is {@code null}
2407+
* @param parameterTypes the parameter array, may be {@code null}
2408+
* @return the {@code Method} object for the method of this class
2409+
* matching the specified name and parameters
2410+
* @throws NoSuchMethodException if a matching method is not found,
2411+
* if {@code parameterTypes} contains {@code null},
2412+
* or if the name is {@value ConstantDescs#INIT_NAME} or
2413+
* {@value ConstantDescs#CLASS_INIT_NAME}
24142414
*
24152415
* @jls 8.2 Class Members
24162416
* @jls 8.4 Method Declarations
@@ -2477,12 +2477,13 @@ Method findMethod(boolean publicOnly, String name, Class<?>... parameterTypes) {
24772477
* declared in a non-static context, the formal parameter types
24782478
* include the explicit enclosing instance as the first parameter.
24792479
*
2480-
* @param parameterTypes the parameter array
2480+
* @param parameterTypes the parameter array, may be {@code null}
24812481
* @return The {@code Constructor} object for the constructor with the
24822482
* specified parameter list
24832483
* @throws NoSuchMethodException if a matching constructor is not found,
2484-
* including when this {@code Class} object represents
2485-
* an interface, a primitive type, an array class, or void.
2484+
* if this {@code Class} object represents an interface, a
2485+
* primitive type, an array class, or void, or if
2486+
* {@code parameterTypes} contains {@code null}
24862487
*
24872488
* @see #getConstructor(Class[])
24882489
* @since 1.1
@@ -2541,7 +2542,6 @@ public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
25412542
* resource with this name is found, or the resource is in a package
25422543
* that is not {@linkplain Module#isOpen(String, Module) open} to at
25432544
* least the caller module.
2544-
* @throws NullPointerException If {@code name} is {@code null}
25452545
*
25462546
* @see Module#getResourceAsStream(String)
25472547
* @since 1.1
@@ -2637,7 +2637,6 @@ public InputStream getResourceAsStream(String name) {
26372637
* resource is in a package that is not
26382638
* {@linkplain Module#isOpen(String, Module) open} to at least the caller
26392639
* module.
2640-
* @throws NullPointerException If {@code name} is {@code null}
26412640
* @since 1.1
26422641
*/
26432642
@CallerSensitive
@@ -3479,7 +3478,7 @@ Map<String, T> enumConstantDirectory() {
34793478
* Casts an object to the class or interface represented
34803479
* by this {@code Class} object.
34813480
*
3482-
* @param obj the object to be cast
3481+
* @param obj the object to be cast, may be {@code null}
34833482
* @return the object after casting, or null if obj is null
34843483
*
34853484
* @throws ClassCastException if the object is not
@@ -3534,7 +3533,6 @@ public <U> Class<? extends U> asSubclass(Class<U> clazz) {
35343533
* <p>Note that any annotation returned by this method is a
35353534
* declaration annotation.
35363535
*
3537-
* @throws NullPointerException {@inheritDoc}
35383536
* @since 1.5
35393537
*/
35403538
@Override
@@ -3547,7 +3545,6 @@ public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
35473545

35483546
/**
35493547
* {@inheritDoc}
3550-
* @throws NullPointerException {@inheritDoc}
35513548
* @since 1.5
35523549
*/
35533550
@Override
@@ -3560,7 +3557,6 @@ public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
35603557
* <p>Note that any annotations returned by this method are
35613558
* declaration annotations.
35623559
*
3563-
* @throws NullPointerException {@inheritDoc}
35643560
* @since 1.8
35653561
*/
35663562
@Override
@@ -3590,7 +3586,6 @@ public Annotation[] getAnnotations() {
35903586
* <p>Note that any annotation returned by this method is a
35913587
* declaration annotation.
35923588
*
3593-
* @throws NullPointerException {@inheritDoc}
35943589
* @since 1.8
35953590
*/
35963591
@Override
@@ -3606,7 +3601,6 @@ public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass)
36063601
* <p>Note that any annotations returned by this method are
36073602
* declaration annotations.
36083603
*
3609-
* @throws NullPointerException {@inheritDoc}
36103604
* @since 1.8
36113605
*/
36123606
@Override
@@ -3837,6 +3831,7 @@ public Class<?> getNestHost() {
38373831
* @since 11
38383832
*/
38393833
public boolean isNestmateOf(Class<?> c) {
3834+
Objects.requireNonNull(c);
38403835
if (this == c) {
38413836
return true;
38423837
}

src/java.base/share/classes/java/lang/reflect/AccessibleObject.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public class AccessibleObject implements AnnotatedElement {
9797
* objects in the array
9898
* @throws SecurityException if an element in the array is a constructor for {@code
9999
* java.lang.Class}
100+
* @throws NullPointerException if {@code array} or any of its elements is
101+
* {@code null}
100102
*/
101103
@CallerSensitive
102104
public static void setAccessible(AccessibleObject[] array, boolean flag) {

src/java.base/share/classes/java/lang/reflect/Array.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -98,8 +98,8 @@ public static Object newInstance(Class<?> componentType, int length)
9898
* @param dimensions an array of {@code int} representing the dimensions of
9999
* the new array
100100
* @return the new array
101-
* @throws NullPointerException if the specified
102-
* {@code componentType} argument is null
101+
* @throws NullPointerException if any of the specified
102+
* {@code componentType} or {@code dimensions} arguments is null
103103
* @throws IllegalArgumentException if the specified {@code dimensions}
104104
* argument is a zero-dimensional array, if componentType is {@link
105105
* Void#TYPE}, or if the number of dimensions of the requested array
@@ -117,8 +117,8 @@ public static Object newInstance(Class<?> componentType, int... dimensions)
117117
*
118118
* @param array the array
119119
* @return the length of the array
120-
* @throws IllegalArgumentException if the object argument is not
121-
* an array
120+
* @throws NullPointerException if {@code array} is {@code null}
121+
* @throws IllegalArgumentException if {@code array} is not an array
122122
*/
123123
@IntrinsicCandidate
124124
public static native int getLength(Object array)

src/java.base/share/classes/java/lang/reflect/ClassFileFormatVersion.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ public int major() {
433433
* ClassFileFormatVersion.valueOf(Runtime.Version.parse("17"))}
434434
*
435435
* @param rv runtime version to map to a class file format version
436+
* @throws NullPointerException if {@code rv} is {@code null}
436437
* @throws IllegalArgumentException if the feature of version
437438
* argument is greater than the feature of the platform version.
438439
*/

src/java.base/share/classes/java/lang/reflect/InaccessibleObjectException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -47,7 +47,7 @@ public InaccessibleObjectException() {
4747
* message.
4848
*
4949
* @param msg
50-
* The detail message
50+
* The detail message, may be {@code null}
5151
*/
5252
public InaccessibleObjectException(String msg) {
5353
super(msg);

src/java.base/share/classes/java/lang/reflect/InvocationTargetException.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -62,7 +62,7 @@ protected InvocationTargetException() {
6262
/**
6363
* Constructs a InvocationTargetException with a target exception.
6464
*
65-
* @param target the target exception
65+
* @param target the target exception, may be {@code null}
6666
*/
6767
public InvocationTargetException(Throwable target) {
6868
super((Throwable)null); // Disallow initCause
@@ -73,8 +73,8 @@ public InvocationTargetException(Throwable target) {
7373
* Constructs a InvocationTargetException with a target exception
7474
* and a detail message.
7575
*
76-
* @param target the target exception
77-
* @param s the detail message
76+
* @param target the target exception, may be {@code null}
77+
* @param s the detail message, may be {@code null}
7878
*/
7979
public InvocationTargetException(Throwable target, String s) {
8080
super(s, null); // Disallow initCause

src/java.base/share/classes/java/lang/reflect/MalformedParametersException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -65,7 +65,7 @@ public MalformedParametersException() {}
6565
/**
6666
* Create a {@code MalformedParametersException}.
6767
*
68-
* @param reason The reason for the exception.
68+
* @param reason The reason for the exception, may be {@code null}
6969
*/
7070
public MalformedParametersException(String reason) {
7171
super(reason);

src/java.base/share/classes/java/lang/reflect/Proxy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,8 @@ private static Module getDynamicModule(ClassLoader loader) {
897897
* of interfaces but in a different order will result in two distinct
898898
* proxy classes.
899899
*
900-
* @param loader the class loader to define the proxy class
900+
* @param loader the class loader to define the proxy class, may be
901+
* {@code null} to represent the bootstrap class loader
901902
* @param interfaces the list of interfaces for the proxy class
902903
* to implement
903904
* @param h the invocation handler to dispatch method invocations to

src/java.base/share/classes/java/lang/reflect/UndeclaredThrowableException.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -61,7 +61,7 @@ public class UndeclaredThrowableException extends RuntimeException {
6161
* specified {@code Throwable}.
6262
*
6363
* @param undeclaredThrowable the undeclared checked exception
64-
* that was thrown
64+
* that was thrown, may be {@code null}
6565
*/
6666
public UndeclaredThrowableException(Throwable undeclaredThrowable) {
6767
super(null, undeclaredThrowable); // Disallow initCause
@@ -72,8 +72,8 @@ public UndeclaredThrowableException(Throwable undeclaredThrowable) {
7272
* specified {@code Throwable} and a detail message.
7373
*
7474
* @param undeclaredThrowable the undeclared checked exception
75-
* that was thrown
76-
* @param s the detail message
75+
* that was thrown, may be {@code null}
76+
* @param s the detail message, may be {@code null}
7777
*/
7878
public UndeclaredThrowableException(Throwable undeclaredThrowable,
7979
String s)

0 commit comments

Comments
 (0)