简体   繁体   中英

PMD error: AccessorMethodGeneration when overriding some method in Lombok Builder

I have the following class that should be built with Lombok @Builder :

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder(toBuilder = true)
public class Foo {
    private String myName;
    private int myAge;
    
    public static class FooBuilder {

        // I'm overriding this method to add some validation logic
        public FooBuilder myName(String myName) {
            // some simple logic to validate "myName"

            this.myName= myName;
            return this;
        }
    }
}

I'd like to add some simple logic to validate myName field before setting it. But I don't want to do that for myAge . So myAge field's builder and the FooBuilder constructor will be generated by Lombok.

This makes PMD throw an error:

Rule:AccessorMethodGeneration Priority:3 Avoid autogenerated methods to access private fields and methods of inner / outer classes.

Should I suppress this error or is there a better way to achieve what I want?

The rules AccessorClassGeneration and AccessorMethodGeneration were created to help Android (,) development. when there was a limitation with the DEX format. These generated classes/methods counted towards the total classes/methods (64k) that were possible back then.

If you are not developing old Android apps, then you can probably remove these rules completely from your ruleset. Eg today there is Multidex possible which allows to use more than 64k methods in your app.

Also, since Java 11, the java compiler doesn't generate those accessor classes and methods anymore, see JEP 181: Nest-Based Access Control .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM