-
Notifications
You must be signed in to change notification settings - Fork 306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fluency for notAccessed #254
Comments
Even though not fluent, you can easily use a custom condition: @ArchTest
public static final ArchRule implOnlyUsedByInjection = classes()
.that().resideInAPackage(IMPL)
.should(notBeAccessedAtAll());
static ArchCondition<JavaClass> notBeAccessedAtAll() {
return new ArchCondition<JavaClass>("not be accessed at all") {
@Override
public void check(JavaClass clazz, ConditionEvents events) {
Set<JavaAccess<?>> accessesToSelf = clazz.getAccessesToSelf();
StringBuilder message = new StringBuilder(clazz.getName()).append(" is accessed ").append(accessesToSelf.size()).append(" times");
if (!accessesToSelf.isEmpty()) {
message.append(":");
}
for (JavaAccess<?> javaAccess : accessesToSelf) {
message.append("\n").append(javaAccess.getDescription());
}
events.add(new SimpleConditionEvent(clazz, accessesToSelf.isEmpty(), message.toString()));
}
};
} Violation Message:
|
Thanks @hankem that is a very tidy example |
Just to mention, if it does what you want, and you only don't like the message, you could also just overwrite that:
That's also a documentation for anyone reading your rule, what was the intention. |
Modified code to allow class to access itself internally |
Thanks for the help. Love ArchUnit. Looking forward to 1.0 so that our large business friends can use it without fighting with the corporate policies. |
Yes, we should really release 1.0 |
PS: If your question is solved, do you want to close the issue? |
Using the following rule I can ensure that no impl class gets accessed directly (they will instead be injected via spring @component injection against their interface). However it doesn't read as well as it could, and the violation message doesn't read well either. Suggest adding .notBeAccessed()
@archtest
public static final ArchRule implOnlyUsedByInjection = classes()
.that().resideInAPackage(IMPL)
.should().onlyBeAccessed().byAnyPackage();
Violation Message:
Architecture Violation [Priority: MEDIUM] - Rule 'classes that reside in a package 'com.eyecon.xyz.impl..' should only be accessed by any package ['']' was violated (51 times):
The text was updated successfully, but these errors were encountered: