2424import com .tngtech .archunit .core .domain .JavaAnnotation ;
2525
2626import static com .tngtech .archunit .PublicAPI .Usage .ACCESS ;
27+ import static com .tngtech .archunit .core .domain .properties .CanBeAnnotated .Utils .toAnnotationOfType ;
2728
2829@ PublicAPI (usage = ACCESS )
2930public interface HasAnnotations <SELF extends HasAnnotations <SELF >> extends CanBeAnnotated , HasDescription {
@@ -33,23 +34,31 @@ public interface HasAnnotations<SELF extends HasAnnotations<SELF>> extends CanBe
3334 /**
3435 * @param type The {@link Class} of the {@link Annotation} to retrieve.
3536 * @return The {@link Annotation} of the given type.
36- * Will throw an {@link IllegalArgumentException} if no matching {@link Annotation} is present.
37+ * @throws IllegalArgumentException if no matching {@link Annotation} is present.
3738 * @param <A> The type of the {@link Annotation} to retrieve
3839 * @see #tryGetAnnotationOfType(Class)
3940 * @see #getAnnotationOfType(String)
4041 */
4142 @ PublicAPI (usage = ACCESS )
42- <A extends Annotation > A getAnnotationOfType (Class <A > type );
43+ default <A extends Annotation > A getAnnotationOfType (Class <A > type ) {
44+ return getAnnotationOfType (type .getName ()).as (type );
45+ }
4346
4447 /**
4548 * @param typeName The fully qualified class name of the {@link Annotation} type to retrieve.
4649 * @return The {@link JavaAnnotation} matching the given type.
47- * Will throw an {@link IllegalArgumentException} if no matching {@link Annotation} is present.
50+ * @throws IllegalArgumentException if no matching {@link Annotation} is present.
4851 * @see #tryGetAnnotationOfType(String)
4952 * @see #getAnnotationOfType(Class)
5053 */
5154 @ PublicAPI (usage = ACCESS )
52- JavaAnnotation <? extends SELF > getAnnotationOfType (String typeName );
55+ default JavaAnnotation <? extends SELF > getAnnotationOfType (String typeName ) {
56+ Optional <? extends JavaAnnotation <? extends SELF >> annotation = tryGetAnnotationOfType (typeName );
57+ if (!annotation .isPresent ()) {
58+ throw new IllegalArgumentException (String .format ("%s is not annotated with @%s" , getDescription (), typeName ));
59+ }
60+ return annotation .get ();
61+ }
5362
5463 /**
5564 * @param type The {@link Class} of the {@link Annotation} to retrieve.
@@ -60,7 +69,9 @@ public interface HasAnnotations<SELF extends HasAnnotations<SELF>> extends CanBe
6069 * @see #tryGetAnnotationOfType(String)
6170 */
6271 @ PublicAPI (usage = ACCESS )
63- <A extends Annotation > Optional <A > tryGetAnnotationOfType (Class <A > type );
72+ default <A extends Annotation > Optional <A > tryGetAnnotationOfType (Class <A > type ) {
73+ return tryGetAnnotationOfType (type .getName ()).map (toAnnotationOfType (type ));
74+ }
6475
6576 /**
6677 * @param typeName The fully qualified class name of the {@link Annotation} type to retrieve.
0 commit comments