1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -114,19 +114,6 @@ public AnnotationAttributes(Class<? extends Annotation> annotationType) {
114
114
this (annotationType , false );
115
115
}
116
116
117
- /**
118
- * Create a new, empty {@link AnnotationAttributes} instance for the
119
- * specified {@code annotationType}.
120
- * @param annotationType the annotation type name represented by this
121
- * {@code AnnotationAttributes} instance; never {@code null}
122
- * @param classLoader the ClassLoader to try to load the annotation type on,
123
- * or {@code null} to just store the annotation type name
124
- * @since 4.3.2
125
- */
126
- public AnnotationAttributes (String annotationType , @ Nullable ClassLoader classLoader ) {
127
- this (getAnnotationType (annotationType , classLoader ), false );
128
- }
129
-
130
117
/**
131
118
* Create a possibly already validated new, empty
132
119
* {@link AnnotationAttributes} instance for the specified
@@ -143,6 +130,21 @@ public AnnotationAttributes(String annotationType, @Nullable ClassLoader classLo
143
130
this .validated = validated ;
144
131
}
145
132
133
+ /**
134
+ * Create a new, empty {@link AnnotationAttributes} instance for the
135
+ * specified {@code annotationType}.
136
+ * @param annotationType the annotation type name represented by this
137
+ * {@code AnnotationAttributes} instance; never {@code null}
138
+ * @param classLoader the ClassLoader to try to load the annotation type on,
139
+ * or {@code null} to just store the annotation type name
140
+ * @since 4.3.2
141
+ */
142
+ public AnnotationAttributes (String annotationType , @ Nullable ClassLoader classLoader ) {
143
+ Assert .notNull (annotationType , "'annotationType' must not be null" );
144
+ this .annotationType = getAnnotationType (annotationType , classLoader );
145
+ this .displayName = annotationType ;
146
+ this .validated = false ;
147
+ }
146
148
147
149
@ SuppressWarnings ("unchecked" )
148
150
@ Nullable
@@ -349,39 +351,29 @@ public <A extends Annotation> A[] getAnnotationArray(String attributeName, Class
349
351
private <T > T getRequiredAttribute (String attributeName , Class <T > expectedType ) {
350
352
Assert .hasText (attributeName , "'attributeName' must not be null or empty" );
351
353
Object value = get (attributeName );
352
- assertAttributePresence (attributeName , value );
353
- assertNotException (attributeName , value );
354
+ if (value == null ) {
355
+ throw new IllegalArgumentException (String .format (
356
+ "Attribute '%s' not found in attributes for annotation [%s]" ,
357
+ attributeName , this .displayName ));
358
+ }
359
+ if (value instanceof Throwable throwable ) {
360
+ throw new IllegalArgumentException (String .format (
361
+ "Attribute '%s' for annotation [%s] was not resolvable due to exception [%s]" ,
362
+ attributeName , this .displayName , value ), throwable );
363
+ }
354
364
if (!expectedType .isInstance (value ) && expectedType .isArray () &&
355
365
expectedType .componentType ().isInstance (value )) {
356
366
Object array = Array .newInstance (expectedType .componentType (), 1 );
357
367
Array .set (array , 0 , value );
358
368
value = array ;
359
369
}
360
- assertAttributeType (attributeName , value , expectedType );
361
- return (T ) value ;
362
- }
363
-
364
- private void assertAttributePresence (String attributeName , Object attributeValue ) {
365
- Assert .notNull (attributeValue , () -> String .format (
366
- "Attribute '%s' not found in attributes for annotation [%s]" ,
367
- attributeName , this .displayName ));
368
- }
369
-
370
- private void assertNotException (String attributeName , Object attributeValue ) {
371
- if (attributeValue instanceof Throwable throwable ) {
372
- throw new IllegalArgumentException (String .format (
373
- "Attribute '%s' for annotation [%s] was not resolvable due to exception [%s]" ,
374
- attributeName , this .displayName , attributeValue ), throwable );
375
- }
376
- }
377
-
378
- private void assertAttributeType (String attributeName , Object attributeValue , Class <?> expectedType ) {
379
- if (!expectedType .isInstance (attributeValue )) {
370
+ if (!expectedType .isInstance (value )) {
380
371
throw new IllegalArgumentException (String .format (
381
372
"Attribute '%s' is of type %s, but %s was expected in attributes for annotation [%s]" ,
382
- attributeName , attributeValue .getClass ().getSimpleName (), expectedType .getSimpleName (),
373
+ attributeName , value .getClass ().getSimpleName (), expectedType .getSimpleName (),
383
374
this .displayName ));
384
375
}
376
+ return (T ) value ;
385
377
}
386
378
387
379
@ Override
0 commit comments