Skip to content

Commit

Permalink
Provide new Java Rule for checking classes reside outside a specified…
Browse files Browse the repository at this point in the history
… package

Closes gh-65
  • Loading branch information
mnhock committed Jul 6, 2024
1 parent e9d6252 commit 5ca2fb5
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 @@ -109,6 +109,7 @@ The default mode is `WITHOUT_TESTS`, which excludes test classes from the import
|----------|--------------------------------------------------------|-----------------------------------------------------------------------------------------------------------|
| General | `classesShouldImplementHashCodeAndEquals` | Classes should implement `hashCode` and `equals` together |
| General | `classesShouldResideInPackage` | Classes match a specific naming patterns should reside in a specified package |
| General | `classesShouldResideOutsidePackage` | Classes match a specific naming patterns should reside outside 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 @@ -220,6 +221,17 @@ Taikai.builder()
.check();
```

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

```java
Taikai.builder()
.namespace("com.company.yourproject")
.java(java -> java
.classesShouldResideOutsidePackage(".*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 @@ -77,6 +77,17 @@ public JavaConfigurer classesShouldResideInPackage(String regex, String packageI
.should().resideInAPackage(packageIdentifier), configuration));
}

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

public JavaConfigurer classesShouldResideOutsidePackage(String regex, String packageIdentifier,
Configuration configuration) {
return addRule(TaikaiRule.of(classes()
.that().haveNameMatching(regex)
.should().resideOutsideOfPackage(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 @@ -21,6 +21,7 @@ public static void main(String[] args) {
.noUsageOfDeprecatedAPIs()
.classesShouldImplementHashCodeAndEquals()
.classesShouldResideInPackage("regex", "com.enofex.taikai")
.classesShouldResideOutsidePackage("regex", "com.enofex.taikai")
.methodsShouldNotDeclareGenericExceptions()
.finalClassesShouldNotHaveProtectedMembers()
.utilityClassesShouldBeFinalAndHavePrivateConstructor()
Expand Down

0 comments on commit 5ca2fb5

Please sign in to comment.