-
-
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 fields by disallowing public fields
Closes gh-18
- Loading branch information
Showing
4 changed files
with
50 additions
and
0 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
28 changes: 28 additions & 0 deletions
28
src/main/java/com/enofex/taikai/java/FieldsShouldNotBePublic.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,28 @@ | ||
package com.enofex.taikai.java; | ||
|
||
import com.tngtech.archunit.core.domain.JavaField; | ||
import com.tngtech.archunit.core.domain.JavaModifier; | ||
import com.tngtech.archunit.lang.ArchCondition; | ||
import com.tngtech.archunit.lang.ConditionEvents; | ||
import com.tngtech.archunit.lang.SimpleConditionEvent; | ||
|
||
final class FieldsShouldNotBePublic { | ||
|
||
private FieldsShouldNotBePublic() { | ||
} | ||
|
||
static ArchCondition<JavaField> notBePublic() { | ||
return new ArchCondition<>("not be public") { | ||
@Override | ||
public void check(JavaField field, ConditionEvents events) { | ||
if (!field.getModifiers().contains(JavaModifier.STATIC) | ||
&& field.getModifiers().contains(JavaModifier.PUBLIC)) { | ||
String message = String.format("Field %s in class %s is public", | ||
field.getName(), | ||
field.getOwner().getFullName()); | ||
events.add(SimpleConditionEvent.violated(field, 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