@@ -78,14 +78,24 @@ public interface Nullability {
78
78
*/
79
79
boolean isNonNull ();
80
80
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
+
81
91
/**
82
92
* Creates a new {@link Nullability} instance by introspecting the {@link MethodParameter}.
83
93
*
84
94
* @param parameter the source method parameter.
85
95
* @return a {@code Nullability} instance containing the element's nullability declaration.
86
96
*/
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 );
89
99
}
90
100
91
101
/**
@@ -95,7 +105,7 @@ static Nullability from(MethodParameter parameter) {
95
105
* @return a {@code Nullability} instance containing the element's nullability declaration.
96
106
*/
97
107
static Nullability forMethodReturnType (Method method ) {
98
- return introspect (method .getDeclaringClass ()).forReturnType (method );
108
+ return new NullabilityIntrospector (method .getDeclaringClass (), false ).forReturnType (method );
99
109
}
100
110
101
111
/**
@@ -104,8 +114,9 @@ static Nullability forMethodReturnType(Method method) {
104
114
* @param parameter the source method parameter.
105
115
* @return a {@code Nullability} instance containing the element's nullability declaration.
106
116
*/
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 );
109
120
}
110
121
111
122
/**
@@ -115,7 +126,7 @@ static Nullability forMethodParameter(Parameter parameter) {
115
126
* @return a {@code Introspector} instance considering nullability declarations from the {@link Class} and package.
116
127
*/
117
128
static Introspector introspect (Class <?> cls ) {
118
- return new NullabilityIntrospector (cls );
129
+ return new NullabilityIntrospector (cls , true );
119
130
}
120
131
121
132
/**
@@ -125,14 +136,25 @@ static Introspector introspect(Class<?> cls) {
125
136
* @return a {@code Introspector} instance considering nullability declarations from package.
126
137
*/
127
138
static Introspector introspect (Package pkg ) {
128
- return new NullabilityIntrospector (pkg );
139
+ return new NullabilityIntrospector (pkg , true );
129
140
}
130
141
131
142
/**
132
143
* Nullability introspector to introspect multiple elements within the context of their source container.
133
144
*/
134
145
interface Introspector {
135
146
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
+
136
158
/**
137
159
* Creates a new {@link Nullability} instance by introspecting the {@link MethodParameter}.
138
160
* <p>
@@ -159,7 +181,7 @@ default Nullability forParameter(MethodParameter parameter) {
159
181
Nullability forReturnType (Method method );
160
182
161
183
/**
162
- * Creates a new {@link Nullability} instance by introspecting the {@link MethodParameter }.
184
+ * Creates a new {@link Nullability} instance by introspecting the {@link Parameter }.
163
185
* <p>
164
186
* If the method parameter does not declare any nullability rules, then introspection falls back to the source
165
187
* container that was used to create the introspector.
@@ -171,4 +193,41 @@ default Nullability forParameter(MethodParameter parameter) {
171
193
172
194
}
173
195
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
+
174
233
}
0 commit comments