我可能会为您提供(部分)解决方案。
创建一个ArchCondition
public static class NotBeUnreferenced extends ArchCondition<JavaClass> {
NotBeUnreferenced() {
super("is not referenced by any other compilation units");
}
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
Set<JavaAccess<?>> accessesFromOtherCompilationUnits = new HashSet<JavaAccess<?>>();
accessesFromOtherCompilationUnits.addAll(javaClass.getAccessesToSelf());
accessesFromOtherCompilationUnits.removeAll(javaClass.getAccessesFromSelf());
if (accessesFromOtherCompilationUnits.isEmpty() && javaClass.getDirectDependenciesToSelf().isEmpty()) {
String message = createCheckMessage(javaClass, "is unreferenced");
events.add(new SimpleConditionEvent(javaClass, false, message));
}
}
// taken from com.tngtech.archunit.lang.conditions.ArchConditions
public static <T extends HasDescription & HasSourceCodeLocation> String createCheckMessage(T object,
String message) {
return object.getDescription() + " " + message + " in " + object.getSourceCodeLocation();
}
}
使用ArchCondition
ArchCondition<JavaClass> notBeUnreferenced = new NotBeUnreferenced();
ArchRule rule = classes().should(notBeUnreferenced);
collectedRules.add(rule);
你可能想要
- 通过附加或条件 ( )排除具有某些注释的类(例如 , ,
@EJB
)@Dependent
@Stateless
rule = rule.or(ArchConditions.beAnnotatedWith(...))
- 从测试类中排除代码
- 排除仅显示常量的类和接口
已知限制
请注意,如果唯一的访问是
- 访问
.class
和其他反射访问
- 访问常量(即静态最终字段)