SyntaxSamurai
SyntaxSamurai

Reputation: 1458

What does these FindBug messages show?

Not every description from from http://findbugs.sourceforge.net/bugDescriptions.html is clear to me. Sure, I can study the implementation but if somebody is more experienced then me, some explanation and examples would be great.

Upvotes: 0

Views: 6449

Answers (1)

Christian Semrau
Christian Semrau

Reputation: 9013

I can comment on some of the FindBugs descriptions you mention, based on my experience and testing, without consulting the FindBugs source code.

  • UI_INHERITANCE_UNSAFE_GETRESOURCE: If you use this.getClass().getResource(...) with a relative URI, that URI is resolved with respect to the class of this. When a subclass resides in a different package and you get the resource for a subclass, you end up looking at a different place (relative to the subclass). I know of examples where a class looks for a resource known to reside in the same package, by calling getResource() with a relative URI containing only the filename. If that class where to use this.getClass() instead of ClassName.class, and the actual instance is of a subclass, the resource would not be found.

  • BX_UNBOXED_AND_COERCED_FOR_TERNARY_OPERATOR: This talks about boxed number types, like Integer and Float, as opposed to primitive number types, like int and float. For primitives, your expectation is correct: In boolean ? int : float the int is coerced to float. But for wrapped numbers, something unexpected happens: In boolean ? Integer : Float, the Integer is unboxed and coerced to float. I would have expected the objects to be returned unchanged, like in boolean ? (Number)Integer : Float.

    boolean b = Boolean.TRUE;
    final Integer i = 123456789;
    final Float f = 1.0f;
    final Number x = b ? i : f;
    System.out.println("wrapped coerced: " + x); // 1.23456792E8
    final Number y = b ? (Number) i : f;
    System.out.println("wrapped uncoerced: " + y); // 123456789
    
  • GC_UNRELATED_TYPES: FindBugs knows about some collection methods that could not be generified for Java 1.5, like Collection.contains(Object). Here, the argument must be of type Object, because otherwise existing source code might break. But any object not of the collection's type is sure to be not contained, so asking for containment of an Integer in a String collection probably is a bug.

    Collection<String> coll = new ArrayList<String>();
    System.out.println(coll.contains(42));
    
  • HE_SIGNATURE_DECLARES_HASHING_OF_UNHASHABLE_CLASS: ?

  • NP_GUARANTEED_DEREF_ON_EXCEPTION_PATH: ?

  • SIO_SUPERFLUOUS_INSTANCEOF: ?

  • NN_NAKED_NOTIFY: ?

  • SP_SPIN_ON_FIELD: If you spin on a field that is not volatile, the JIT is free to optimize the code (by moving the read out of the loop) as long as the thread executing the loop produces the same result, without taking into account the existence and actions of other threads. That's why there is a volatile keyword. I know of no example where this actually lead to a bug.

  • STCAL_STATIC_CALENDAR_INSTANCE: ?

  • WL_USING_GETCLASS_RATHER_THAN_CLASS_LITERAL: In the FindBugs example, the method synchronizes on getClass() to access a static member of its class. A subclass will synchronize on the subclass, and so the superclass and the subclass can enter the synchronized block at the same time because they synchronize on different monitors, leading to a race condition.

  • EQ_UNUSUAL: ?

  • SF_SWITCH_FALLTHROUGH: This helps me sometimes, because I tend to miss the break. Interestingly, in a test case just performed, I got in addition to this FindBugs message the message SF_DEAD_STORE_DUE_TO_SWITCH_FALLTHROUGH.

  • TQ_EXPLICIT_UNKNOWN_SOURCE_VALUE_REACHES_ALWAYS_SINK: ?

Upvotes: 8

Related Questions