Skip to content

Commit

Permalink
Add predicate springAnnotatedWith(String)
Browse files Browse the repository at this point in the history
  • Loading branch information
rweisleder committed Nov 29, 2023
1 parent b7cf1dc commit 64fa0df
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 203 deletions.
24 changes: 1 addition & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@
<artifactId>spring-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -74,25 +68,9 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.lang.reflect.Method;

import static com.tngtech.archunit.base.DescribedPredicate.describe;
import static com.tngtech.archunit.core.domain.Formatters.ensureSimpleName;

/**
* Collection of {@link DescribedPredicate predicates} that can be used with ArchUnit to check elements for the
Expand Down Expand Up @@ -63,6 +64,37 @@ public static DescribedPredicate<CanBeAnnotated> springAnnotatedWith(Class<? ext
});
}

/**
* Returns a predicate that matches elements that are directly or meta-annotated with the given annotation type.
* <p>
* As an example:
* <pre>{@code
* @RestController
* class DemoRestController {
* }
*
* // matches the class:
* springAnnotatedWith("org.springframework.web.bind.annotation.RestController")
* springAnnotatedWith("org.springframework.stereotype.Controller")
* springAnnotatedWith("org.springframework.stereotype.Component")
* springAnnotatedWith("org.springframework.web.bind.annotation.ResponseBody")
*
* // does not match the class:
* springAnnotatedWith("org.springframework.stereotype.Service")
* }</pre>
*
* @param annotationTypeName the fully qualified class name of the annotation type to check
* @see MergedAnnotations#isPresent(String)
* @see CanBeAnnotated.Predicates#annotatedWith(String)
* @see CanBeAnnotated.Predicates#metaAnnotatedWith(String)
*/
public static DescribedPredicate<CanBeAnnotated> springAnnotatedWith(String annotationTypeName) {
return describe("annotated with @" + ensureSimpleName(annotationTypeName), annotated -> {
MergedAnnotations mergedAnnotations = getMergedAnnotations(annotated);
return mergedAnnotations.isPresent(annotationTypeName);
});
}

/**
* Returns a predicate that matches elements that are directly or meta-annotated with the given annotation type
* matching the given predicate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.conditions.ArchConditions;
import org.springframework.boot.SpringBootConfiguration;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -50,7 +49,7 @@ public static ArchCondition<JavaClass> beInApplicationPackage() {
@Override
public void init(Collection<JavaClass> javaClasses) {
List<JavaClass> springBootApplicationClasses = javaClasses.stream()
.filter(springAnnotatedWith(SpringBootConfiguration.class))
.filter(springAnnotatedWith("org.springframework.boot.SpringBootConfiguration"))
.collect(toList());

if (springBootApplicationClasses.isEmpty()) {
Expand Down
Loading

0 comments on commit 64fa0df

Please sign in to comment.