Skip to content

Commit

Permalink
Add New Rule for Methods Matching Specific Naming Patterns to Be Anno…
Browse files Browse the repository at this point in the history
…tated with a Specified Annotation

Closes gh-111
  • Loading branch information
mnhock authored and mnhock committed Oct 5, 2024
1 parent 2f6d908 commit 9edacea
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/USERGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ The default mode is `WITHOUT_TESTS`, which excludes test classes from the import
| General | `fieldsShouldNotBePublic` | Fields should not be `public`, except constants. |
| General | `methodsShouldNotDeclareGenericExceptions` | Methods should not declare generic exceptions, like `Exception` or `RuntimeException`. |
| General | `methodsShouldNotDeclareException` | Methods with names matching a specified pattern should not declare a specified exception type. |
| General | `methodsShouldBeAnnotatedWith` | Methods matching specific naming patterns should be annotated with a specified annotation. |
| General | `methodsShouldBeAnnotatedWithAll` | Methods annotated with a specific annotation should be annotated with a specified annotations. |
| General | `noUsageOf` | Disallow usage of specific classes. |
| General | `noUsageOfDeprecatedAPIs` | No usage of deprecated APIs annotated with `@Deprecated`. |
Expand Down Expand Up @@ -348,6 +349,17 @@ Taikai.builder()
.check();
```

- **Methods Should Be Annotated with Specified Annotation**: Ensure that methods matching a specific regex pattern are annotated with the specified annotation.

```java
Taikai.builder()
.namespace("com.company.project")
.java(java -> java
.methodsShouldBeAnnotatedWith(".*Api", PublicApi.class)
.methodsShouldBeAnnotatedWith(".*Api", "com.company.project.PublicApi"))
.build()
.check();
```

- **Methods Annotated with a Specified Annotation Should Be Annotated with Specified Annotations**: Ensure that methods annotated with a specific annotations should be annotated with the specified annotations.

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/enofex/taikai/java/JavaConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,29 @@ public JavaConfigurer methodsShouldNotDeclareException(String regex, String type
configuration));
}

public JavaConfigurer methodsShouldBeAnnotatedWith(String regex,
Class<? extends Annotation> annotationType) {
return methodsShouldBeAnnotatedWith(regex, annotationType.getName(), defaultConfiguration());
}

public JavaConfigurer methodsShouldBeAnnotatedWith(String regex,
Class<? extends Annotation> annotationType, Configuration configuration) {
return methodsShouldBeAnnotatedWith(regex, annotationType.getName(), configuration);
}

public JavaConfigurer methodsShouldBeAnnotatedWith(String regex, String annotationType) {
return methodsShouldBeAnnotatedWith(regex, annotationType, defaultConfiguration());
}

public JavaConfigurer methodsShouldBeAnnotatedWith(String regex, String annotationType,
Configuration configuration) {
return addRule(TaikaiRule.of(methods()
.that().haveNameMatching(regex)
.should().beMetaAnnotatedWith(annotationType)
.as("Methods have name matching %s should be annotated with %s".formatted(regex,
annotationType)), configuration));
}

public JavaConfigurer methodsShouldBeAnnotatedWithAll(Class<? extends Annotation> annotationType,
Collection<Class<? extends Annotation>> requiredAnnotationTypes) {
return methodsShouldBeAnnotatedWithAll(annotationType.getName(),
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/enofex/taikai/Usage.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public static void main(String[] args) {
.classesShouldBeAnnotatedWithAll("org.junit.jupiter.api.DisplayName", List.of())
.classesShouldBeAnnotatedWithAll("org.junit.jupiter.api.DisplayName", List.of(), defaultConfiguration())

.methodsShouldBeAnnotatedWith("regex", DisplayName.class)
.methodsShouldBeAnnotatedWith("regex", DisplayName.class, defaultConfiguration())
.methodsShouldBeAnnotatedWith("regex", "org.junit.jupiter.api.DisplayName")
.methodsShouldBeAnnotatedWith("regex", "org.junit.jupiter.api.DisplayName", defaultConfiguration())

.methodsShouldNotDeclareGenericExceptions()
.methodsShouldNotDeclareGenericExceptions(defaultConfiguration())

Expand Down

0 comments on commit 9edacea

Please sign in to comment.