Discussion:
Nonsensical call to isEmptyOrNull()
David Harkness
2014-07-11 22:10:42 UTC
Permalink
I have some objects that are deserialized from XML REST responses, and this
is tripping up FindBugs because it appears a String field is a constant
(it's package-private with no setter). Here's the relevant snippet:

@XmlRootElement(name = "response")
public XmlReponse {
String id = "";

@Nonnull

public String getId() {

if (Strings.isNullOrEmpty(id)) { // D'oh!

throw new InvalidResponseException("Received response without
ID");

}

return id;

}

}


Now I know I can use @SuppressFBWarnings on getId() for this, but I'm
wondering if there's a way to tell FindBugs that the id field can be
modified externally even though the code makes it look impossible. I can
change the field to public so FB assumes this, but is there another way?

Thanks,
David
Andrey Loskutov
2014-07-11 22:15:12 UTC
Permalink
You can provide get/set methods or make the field protected or use filter rules for either class or pattern etc.
Post by David Harkness
I have some objects that are deserialized from XML REST responses, and this
is tripping up FindBugs because it appears a String field is a constant
@XmlRootElement(name = "response")
public XmlReponse {
String id = "";
@Nonnull
public String getId() {
if (Strings.isNullOrEmpty(id)) { // D'oh!
throw new InvalidResponseException("Received response without
ID");
}
return id;
}
}
wondering if there's a way to tell FindBugs that the id field can be
modified externally even though the code makes it look impossible. I can
change the field to public so FB assumes this, but is there another way?
--
Kind regards,
Andrey Loskutov

http://google.com/+AndreyLoskutov
David Harkness
2014-07-12 00:09:40 UTC
Permalink
Post by Andrey Loskutov
You can provide get/set methods or make the field protected or use filter
rules for either class or pattern etc.
For this I used the annotation since I don't want to miss out on potential
bugs elsewhere.

I wonder if FindBugs should treat a field annotated with @XmlElement (and
other serialization annotations) as public, knowing it is likely to be
read/written via reflection. I've created a feature request for this. [1]

Thanks,
David

[1] https://sourceforge.net/p/findbugs/feature-requests/306/

Loading...