FindBugs: How to avoid "Unwritten public field" warning when using JPA meta model?

12,370

Solution 1

Here are some ideas:


1 - The setter could potentially be declared as private. There is a good chance that FindBugs doesn't check that the setter is called.

Solution 2

Possible duplicate of How to suppress FindBugs warnings for fields or local variables.

You can extract a method and apply method level @SuppressFBWarnings to that method.

Share:
12,370

Related videos on Youtube

bish
Author by

bish

Playing computer games since childhood I started as a software engineer trainee in the finanical sector after finishing school without had something to do with this anytime before. Since my training I'm addicted to it. After my training I did my B.Sc in computer science parallel to work. On work I mostly do JAVA EE software engeneering, quality assurance and are build manager for our team using Maven and Jenkins pipelines. I'm always looking forward to improve my skills and find ways to make our team work more effecient.

Updated on June 04, 2022

Comments

  • bish
    bish almost 2 years

    I've written quite a lot of DAO class and using the JPA criteria API and its meta model in them, like in this example:

    @Override
    public EntityA findByEntityB(EntityB entityB) {
      CriteriaBuilder builder = this.getCriteriaBuilder();
      CriteriaQuery<EntityA> criteriaQuery = builder.createQuery(EntityA.class);
      Root<EntityA> root = criteriaQuery.from(EntityA.class);
      criteriaQuery.select(root);
      criteriaQuery.where(builder.and(builder.equal(root.get(EntityA_.entityB), entityB)));
      return this.findByCriteriaQuery(criteriaQuery);
    }
    

    While running static code analysis, FindBugs throws the following warning:

    UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD, Priorität: Normal

    Unwritten public or protected field: EntityA_.entityB

    No writes were seen to this public/protected field. All reads of it will return the default value. Check for errors (should it have been initialized?), or remove it if it is useless.

    As I use the meta model classes in nearly all of my queries this warning is thrown very often.

    Is there any useful way to avoid these warnings? As we all know the meta model classes are just generated and their attributs are never written.

    I don't want to exclude the DAO classes from FindBugs sca as I want to check these to maybe find other possible bugs!

    • Stephen C
      Stephen C over 6 years
      You could add a setter for the field.
    • bish
      bish over 6 years
      @StephenC the metamodel classes are generated within each build and their attributs are static
    • Stephen C
      Stephen C over 6 years
      You can implement a getter for a static. This doesn't need to be "good style" :-)
  • bish
    bish over 6 years
    I want to avoid this warnings, not supress / ignore them.
  • bish
    bish over 6 years
    As it seems, that there is no programmatic way to avoid these errors, but only ignore them I add a filter in my findbugs configuration.
  • sofien.lpx
    sofien.lpx over 4 years
    I'm facing the same issue.I don't think it's a duplicate, it's linked to JPA meta model. I'm looking for a programmatic way to clean properly these warnings.