There is a PMD rule that checks for mutators (getters and setters) on Java Beans: BeanMembersShouldSerialize. I have a lot of these violations in my code. I’d rather understand the issue than just turn it off, but I’m struggling with what it means.
What I learned is: being Serializable does not make a class a Java Bean.
But PMD thinks my object is a bean. Why?
After reading the source, I determined that there are only a few criteria that exclude classes:
- if it’s an interface
- if the variable is transient
- if the variable is static
Otherwise, PMD assumes it’s a bean and tries to match variables with methods. If there isn’t both a setter and getter, it creates a violation.
My Serializable classes are not Beans and do not need getters and setters. So I think I can safely turn off this warning.
- Only check Java Beans (or at least Serializable classes)
- Re-word the rule to something like “Java Beans should have mutators (getters and setters)”