Skip to content

Commit 7423731

Browse files
refactor: reduce code duplication in children of HasAnnotations (#1730)
- no change in public API function signatures intended (please double-check that this is indeed the case) - almost no change in behaviour (a few error exception messages have changed)
2 parents 103adb8 + 78d5c17 commit 7423731

5 files changed

Lines changed: 24 additions & 88 deletions

File tree

‎archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClass.java‎

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import static com.tngtech.archunit.core.domain.JavaClass.Functions.GET_STATIC_INITIALIZER;
6363
import static com.tngtech.archunit.core.domain.JavaModifier.ENUM;
6464
import static com.tngtech.archunit.core.domain.JavaType.Functions.TO_ERASURE;
65-
import static com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Utils.toAnnotationOfType;
6665
import static com.tngtech.archunit.core.domain.properties.HasName.Functions.GET_NAME;
6766
import static com.tngtech.archunit.core.domain.properties.HasType.Functions.GET_RAW_TYPE;
6867
import static java.util.Arrays.stream;
@@ -585,46 +584,17 @@ public boolean isMetaAnnotatedWith(DescribedPredicate<? super JavaAnnotation<?>>
585584
return CanBeAnnotated.Utils.isMetaAnnotatedWith(annotations.values(), predicate);
586585
}
587586

588-
/**
589-
* @param type A given annotation type to match {@link JavaAnnotation JavaAnnotations} against
590-
* @return An {@link Annotation} of the given annotation type
591-
* @throws IllegalArgumentException if the class is not annotated with the given type
592-
* @see #isAnnotatedWith(Class)
593-
* @see #tryGetAnnotationOfType(Class)
594-
*/
595-
@Override
596-
@PublicAPI(usage = ACCESS)
597-
public <A extends Annotation> A getAnnotationOfType(Class<A> type) {
598-
return getAnnotationOfType(type.getName()).as(type);
599-
}
600-
601-
@Override
602-
@PublicAPI(usage = ACCESS)
603-
public JavaAnnotation<JavaClass> getAnnotationOfType(String typeName) {
604-
Optional<JavaAnnotation<JavaClass>> annotation = tryGetAnnotationOfType(typeName);
605-
if (!annotation.isPresent()) {
606-
throw new IllegalArgumentException(String.format("Type %s is not annotated with @%s", getSimpleName(), typeName));
607-
}
608-
return annotation.get();
609-
}
610-
611587
@Override
612588
@PublicAPI(usage = ACCESS)
613589
public Set<JavaAnnotation<JavaClass>> getAnnotations() {
614590
return ImmutableSet.copyOf(annotations.values());
615591
}
616592

617-
/**
618-
* @param type A given annotation type to match {@link JavaAnnotation JavaAnnotations} against
619-
* @return An {@link Optional} containing an {@link Annotation} of the given annotation type,
620-
* if this class is annotated with the given type, otherwise Optional.absent()
621-
* @see #isAnnotatedWith(Class)
622-
* @see #getAnnotationOfType(Class)
623-
*/
624593
@Override
625594
@PublicAPI(usage = ACCESS)
626-
public <A extends Annotation> Optional<A> tryGetAnnotationOfType(Class<A> type) {
627-
return tryGetAnnotationOfType(type.getName()).map(toAnnotationOfType(type));
595+
@SuppressWarnings("unchecked") // we know the 'owning' element is this class
596+
public JavaAnnotation<JavaClass> getAnnotationOfType(String typeName) {
597+
return (JavaAnnotation<JavaClass>) HasAnnotations.super.getAnnotationOfType(typeName);
628598
}
629599

630600
/**

‎archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMember.java‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import static com.google.common.base.Preconditions.checkNotNull;
4040
import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;
4141
import static com.tngtech.archunit.base.DescribedPredicate.equalTo;
42-
import static com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Utils.toAnnotationOfType;
4342
import static com.tngtech.archunit.core.domain.properties.HasName.Functions.GET_NAME;
4443
import static com.tngtech.archunit.core.domain.properties.HasType.Functions.GET_RAW_TYPE;
4544

@@ -78,33 +77,6 @@ public Set<? extends JavaAnnotation<? extends JavaMember>> getAnnotations() {
7877
return ImmutableSet.copyOf(annotations.values());
7978
}
8079

81-
/**
82-
* Returns the {@link Annotation} of this member of the given {@link Annotation} type.
83-
*
84-
* @throws IllegalArgumentException if there is no annotation of the respective reflection type
85-
*/
86-
@Override
87-
@PublicAPI(usage = ACCESS)
88-
public <A extends Annotation> A getAnnotationOfType(Class<A> type) {
89-
return getAnnotationOfType(type.getName()).as(type);
90-
}
91-
92-
@Override
93-
@PublicAPI(usage = ACCESS)
94-
public JavaAnnotation<? extends JavaMember> getAnnotationOfType(String typeName) {
95-
Optional<? extends JavaAnnotation<? extends JavaMember>> annotation = tryGetAnnotationOfType(typeName);
96-
if (!annotation.isPresent()) {
97-
throw new IllegalArgumentException(String.format("Member %s is not annotated with @%s", getFullName(), typeName));
98-
}
99-
return annotation.get();
100-
}
101-
102-
@Override
103-
@PublicAPI(usage = ACCESS)
104-
public <A extends Annotation> Optional<A> tryGetAnnotationOfType(Class<A> type) {
105-
return tryGetAnnotationOfType(type.getName()).map(toAnnotationOfType(type));
106-
}
107-
10880
@Override
10981
@PublicAPI(usage = ACCESS)
11082
public Optional<? extends JavaAnnotation<? extends JavaMember>> tryGetAnnotationOfType(String typeName) {

‎archunit/src/main/java/com/tngtech/archunit/core/domain/JavaPackage.java‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public Set<? extends JavaAnnotation<JavaPackage>> getAnnotations() {
167167
@Override
168168
@PublicAPI(usage = ACCESS)
169169
public <A extends Annotation> A getAnnotationOfType(Class<A> type) {
170-
return getAnnotationOfType(type.getName()).as(type);
170+
return HasAnnotations.super.getAnnotationOfType(type);
171171
}
172172

173173
/**
@@ -180,12 +180,9 @@ public <A extends Annotation> A getAnnotationOfType(Class<A> type) {
180180
*/
181181
@Override
182182
@PublicAPI(usage = ACCESS)
183+
@SuppressWarnings("unchecked") // we know the 'owning' element is this package
183184
public JavaAnnotation<JavaPackage> getAnnotationOfType(String typeName) {
184-
Optional<JavaAnnotation<JavaPackage>> annotation = tryGetAnnotationOfType(typeName);
185-
if (!annotation.isPresent()) {
186-
throw new IllegalArgumentException(String.format("%s is not annotated with @%s", getDescription(), typeName));
187-
}
188-
return annotation.get();
185+
return (JavaAnnotation<JavaPackage>) HasAnnotations.super.getAnnotationOfType(typeName);
189186
}
190187

191188
/**

‎archunit/src/main/java/com/tngtech/archunit/core/domain/JavaParameter.java‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import static com.tngtech.archunit.base.DescribedPredicate.anyElementThat;
3535
import static com.tngtech.archunit.base.DescribedPredicate.equalTo;
3636
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.equivalentTo;
37-
import static com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Utils.toAnnotationOfType;
3837
import static com.tngtech.archunit.core.domain.properties.HasName.Functions.GET_NAME;
3938
import static com.tngtech.archunit.core.domain.properties.HasType.Functions.GET_RAW_TYPE;
4039

@@ -124,22 +123,9 @@ public Set<JavaAnnotation<JavaParameter>> getAnnotations() {
124123
}
125124

126125
@Override
127-
public <A extends Annotation> A getAnnotationOfType(Class<A> type) {
128-
return getAnnotationOfType(type.getName()).as(type);
129-
}
130-
131-
@Override
126+
@SuppressWarnings("unchecked") // we know the 'owning' element is this parameter
132127
public JavaAnnotation<JavaParameter> getAnnotationOfType(String typeName) {
133-
Optional<JavaAnnotation<JavaParameter>> annotation = tryGetAnnotationOfType(typeName);
134-
if (!annotation.isPresent()) {
135-
throw new IllegalArgumentException(String.format("%s is not annotated with @%s", getDescription(), typeName));
136-
}
137-
return annotation.get();
138-
}
139-
140-
@Override
141-
public <A extends Annotation> Optional<A> tryGetAnnotationOfType(Class<A> type) {
142-
return tryGetAnnotationOfType(type.getName()).map(toAnnotationOfType(type));
128+
return (JavaAnnotation<JavaParameter>) HasAnnotations.super.getAnnotationOfType(typeName);
143129
}
144130

145131
@Override

‎archunit/src/main/java/com/tngtech/archunit/core/domain/properties/HasAnnotations.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.tngtech.archunit.core.domain.JavaAnnotation;
2525

2626
import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;
27+
import static com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Utils.toAnnotationOfType;
2728

2829
@PublicAPI(usage = ACCESS)
2930
public 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

Comments
 (0)