-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a New Java Rule checking if System.out and System.err is not …
…used Closes gh-20
- Loading branch information
Showing
5 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/com/enofex/taikai/java/NoSystemOutOrErr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters