diff --git a/docs/USERGUIDE.md b/docs/USERGUIDE.md index 9eda760..0725716 100644 --- a/docs/USERGUIDE.md +++ b/docs/USERGUIDE.md @@ -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 | @@ -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 diff --git a/src/main/java/com/enofex/taikai/java/JavaConfigurer.java b/src/main/java/com/enofex/taikai/java/JavaConfigurer.java index a236813..eb344ed 100644 --- a/src/main/java/com/enofex/taikai/java/JavaConfigurer.java +++ b/src/main/java/com/enofex/taikai/java/JavaConfigurer.java @@ -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); } diff --git a/src/test/java/com/enofex/taikai/Usage.java b/src/test/java/com/enofex/taikai/Usage.java index 9472be0..73a818a 100644 --- a/src/test/java/com/enofex/taikai/Usage.java +++ b/src/test/java/com/enofex/taikai/Usage.java @@ -20,6 +20,7 @@ public static void main(String[] args) { .noUsageOfSystemOutOrErr() .noUsageOfDeprecatedAPIs() .classesShouldImplementHashCodeAndEquals() + .classesShouldResideInPackage("regex", "com.enofex.taikai") .methodsShouldNotDeclareGenericExceptions() .finalClassesShouldNotHaveProtectedMembers() .utilityClassesShouldBeFinalAndHavePrivateConstructor()