Why does IntelliJ IDEA show the warning "Inappropriate blocking method call" when one StringBuilder
appends another?
public Mono<String> doSomethingReactive() {
final StringBuilder sb1 = new StringBuilder("hello");
final StringBuilder sb2 = new StringBuilder("world");
sb1.append(sb2); // This append() causes warning "Inappropriate blocking method call"
sb1.append(sb2.toString()); // ... but this append() is OK :)
return Mono.just(sb1.toString());
}
CodePudding user response:
I suspect it is a bug in IntelliJ IDEA, unless someone comes with a reasonable explanation. See https://youtrack.jetbrains.com/issue/IDEA-282947
CodePudding user response:
I think it is blocking so perhaps that is why intelliJ is complaining. Since you are creating the Mono at the end of the method everything that comes before that is executed before or outside of the Mono flow. It defeats the purpose of a reactive flow. If I put the Mono at the top then no warning:
public Mono<String> doSomethingReactive() {
return Mono.just(new StringBuilder("hello"))
.map(sb1 -> {
StringBuilder sb2 = new StringBuilder("world");
sb1.append(sb2);
sb1.append(sb2.toString());
return sb1.toString();
});
}
In this case only the creation of the first StringBuilder is executed outside of the Mono flow.
Also, mind you, I think intelliJ may think that sb1.append(sb2)
is calling Object::toString() which may well be blocking when it is caching the class name in the VM whereas sb1.append(sb2.toString())
is called StringBuilder::toString() which may not do any blocking.