Skip to content

Commit

Permalink
Provide a New Java Rule checking if a class is not used
Browse files Browse the repository at this point in the history
Closes gh-19
  • Loading branch information
mnhock committed Jun 12, 2024
1 parent fae6e48 commit e594754
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/USERGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/enofex/taikai/java/JavaConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/enofex/taikai/ArchitectureTest.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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..")
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/enofex/taikai/Usage.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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..")
Expand Down

0 comments on commit e594754

Please sign in to comment.