FindBugs: How to avoid "Unwritten public field" warning when using JPA meta model?
Solution 1
Here are some ideas:
- Modify the generator to add a (redundant) setter1.
- Implement a FindBugs filter (see http://findbugs.sourceforge.net/manual/filter.html) to exclude that specific bug in specific classes or packages. Or generally.
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.
Related videos on Youtube
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, 2022Comments
-
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 over 6 yearsYou could add a
setter
for the field. -
bish over 6 years@StephenC the metamodel classes are generated within each build and their attributs are
static
-
Stephen C over 6 yearsYou can implement a getter for a
static
. This doesn't need to be "good style" :-)
-
-
bish over 6 yearsI want to avoid this warnings, not supress / ignore them.
-
bish over 6 yearsAs 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 over 4 yearsI'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.