Skip to content

[java] ImmutableField: Do not flag fields in @Entity #3823

@numeralnathan

Description

@numeralnathan

Affects PMD Version: 6.43.0

Rule: ImmutableField

Description:

JPA classes do not allow for final fields. The primary key fields (i.e., annotated with @javax.persistence.Id) are sometimes implemented as immutable (i.e., no setters). ImmutableField rule flags that these fields could be final. Please fix the rule to not operate on fields annotated with @javax.persistence.Id.

Code Sample demonstrating the issue:

@Entity
@Table(name = "my_table")
public class MyTable implements Serializable
{
   private static final long serialVersionUID = 1L;

   @Id
   @Column(name = "directory", nullable = false)
   private long m_id;  // PMD Violation: ImmutableField

   // other fields

   /**
    * For JPA
    */
   public MyTable()
   {
      // nothing to do
   }

   public long getId()
   {
      return m_id;
   }
}

Expected outcome:

PMD reports a violation at line private long m_id; // PMD Violation: ImmutableField, but that's wrong. That's a false positive.

Running PMD through: Eclipse PMD Plugin

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions