-
-
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.
- Loading branch information
mnhock
authored and
mnhock
committed
Jun 7, 2024
1 parent
c90a221
commit cdf5b88
Showing
8 changed files
with
177 additions
and
41 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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 Deprecations { | ||
|
||
private Deprecations() { | ||
} | ||
|
||
static ArchCondition<JavaClass> notUseDeprecatedAPIs() { | ||
return new ArchCondition<JavaClass>("not use deprecated APIs") { | ||
@Override | ||
public void check(JavaClass item, ConditionEvents events) { | ||
if (item.isAnnotatedWith(Deprecated.class)) { | ||
events.add(SimpleConditionEvent.violated(item, | ||
String.format("Class %s is deprecated", item.getName()))); | ||
} | ||
|
||
item.getAllFields().stream() | ||
.filter(field -> field.isAnnotatedWith(Deprecated.class)) | ||
.forEach(field -> events.add(SimpleConditionEvent.violated(field, | ||
String.format("Field %s in class %s is deprecated", field.getName(), | ||
item.getName())))); | ||
|
||
item.getAllMethods().stream() | ||
.filter(method -> !method.getOwner().getName().equals(Object.class.getName())) | ||
.filter(method -> !method.getOwner().getName().equals(Enum.class.getName())) | ||
.filter(method -> method.isAnnotatedWith(Deprecated.class)) | ||
.forEach(method -> events.add(SimpleConditionEvent.violated(method, | ||
String.format("Method %s in class %s is deprecated", method.getName(), | ||
item.getName())))); | ||
|
||
item.getAllConstructors().stream() | ||
.filter(constructor -> constructor.isAnnotatedWith(Deprecated.class)) | ||
.forEach(constructor -> events.add(SimpleConditionEvent.violated(constructor, | ||
String.format("Constructor %s in class %s is deprecated", | ||
constructor.getFullName(), item.getName())))); | ||
|
||
item.getDirectDependenciesFromSelf().stream() | ||
.filter(dependency -> dependency.getTargetClass().isAnnotatedWith(Deprecated.class)) | ||
.forEach(dependency -> events.add( | ||
SimpleConditionEvent.violated(dependency.getTargetClass(), | ||
String.format("Class %s references deprecated class %s", item.getName(), | ||
dependency.getTargetClass().getName())))); | ||
} | ||
}.as("No usage of deprecated APIs"); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.enofex.taikai.java; | ||
|
||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; | ||
|
||
import com.tngtech.archunit.base.DescribedPredicate; | ||
import com.tngtech.archunit.core.domain.JavaClass; | ||
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; | ||
import com.tngtech.archunit.lang.syntax.elements.GivenClassesConjunction; | ||
|
||
final class UtilityClasses { | ||
|
||
private UtilityClasses() { | ||
} | ||
|
||
static GivenClassesConjunction utilityClasses() { | ||
return classes().that(haveOnlyStaticMethods()); | ||
} | ||
|
||
static DescribedPredicate<JavaClass> haveOnlyStaticMethods() { | ||
return new DescribedPredicate<>("have only static methods") { | ||
@Override | ||
public boolean test(JavaClass javaClass) { | ||
return !javaClass.getMethods().isEmpty() && javaClass.getMethods().stream() | ||
.allMatch(method -> method.getModifiers().contains(JavaModifier.STATIC) | ||
&& !"main".equals(method.getName())); | ||
} | ||
}; | ||
} | ||
|
||
static ArchCondition<JavaClass> beFinal() { | ||
return new ArchCondition<>("be final") { | ||
@Override | ||
public void check(JavaClass javaClass, ConditionEvents events) { | ||
boolean isFinal = javaClass.getModifiers().contains(JavaModifier.FINAL); | ||
String message = String.format("Class %s is not final", javaClass.getName()); | ||
events.add(new SimpleConditionEvent(javaClass, isFinal, message)); | ||
} | ||
}; | ||
} | ||
|
||
static ArchCondition<JavaClass> havePrivateConstructor() { | ||
return new ArchCondition<>("have a private constructor") { | ||
@Override | ||
public void check(JavaClass javaClass, ConditionEvents events) { | ||
boolean hasPrivateConstructor = javaClass.getConstructors().stream() | ||
.anyMatch(constructor -> constructor.getModifiers().contains(JavaModifier.PRIVATE)); | ||
String message = String.format("Class %s does not have a private constructor", | ||
javaClass.getName()); | ||
events.add(new SimpleConditionEvent(javaClass, hasPrivateConstructor, 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