Skip to content

Commit

Permalink
Provide a New Java Rule checking fields by disallowing public fields
Browse files Browse the repository at this point in the history
Closes gh-18
  • Loading branch information
mnhock committed Jun 11, 2024
1 parent d21e838 commit fae6e48
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/USERGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ Taikai.builder()
.check();
```

- **Fields Should Not Be Public**: Ensure that no fields in your Java classes are declared as `public`, except constants.

```java
Taikai.builder()
.namespace("com.enofex.taikai")
.java(java -> java
.fieldsShouldNotBePublic())
.build()
.check();
```

- **Imports Configuration**: Ensure that there are no cyclic dependencies in imports and disallow specific imports.

```java
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/enofex/taikai/java/FieldsShouldNotBePublic.java
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));
}
}
};
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/enofex/taikai/java/JavaConfigurer.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.enofex.taikai.java;

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.UtilityClasses.beFinal;
import static com.enofex.taikai.java.UtilityClasses.havePrivateConstructor;
import static com.enofex.taikai.java.UtilityClasses.utilityClasses;
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 com.enofex.taikai.TaikaiRule;
Expand Down Expand Up @@ -66,6 +68,14 @@ public JavaConfigurer classesShouldImplementHashCodeAndEquals(Configuration conf
return addRule(TaikaiRule.of(classes().should(implementHashCodeAndEquals()), configuration));
}

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

public JavaConfigurer fieldsShouldNotBePublic(Configuration configuration) {
return addRule(TaikaiRule.of(fields().should(notBePublic()), configuration));
}

@Override
public void disable() {
disable(ImportsConfigurer.class);
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/enofex/taikai/ArchitectureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ void shouldFulfilConstrains() {
.classesShouldImplementHashCodeAndEquals()
.methodsShouldNotThrowGenericException()
.utilityClassesShouldBeFinalAndHavePrivateConstructor()
.fieldsShouldNotBePublic()
.imports(imports -> imports
.shouldHaveNoCycles()
.shouldNotImport("..shaded..")
Expand Down

0 comments on commit fae6e48

Please sign in to comment.