@@ -78,14 +78,24 @@ public interface Nullability {
7878 */
7979 boolean isNonNull ();
8080
81+ /**
82+ * Creates a new {@link MethodNullability} instance by introspecting the {@link Method} return type.
83+ *
84+ * @param method the source method.
85+ * @return a {@code Nullability} instance containing the element's nullability declaration.
86+ */
87+ static MethodNullability forMethod (Method method ) {
88+ return new NullabilityIntrospector (method .getDeclaringClass (), true ).forMethod (method );
89+ }
90+
8191 /**
8292 * Creates a new {@link Nullability} instance by introspecting the {@link MethodParameter}.
8393 *
8494 * @param parameter the source method parameter.
8595 * @return a {@code Nullability} instance containing the element's nullability declaration.
8696 */
87- static Nullability from (MethodParameter parameter ) {
88- return introspect (parameter .getContainingClass ()).forParameter (parameter );
97+ static Nullability forParameter (MethodParameter parameter ) {
98+ return new NullabilityIntrospector (parameter .getContainingClass (), false ).forParameter (parameter );
8999 }
90100
91101 /**
@@ -95,7 +105,7 @@ static Nullability from(MethodParameter parameter) {
95105 * @return a {@code Nullability} instance containing the element's nullability declaration.
96106 */
97107 static Nullability forMethodReturnType (Method method ) {
98- return introspect (method .getDeclaringClass ()).forReturnType (method );
108+ return new NullabilityIntrospector (method .getDeclaringClass (), false ).forReturnType (method );
99109 }
100110
101111 /**
@@ -104,8 +114,9 @@ static Nullability forMethodReturnType(Method method) {
104114 * @param parameter the source method parameter.
105115 * @return a {@code Nullability} instance containing the element's nullability declaration.
106116 */
107- static Nullability forMethodParameter (Parameter parameter ) {
108- return introspect (parameter .getDeclaringExecutable ().getDeclaringClass ()).forParameter (parameter );
117+ static Nullability forParameter (Parameter parameter ) {
118+ return new NullabilityIntrospector (parameter .getDeclaringExecutable ().getDeclaringClass (), false )
119+ .forParameter (parameter );
109120 }
110121
111122 /**
@@ -115,7 +126,7 @@ static Nullability forMethodParameter(Parameter parameter) {
115126 * @return a {@code Introspector} instance considering nullability declarations from the {@link Class} and package.
116127 */
117128 static Introspector introspect (Class <?> cls ) {
118- return new NullabilityIntrospector (cls );
129+ return new NullabilityIntrospector (cls , true );
119130 }
120131
121132 /**
@@ -125,14 +136,25 @@ static Introspector introspect(Class<?> cls) {
125136 * @return a {@code Introspector} instance considering nullability declarations from package.
126137 */
127138 static Introspector introspect (Package pkg ) {
128- return new NullabilityIntrospector (pkg );
139+ return new NullabilityIntrospector (pkg , true );
129140 }
130141
131142 /**
132143 * Nullability introspector to introspect multiple elements within the context of their source container.
133144 */
134145 interface Introspector {
135146
147+ /**
148+ * Creates a new {@link MethodNullability} instance by introspecting the {@link Method}.
149+ * <p>
150+ * If the method parameter does not declare any nullability rules, then introspection falls back to the source
151+ * container that was used to create the introspector.
152+ *
153+ * @param method the source method.
154+ * @return a {@code Nullability} instance containing the element's nullability declaration.
155+ */
156+ MethodNullability forMethod (Method method );
157+
136158 /**
137159 * Creates a new {@link Nullability} instance by introspecting the {@link MethodParameter}.
138160 * <p>
@@ -159,7 +181,7 @@ default Nullability forParameter(MethodParameter parameter) {
159181 Nullability forReturnType (Method method );
160182
161183 /**
162- * Creates a new {@link Nullability} instance by introspecting the {@link MethodParameter }.
184+ * Creates a new {@link Nullability} instance by introspecting the {@link Parameter }.
163185 * <p>
164186 * If the method parameter does not declare any nullability rules, then introspection falls back to the source
165187 * container that was used to create the introspector.
@@ -171,4 +193,41 @@ default Nullability forParameter(MethodParameter parameter) {
171193
172194 }
173195
196+ /**
197+ * Nullability introspector to introspect multiple elements within the context of their source container. Inherited
198+ * nullability methods nullability of the method return type.
199+ */
200+ interface MethodNullability extends Nullability {
201+
202+ /**
203+ * Returns a {@link Nullability} instance for the method return type.
204+ *
205+ * @return a {@link Nullability} instance for the method return type.
206+ */
207+ default Nullability forReturnType () {
208+ return this ;
209+ }
210+
211+ /**
212+ * Returns a {@link Nullability} instance for a method parameter.
213+ *
214+ * @param parameter the method parameter.
215+ * @return a {@link Nullability} instance for a method parameter.
216+ * @throws IllegalArgumentException if the method parameter is not defined by the underlying method.
217+ */
218+ Nullability forParameter (Parameter parameter );
219+
220+ /**
221+ * Returns a {@link Nullability} instance for a method parameter.
222+ *
223+ * @param parameter the method parameter.
224+ * @return a {@link Nullability} instance for a method parameter.
225+ * @throws IllegalArgumentException if the method parameter is not defined by the underlying method.
226+ */
227+ default Nullability forParameter (MethodParameter parameter ) {
228+ return parameter .getParameterIndex () == -1 ? forReturnType () : forParameter (parameter .getParameter ());
229+ }
230+
231+ }
232+
174233}
0 commit comments