Serialization, Beans and PMD

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. And, to be Serializeable, you don’t need mutators.

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:

  1. if it’s an interface
  2. if the variable is transient
  3. 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.


Edit: PMD should change the rule:

  1. Only check Java Beans (or at least Serializable classes)
  2. Re-word the rule to something like “Java Beans should have mutators (getters and setters)”

Leave a Reply

Your email address will not be published. Required fields are marked *