Open rala72 opened 1 year ago
The problem is that Optional::get
is no "lambda" for ArchUnit, but a "method reference". The issue you refer to addresses issues like the following, where the call was not detected from origin()
before, but some static method lambda$...
:
Runnable origin() {
return () -> {
target();
};
}
void target() {..}
So what you need to check for are calls and method references. Unfortunately, the API doesn't make this as convenient yet, but you can e.g. use the following custom code:
noClasses().should(dependOnMethodOptionalGet())
// ...
ArchCondition<JavaClass> dependOnMethodOptionalGet() {
return new ArchCondition<JavaClass>("depend on method Optional.get(..)") {
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
Stream.concat(javaClass.getMethodCallsFromSelf().stream(), javaClass.getMethodReferencesFromSelf().stream())
.filter(access -> access.getTargetOwner().isEquivalentTo(Optional.class) && access.getTarget().getName().equals("get"))
.forEach(access -> events.add(SimpleConditionEvent.satisfied(javaClass, access.getDescription())));
}
};
}
are there any plans to make this more convenient?
that allow you to use
@ArchTest
ArchRule optional_get = noClasses().should().accessTargetWhere(
target(owner(equivalentTo(Optional.class))).and(nameMatching("get"))
);
(but I understand that one would like to have that in a fluent API).
thank you for pointing this out
are there any plans to add it to the fluent API (in any way)?
otherwise I would close this issue as not planned
I'd keep it. I think it's a very good suggestion. If one only had more time... 🙈
I saw that #266 is resolved, but method calls described in
callMethod
are still not recognized in lambdas.I wrote a test that should ensure, that we don't use optional get:
I put somewhere in the code the following lines to test it:
The first line is detected correctly, but the second did not.