The ClassBuilder API provides multiple ways to declare a method and its body, including withMethod and
withMethodBody. While they can be used in very similar ways, withMethodBody reduces boilerplate code, lowers cognitive
complexity and improves maintainability.
The rule will not raise on calls where the method under construction is abstract (eg: using the flag ClassFile.ACC_ABSTRACT).
Replace the invocation of withMethod with withMethodBody.
ClassBuilder addMethod(ClassBuilder builder) {
return builder
.withMethod("foo", MTD_void, ACC_PUBLIC | ACC_STATIC, methodBuilder -> { // Noncompliant
methodBuilder.withCode(codeBuilder ->
codeBuilder.getstatic(ClassDesc.of("java.lang.System"), "out", ClassDesc.of("java.io.PrintStream"))
.ldc("Hello World")
.invokevirtual(ClassDesc.of("java.io.PrintStream"), "println", MTD_void)
.return_()
);
});
}
ClassBuilder addMethod(ClassBuilder builder) {
return builder
.withMethodBody("foo", MTD_void, ACC_PUBLIC | ACC_STATIC, codeBuilder ->
codeBuilder.getstatic(ClassDesc.of("java.lang.System"), "out", ClassDesc.of("java.io.PrintStream"))
.ldc("Hello World")
.invokevirtual(ClassDesc.of("java.io.PrintStream"), "println", MTD_void)
.return_()
);
}