Why is this an issue?

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.

Exceptions

The rule will not raise on calls where the method under construction is abstract (eg: using the flag ClassFile.ACC_ABSTRACT).

How to fix it

Replace the invocation of withMethod with withMethodBody.

Code examples

Noncompliant code example

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_()
            );
        });
}

Compliant solution

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_()
        );
}

Resources

Documentation