问题
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!
回答1:
Here are some ideas:
- Modify the generator to add a (redundant) setter.
- Implement a FindBugs filter (see http://findbugs.sourceforge.net/manual/filter.html) to exclude that specific bug in specific classes or packages. Or generally.
回答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.
回答3:
You can add the Setter of these fields, then it's able to remove the FindBugs error message
来源:https://stackoverflow.com/questions/46178630/findbugs-how-to-avoid-unwritten-public-field-warning-when-using-jpa-meta-mode