Findbugs and comparison

I recently started using the findbugs static analysis tool in the java assembly I was doing. The first report returned with many warnings of high priority. Being an obsessive person, I was ready to knock everything out. However, I have to miss something. I get most warnings when comparing things. For example, the following code:

public void setSpacesPerLevel(int value) { if( value >= 0) { spacesPerLevel = value; } else { spacesPerLevel = 0; } } 

throws a high priority warning in an if statement that reads.

File: Indenter.java, Line: 60, Type: BIT_AND_ZZ, Priority: High, Category: CORRECTNESS Check to see if ((...) & 0) == 0 in sample.Indenter.setSpacesPerLevel (integer)

I am comparing int with int, it seems like a normal thing. I get quite a lot of such errors with similar simple comparisons.

I have many other high priority warnings that appear to be simple blocks of code. Am I missing something? I understand that static analysis can produce false positives, but the errors that I see seem too trivial for the case to be false.

I have scratches too.

  for(int spaces = 0;spaces < spacesPerLevel;spaces++) { result = result.concat(" "); } 

What gives a warning about downstream errors:

 File: Indenter.java, Line: 160, Type: IL_INFINITE_LOOP, Priority: High, Category: CORRECTNESS There is an apparent infinite loop in sample.Indenter.indent() This loop doesn't seem to have a way to terminate (other than by perhaps throwing an exception). 

Any ideas?

So basically I have several files and 50-60 warnings with high priority similar to the above. I am using findbugs 1.3.9 and calling it from the findbugs ant task

UPDATE: I have this assembly performed by the hudson server, and the code has been integrated by Clover to cover the code. When I turned it off, all of my high priority warnings disappeared. Now that makes sense. Thanks for the feedback.

+7
java static-analysis findbugs
source share
3 answers

UPDATE: I have this assembly executed by the hudson server and it had code managed by Clover to cover the code. When I turned it off, all of my high priority warnings disappeared. Now that makes sense. Thanks for the feedback.

+7
source share

Note:

 for(int spaces = 0;spaces < spacesPerLevel;spaces++) { result = result.concat(" "); } 

If result is java.lang.String , this may be inefficient, as you follow these steps for each space character:

  • create a new char[] to save the result of concatenation
  • create a new instance of java.lang.String that is wrapped around an array of characters

If you do this repeatedly, especially if the result already long, it takes a long time.

If performance (both time and memory) is important for this method, you should use StringBuilder (insecure stream) or StringBuffer (thread-safe).

+3
source share

Do you run Findbugs through the Eclipse, ant or gui plugin? Is it possible that the code has not been recompiled since it was run (before the changes were made)?

if setSpacesPerLevel is not too long, post the output

javap -v TheClassThatContainssetSpacerPerLevel

As for the second error, you will need to show the whole cycle before you could tell if that was the problem.

+1
source share

All Articles