Skip to content

Commit

Permalink
Provide possibility to add package identifier to Java noUsageOf Rules
Browse files Browse the repository at this point in the history
Closes gh-90
  • Loading branch information
mnhock committed Aug 31, 2024
1 parent 4147c6b commit 45d658b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
6 changes: 4 additions & 2 deletions docs/USERGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ Taikai.builder()
Taikai.builder()
.namespace("com.company.yourproject")
.java(java -> java
.noUsageOf("com.example.UnwantedClass"))
.noUsageOf("com.example.UnwantedClass")
.noUsageOf("com.example.UnwantedClass", "in.specific.package"))
.build()
.check();
```
Expand All @@ -357,7 +358,8 @@ Taikai.builder()
Taikai.builder()
.namespace("com.company.yourproject")
.java(java -> java
.noUsageOf(UnwantedClass.class))
.noUsageOf(UnwantedClass.class)
.noUsageOf(UnwantedClass.class, "in.specific.package"))
.build()
.check();
```
Expand Down
48 changes: 42 additions & 6 deletions src/main/java/com/enofex/taikai/java/JavaConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,57 @@ public JavaConfigurer fieldsShouldNotBePublic(Configuration configuration) {
}

public JavaConfigurer noUsageOf(Class<?> clazz) {
return noUsageOf(clazz, Configuration.defaultConfiguration());
return noUsageOf(clazz, null, Configuration.defaultConfiguration());
}

public JavaConfigurer noUsageOf(Class<?> clazz, String packageIdentifier) {
return noUsageOf(clazz, packageIdentifier, Configuration.defaultConfiguration());
}

public JavaConfigurer noUsageOf(Class<?> clazz, Configuration configuration) {
return noUsageOf(clazz, null, configuration);
}

public JavaConfigurer noUsageOf(Class<?> clazz, String packageIdentifier,
Configuration configuration) {
if (packageIdentifier != null) {
return addRule(TaikaiRule.of(noClasses()
.that().resideInAPackage(packageIdentifier)
.should().dependOnClassesThat().areAssignableTo(clazz)
.as("Classes %s reside in %s should not be used".formatted(
clazz, packageIdentifier)), configuration));
}

return addRule(TaikaiRule.of(noClasses()
.should().dependOnClassesThat().areAssignableTo(clazz)
.as("Classes %s should not be used".formatted(clazz)), configuration));
}

public JavaConfigurer noUsageOf(String typeName) {
return noUsageOf(typeName, Configuration.defaultConfiguration());
return noUsageOf(typeName, null, Configuration.defaultConfiguration());
}

public JavaConfigurer noUsageOf(String typeName, String packageIdentifier) {
return noUsageOf(typeName, packageIdentifier, Configuration.defaultConfiguration());
}

public JavaConfigurer noUsageOf(String typeName, Configuration configuration) {
return addRule(TaikaiRule.of(noClasses()
.should().dependOnClassesThat().areAssignableTo(typeName), configuration));
return noUsageOf(typeName, null, configuration);
}

public JavaConfigurer noUsageOf(Class<?> clazz, Configuration configuration) {
public JavaConfigurer noUsageOf(String typeName, String packageIdentifier,
Configuration configuration) {
if (packageIdentifier != null) {
return addRule(TaikaiRule.of(noClasses()
.that().resideInAPackage(packageIdentifier)
.should().dependOnClassesThat().areAssignableTo(typeName)
.as("Classes %s reside in %s should not be used".formatted(
typeName, packageIdentifier)), configuration));
}

return addRule(TaikaiRule.of(noClasses()
.should().dependOnClassesThat().areAssignableTo(clazz), configuration));
.should().dependOnClassesThat().areAssignableTo(typeName)
.as("Classes %s should not be used".formatted(typeName)), configuration));
}

public JavaConfigurer noUsageOfSystemOutOrErr() {
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/enofex/taikai/Usage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public static void main(String[] args) {
.java(java -> java
.noUsageOf(Date.class)
.noUsageOf(Calendar.class)
.noUsageOf(Calendar.class, "com.enofex.taikai.java")
.noUsageOf("java.text.SimpleDateFormat")
.noUsageOf("java.time.LocalDate", "com.enofex.taikai.java")
.noUsageOfSystemOutOrErr()
.noUsageOfDeprecatedAPIs()
.classesShouldImplementHashCodeAndEquals()
Expand Down

0 comments on commit 45d658b

Please sign in to comment.