Skip to content

Commit

Permalink
Provide new Java Rule for checking classes reside in a specified package
Browse files Browse the repository at this point in the history
Closes gh-64
  • Loading branch information
mnhock committed Jul 6, 2024
1 parent a672da3 commit e9d6252
Show file tree
Hide file tree
Showing 3 changed files with 24 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 @@ -108,6 +108,7 @@ The default mode is `WITHOUT_TESTS`, which excludes test classes from the import
| Category | Method Name | Rule Description |
|----------|--------------------------------------------------------|-----------------------------------------------------------------------------------------------------------|
| General | `classesShouldImplementHashCodeAndEquals` | Classes should implement `hashCode` and `equals` together |
| General | `classesShouldResideInPackage` | Classes match a specific naming patterns should reside in a specified package |
| General | `fieldsShouldNotBePublic` | Fields should not be `public`, except constants |
| General | `methodsShouldNotDeclareGenericExceptions` | Methods should not declare generic exceptions, like `Exception`, `RuntimeException` |
| General | `noUsageOf` | Disallow usage of specific classes |
Expand Down Expand Up @@ -208,6 +209,17 @@ Taikai.builder()
.check();
```

- **Classes Should Reside in Specified Package**: Ensure that classes matching a specific regex pattern reside in the specified package.

```java
Taikai.builder()
.namespace("com.company.yourproject")
.java(java -> java
.classesShouldResideInPackage(".*Utils", "com.company.yourproject.utils"))
.build()
.check();
```

- **Methods Should Not Throw Generic Exception**: Ensure that methods do not throw generic exceptions like `Exception` and `RuntimeException` and use specific exception types instead.

```java
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/enofex/taikai/java/JavaConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ public JavaConfigurer noUsageOfDeprecatedAPIs(Configuration configuration) {
return addRule(TaikaiRule.of(classes().should(notUseDeprecatedAPIs()), configuration));
}

public JavaConfigurer classesShouldResideInPackage(String regex, String packageIdentifier) {
return classesShouldResideInPackage(regex, packageIdentifier, null);
}

public JavaConfigurer classesShouldResideInPackage(String regex, String packageIdentifier,
Configuration configuration) {
return addRule(TaikaiRule.of(classes()
.that().haveNameMatching(regex)
.should().resideInAPackage(packageIdentifier), configuration));
}

public JavaConfigurer classesShouldImplementHashCodeAndEquals() {
return classesShouldImplementHashCodeAndEquals(null);
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/enofex/taikai/Usage.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static void main(String[] args) {
.noUsageOfSystemOutOrErr()
.noUsageOfDeprecatedAPIs()
.classesShouldImplementHashCodeAndEquals()
.classesShouldResideInPackage("regex", "com.enofex.taikai")
.methodsShouldNotDeclareGenericExceptions()
.finalClassesShouldNotHaveProtectedMembers()
.utilityClassesShouldBeFinalAndHavePrivateConstructor()
Expand Down

0 comments on commit e9d6252

Please sign in to comment.