From e5947540da77f0c22db8ed7eea5d9f5dc3efd401 Mon Sep 17 00:00:00 2001 From: mnhock Date: Wed, 12 Jun 2024 17:20:08 +0200 Subject: [PATCH] Provide a New Java Rule checking if a class is not used Closes gh-19 --- docs/USERGUIDE.md | 22 +++++++++++++++++++ .../enofex/taikai/java/JavaConfigurer.java | 19 ++++++++++++++++ .../com/enofex/taikai/ArchitectureTest.java | 6 +++++ src/test/java/com/enofex/taikai/Usage.java | 7 ++++++ 4 files changed, 54 insertions(+) diff --git a/docs/USERGUIDE.md b/docs/USERGUIDE.md index e129b61..182c094 100644 --- a/docs/USERGUIDE.md +++ b/docs/USERGUIDE.md @@ -123,6 +123,28 @@ Taikai.builder() .check(); ``` +- **No Usage of Specific Classes**: Ensure that certain classes are not used in your codebase. + +```java +Taikai.builder() + .namespace("com.company.yourproject") + .java(java -> java + .noUsageOf("com.example.UnwantedClass")) + .build() + .check(); +``` + +- **No Usage of Specific Classes by Class Reference**: Ensure that certain classes are not used in your codebase by directly referencing the class. + +```java +Taikai.builder() + .namespace("com.company.yourproject") + .java(java -> java + .noUsageOf(UnwantedClass.class)) + .build() + .check(); +``` + #### Spring Configuration Spring configuration involves defining constraints specific to Spring Framework usage. diff --git a/src/main/java/com/enofex/taikai/java/JavaConfigurer.java b/src/main/java/com/enofex/taikai/java/JavaConfigurer.java index 119560e..6a6a485 100644 --- a/src/main/java/com/enofex/taikai/java/JavaConfigurer.java +++ b/src/main/java/com/enofex/taikai/java/JavaConfigurer.java @@ -9,6 +9,7 @@ import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.fields; import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods; +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses; import com.enofex.taikai.TaikaiRule; import com.enofex.taikai.TaikaiRule.Configuration; @@ -76,6 +77,24 @@ public JavaConfigurer fieldsShouldNotBePublic(Configuration configuration) { return addRule(TaikaiRule.of(fields().should(notBePublic()), configuration)); } + public JavaConfigurer noUsageOf(Class clazz) { + return noUsageOf(clazz, null); + } + + public JavaConfigurer noUsageOf(String typeName) { + return noUsageOf(typeName, null); + } + + public JavaConfigurer noUsageOf(String typeName, Configuration configuration) { + return addRule(TaikaiRule.of(noClasses() + .should().dependOnClassesThat().areAssignableTo(typeName), configuration)); + } + + public JavaConfigurer noUsageOf(Class clazz, Configuration configuration) { + return addRule(TaikaiRule.of(noClasses().should().dependOnClassesThat().areAssignableTo(clazz), + configuration)); + } + @Override public void disable() { disable(ImportsConfigurer.class); diff --git a/src/test/java/com/enofex/taikai/ArchitectureTest.java b/src/test/java/com/enofex/taikai/ArchitectureTest.java index e8cb648..bfc3a52 100644 --- a/src/test/java/com/enofex/taikai/ArchitectureTest.java +++ b/src/test/java/com/enofex/taikai/ArchitectureTest.java @@ -1,5 +1,8 @@ package com.enofex.taikai; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; import org.junit.jupiter.api.Test; class ArchitectureTest { @@ -18,6 +21,9 @@ void shouldFulfilConstrains() { .methodsShouldNotThrowGenericException() .utilityClassesShouldBeFinalAndHavePrivateConstructor() .fieldsShouldNotBePublic() + .noUsageOf(Date.class) + .noUsageOf(Calendar.class) + .noUsageOf("java.text.SimpleDateFormat") .imports(imports -> imports .shouldHaveNoCycles() .shouldNotImport("..shaded..") diff --git a/src/test/java/com/enofex/taikai/Usage.java b/src/test/java/com/enofex/taikai/Usage.java index 4adeb89..0d32b8e 100644 --- a/src/test/java/com/enofex/taikai/Usage.java +++ b/src/test/java/com/enofex/taikai/Usage.java @@ -1,5 +1,9 @@ package com.enofex.taikai; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + class Usage { public static void main(String[] args) { @@ -35,6 +39,9 @@ public static void main(String[] args) { .classesShouldImplementHashCodeAndEquals() .methodsShouldNotThrowGenericException() .utilityClassesShouldBeFinalAndHavePrivateConstructor() + .noUsageOf(Date.class) + .noUsageOf(Calendar.class) + .noUsageOf("java.text.SimpleDateFormat") .imports(imports -> imports .shouldHaveNoCycles() .shouldNotImport("..shaded..")