33
33
import org .springframework .core .annotation .AnnotatedElementUtils ;
34
34
import org .springframework .core .annotation .AnnotationUtils ;
35
35
import org .springframework .core .annotation .MergedAnnotations ;
36
+ import org .springframework .lang .Contract ;
36
37
import org .springframework .lang .NonNullApi ;
37
38
import org .springframework .lang .Nullable ;
38
39
import org .springframework .util .ClassUtils ;
@@ -100,10 +101,21 @@ private NullableUtils() {}
100
101
*
101
102
* @param method the method to inspect.
102
103
* @param elementType the element type.
103
- * @return {@literal true} if {@link ElementType} allows {@literal null} values by default.
104
+ * @return {@literal false} if {@link ElementType} allows {@literal null} values by default. {@literal true} if the
105
+ * methods return type is a {@link Class#isPrimitive() primitive} and {@literal false} if the method is
106
+ * {@literal void}.
104
107
* @see #isNonNull(Annotation, ElementType)
105
108
*/
106
109
public static boolean isNonNull (Method method , ElementType elementType ) {
110
+
111
+ Class <?> returnType = method .getReturnType ();
112
+
113
+ if (ReflectionUtils .isVoid (returnType )) {
114
+ return false ;
115
+ } else if (returnType .isPrimitive ()) {
116
+ return true ;
117
+ }
118
+
107
119
return isNonNull (method .getDeclaringClass (), elementType ) || isNonNull ((AnnotatedElement ) method , elementType );
108
120
}
109
121
@@ -114,10 +126,16 @@ public static boolean isNonNull(Method method, ElementType elementType) {
114
126
*
115
127
* @param type the class to inspect.
116
128
* @param elementType the element type.
117
- * @return {@literal true} if {@link ElementType} allows {@literal null} values by default.
129
+ * @return {@literal false} if {@link ElementType} allows {@literal null} values. {@literal true} the given
130
+ * {@literal type} is a {@link Class#isPrimitive() primitive}.
118
131
* @see #isNonNull(Annotation, ElementType)
119
132
*/
120
133
public static boolean isNonNull (Class <?> type , ElementType elementType ) {
134
+
135
+ if (type .isPrimitive ()) {
136
+ return true ;
137
+ }
138
+
121
139
return isNonNull (type .getPackage (), elementType ) || isNonNull ((AnnotatedElement ) type , elementType );
122
140
}
123
141
@@ -126,11 +144,17 @@ public static boolean isNonNull(Class<?> type, ElementType elementType) {
126
144
* This method determines default {@code javax.annotation.Nonnull nullability} rules from the annotated element
127
145
*
128
146
* @param element the scope of declaration, may be a {@link Package}, {@link Class}, or
129
- * {@link java.lang.reflect.Method}.
147
+ * {@link java.lang.reflect.Method}. Can be {@literal null}.
130
148
* @param elementType the element type.
131
- * @return {@literal true} if {@link ElementType} allows {@literal null} values by default.
149
+ * @return {@literal false} if {@link ElementType} allows {@literal null} values by default or if the given
150
+ * {@link AnnotatedElement} is {@literal null}.
132
151
*/
133
- public static boolean isNonNull (AnnotatedElement element , ElementType elementType ) {
152
+ @ Contract ("null, _ -> false" )
153
+ public static boolean isNonNull (@ Nullable AnnotatedElement element , ElementType elementType ) {
154
+
155
+ if (element == null ) {
156
+ return false ;
157
+ }
134
158
135
159
for (Annotation annotation : element .getAnnotations ()) {
136
160
@@ -157,8 +181,7 @@ private static boolean isNonNull(Annotation annotation, ElementType elementType)
157
181
return true ;
158
182
}
159
183
160
- if (!MergedAnnotations .from (annotation .annotationType ()).isPresent (annotationClass )
161
- || !isNonNull (annotation )) {
184
+ if (!MergedAnnotations .from (annotation .annotationType ()).isPresent (annotationClass ) || !isNonNull (annotation )) {
162
185
return false ;
163
186
}
164
187
0 commit comments