-
Notifications
You must be signed in to change notification settings - Fork 305
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
How to check for parameters having a certain value #721
Comments
There's two bad news for you: ArchUnit does
To at least offer some good news, you could probably use this to catch usages of static ArchCondition<JavaClass> useFlatMapStreamDistinct() {
return new ArchCondition<JavaClass>("call .flatMap and Stream.distinct() in same line") {
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
List<JavaMethodCall> streamDistinctCalls = getMethodCallsTo(javaClass, Stream.class, "distinct");
for (JavaMethodCall flatMapCall : getMethodCallsTo(javaClass, Stream.class, "flatMap")) {
for (JavaMethodCall streamDistinctCall : streamDistinctCalls) {
SourceCodeLocation sourceCodeLocation = flatMapCall.getSourceCodeLocation();
boolean sameLine = sourceCodeLocation.equals(streamDistinctCall.getSourceCodeLocation());
if (sameLine) { // ← Note that this check could be more liberal!
events.add(SimpleConditionEvent.satisfied(flatMapCall, String.format(
"%s calls .flatMap and Stream.distinct() in the same line in %s",
flatMapCall.getOrigin().getDescription(), sourceCodeLocation)
));
}
}
}
}
List<JavaMethodCall> getMethodCallsTo(JavaClass javaClass, Class<?> methodOwner, String methodName) {
return javaClass.getMethodCallsFromSelf().stream()
.filter(call -> call.getTargetOwner().isEquivalentTo(methodOwner)
&& call.getTarget().getName().equals(methodName))
.collect(toList());
}
};
} It can unfortunately have false positives as well as false negatives (😞), but might be effectively okay if your code style requires / prevents linebreaks accordingly. And once ArchUnit understands method references, it might actually be good enough. |
Hello Manfred, thank you for your quick and informativ reply and also for your code. However, I don't think that solution is really useable to us. There are multiple people involved in the project, telling them they have to use the non-reference style wouldn't really work out. For now we will look out for the problem in our reviews. Still thank you very much for your help, I appreciate it. :) |
Hello,
currently I face that issue that I want to forbid the usage of
Stream#flatMap
with the parameter beingStream::distinct
since it lead to a few bugs. Is this currently supported or possible somehow?I am aware that you cannot check if the given function is equal to another function since the equals method of
Object
is used which checks for the identity.Is it perhaps possible to get access to the line of code so I could work on a String to check against?
Here my current code in case it helps somehow:
The text was updated successfully, but these errors were encountered: