Skip to content

Commit

Permalink
Provide a New Java Rule checking if System.out and System.err is not …
Browse files Browse the repository at this point in the history
…used

Closes gh-20
  • Loading branch information
mnhock committed Jun 12, 2024
1 parent 2355d20 commit 22eb2af
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
11 changes: 11 additions & 0 deletions docs/USERGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ Taikai.builder()
.check();
```

- **No Usage of System.out or System.err**: Enforce disallowing the use of System.out and System.err for logging, encouraging the use of proper logging frameworks instead.

```java
Taikai.builder()
.namespace("com.company.yourproject")
.java(java -> java
.noUsageOfSystemOutOrErr())
.build()
.check();
```

## Spring Configuration

Spring configuration involves defining constraints specific to Spring Framework usage.
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/enofex/taikai/java/JavaConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.enofex.taikai.java.Deprecations.notUseDeprecatedAPIs;
import static com.enofex.taikai.java.FieldsShouldNotBePublic.notBePublic;
import static com.enofex.taikai.java.HashCodeAndEquals.implementHashCodeAndEquals;
import static com.enofex.taikai.java.NoSystemOutOrErr.notUseSystemOutOrErr;
import static com.enofex.taikai.java.UtilityClasses.beFinal;
import static com.enofex.taikai.java.UtilityClasses.havePrivateConstructor;
import static com.enofex.taikai.java.UtilityClasses.utilityClasses;
Expand Down Expand Up @@ -95,6 +96,14 @@ public JavaConfigurer noUsageOf(Class clazz, Configuration configuration) {
configuration));
}

public JavaConfigurer noUsageOfSystemOutOrErr() {
return noUsageOfSystemOutOrErr(null);
}

public JavaConfigurer noUsageOfSystemOutOrErr(Configuration configuration) {
return addRule(TaikaiRule.of(classes().should(notUseSystemOutOrErr()), configuration));
}

@Override
public void disable() {
disable(ImportsConfigurer.class);
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/enofex/taikai/java/NoSystemOutOrErr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.enofex.taikai.java;

import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;

final class NoSystemOutOrErr {

private NoSystemOutOrErr() {
}

static ArchCondition<JavaClass> notUseSystemOutOrErr() {
return new ArchCondition<>("not call System.out or System.err") {
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
javaClass.getFieldAccessesFromSelf().stream()
.filter(fieldAccess -> fieldAccess.getTargetOwner().isEquivalentTo(System.class))
.forEach(fieldAccess -> {
String fieldName = fieldAccess.getTarget().getName();

if ("out".equals(fieldName) || "err".equals(fieldName)) {
String message = String.format("Method %s calls %s.%s",
fieldAccess.getOrigin().getFullName(),
fieldAccess.getTargetOwner().getName(),
fieldAccess.getTarget().getName());

events.add(SimpleConditionEvent.violated(fieldAccess, message));
}
});
}
};
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/enofex/taikai/ArchitectureTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.enofex.taikai;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.junit.jupiter.api.Test;
Expand All @@ -21,6 +20,7 @@ void shouldFulfilConstrains() {
.methodsShouldNotThrowGenericException()
.utilityClassesShouldBeFinalAndHavePrivateConstructor()
.fieldsShouldNotBePublic()
.noUsageOfSystemOutOrErr()
.noUsageOf(Date.class)
.noUsageOf(Calendar.class)
.noUsageOf("java.text.SimpleDateFormat")
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/enofex/taikai/Usage.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.enofex.taikai;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

Expand Down Expand Up @@ -35,6 +34,7 @@ public static void main(String[] args) {
.classesShouldNotBeAnnotatedWithDisabled()
.methodsShouldNotBeAnnotatedWithDisabled()))
.java(java -> java
.noUsageOfSystemOutOrErr()
.noUsageOfDeprecatedAPIs()
.classesShouldImplementHashCodeAndEquals()
.methodsShouldNotThrowGenericException()
Expand Down

0 comments on commit 22eb2af

Please sign in to comment.