osdir.com
mailing list archive

Subject: RE: False positives - msg#00001

List: java.findbugs.general

Date: Prev Next Index Thread: Prev Next Index
Robert,

You run FindBugs on your JUnit code? I hadn't considered doing that
and would be interested to hear what kind of bugs that catches?

At the very least I think you will need to run FindBugs twice, using a
different set of checks on your production code than on your test code.
I imagine you will need to exclude quite a few of the checks for test
code.

Take a look at FindBug's exclusions filter functionality:

http://findbugs.sourceforge.net/manual/filter.html

Hope that helps,

Chris.

-----Original Message-----
From: findbugs-discuss-bounces@xxxxxxxxxx
[mailto:findbugs-discuss-bounces@xxxxxxxxxx] On Behalf Of Robert Wenner
Sent: 07 November 2006 21:36
To: findbugs-discuss@xxxxxxxxxxxxxxxxxx
Subject: [FB-Discuss] False positives

Hi all,


I started using Findbugs recently. It has produced some helpful
warnings.
I also got a lot of what I consider false positives like the ones below.


CI: Class Message is final but declares protected field _cal
This is true, but on purpose. Java allows derived classes AND classes in

the same package to access protected members.
I'd like to not see this warning if there is in fact code in the current

scope (project) that does access the protected field.


DE: PMTATransportTest.testGetMsgDataNoData() might ignore
java.io.IOException
This is the code that triggers this warning:
public void testSomething() {
try {
doSomethingBad();
}
catch (IOException expected) { /* Ok. */ }
}
This is a perfectly fine JUnit 3.8 test case and should go without
complaint.


Dm: AccounterTest.testNativeObjectFreed() forces garbage collection;
extremely dubious except in benchmarking code
This is a test that makes sure the JNI native part gets cleaned up on
garbage collection. This code itself is too complex to cite here, but
the
concept is having mock objects on both sides of the JNI layer and the
test will query the mock whether the native code for releasing the
resources was called. I agree this is a little brittle, but in practice
it works fine on Windows, Solaris, and Linux with JDKs 1.4 and 5.


DP: MessageCompatibilityTest.test7BitIsDefaultEncoding() invokes
java.lang.reflect.Field.setAccessible(boolean), which should be invoked
from within a doPrivileged block
This is a test trying to access a private field in the class under test.
I
agree this is not nice, but I cannot change the code under test (it's a
compatibility test to ensure the old code behaves as the new code).
This way is described in the JUnit FAQ.


IJU: TestCase AccounterTest$1 has no tests
I got this one for bringing mock objects in my test code like this:
public void testFoo() {
doSomething(new FooBar() {
public void overrideSomeMethod() {
// ...
}
});
// ...
}
Note that FooBar is not even derived from TestCase.


Se: NativeObject._nativeObject is transient but NativeObject isn't
Serializable
I get this for an abstract class where derived classes may well be
Serializable, however, if they are, I want to be sure this particular
field is not serialized (it holds a pointer to its JNI part).


UrF: Unread field: NativeObject.finalizerGuardian
I get this for a finalizer guardian as described in Effective Java item
6:
class Foo {
public void free() {
// free some JNI native resources
}

private final Object finalizerGuardian = new Object() {
protected void finalize() {
free();
}
};
}
This ensures that even if derived classes override finalize() and don't
call their base's finalize() it will still run when the finalizer
guardian member is garbage collected.


UwF: Field not initialized in constructor: PMTATransportTest.msg
Here Findbugs does not see that this field is in fact initialized in the

setUp() method.


Generally, I think Findbugs should distinguish between production code
and
test code. Many things are fine in tests that are problems in
production.

Is there a way to disable specific checks only in some hand-picked
places?
Like a special comment or an annotation?
A comment probably won't work since Findbugs works on class files,
right?
Any other way for Java 1.4 compatibility?


Robert
_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@xxxxxxxxxxxxxxxxxx
http://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss


CONFIDENTIALITY & PRIVILEGE NOTICE

This e-mail is confidential to its intended recipient. It may also be
privileged. Neither the confidentiality nor any privilege attaching to this
e-mail is waived lost or destroyed by reason that it has been mistakenly
transmitted to a person or entity other than its intended recipient. If you are
not the intended recipient please notify us immediately by telephone or fax at
the numbers provided above or e-mail by Reply To Author and return the printed
e-mail to us by post at our expense. We believe, but do not warrant, that this
e-mail and any attachments are virus-free, but you should check. We may monitor
traffic data of both business and personal e-mails. We are not liable for any
opinions expressed by the sender where this is a non-business e-mail. If you do
not receive all the message, or if you have difficulty with the transmission,
please telephone us immediately.



Was this page helpful?
Yes No
Thread at a glance:

Previous Message by Date: click to view message preview

False positives

Hi all, I started using Findbugs recently. It has produced some helpful warnings. I also got a lot of what I consider false positives like the ones below. CI: Class Message is final but declares protected field _cal This is true, but on purpose. Java allows derived classes AND classes in the same package to access protected members. I'd like to not see this warning if there is in fact code in the current scope (project) that does access the protected field. DE: PMTATransportTest.testGetMsgDataNoData() might ignore java.io.IOException This is the code that triggers this warning: public void testSomething() { try { doSomethingBad(); } catch (IOException expected) { /* Ok. */ } } This is a perfectly fine JUnit 3.8 test case and should go without complaint. Dm: AccounterTest.testNativeObjectFreed() forces garbage collection; extremely dubious except in benchmarking code This is a test that makes sure the JNI native part gets cleaned up on garbage collection. This code itself is too complex to cite here, but the concept is having mock objects on both sides of the JNI layer and the test will query the mock whether the native code for releasing the resources was called. I agree this is a little brittle, but in practice it works fine on Windows, Solaris, and Linux with JDKs 1.4 and 5. DP: MessageCompatibilityTest.test7BitIsDefaultEncoding() invokes java.lang.reflect.Field.setAccessible(boolean), which should be invoked from within a doPrivileged block This is a test trying to access a private field in the class under test. I agree this is not nice, but I cannot change the code under test (it's a compatibility test to ensure the old code behaves as the new code). This way is described in the JUnit FAQ. IJU: TestCase AccounterTest$1 has no tests I got this one for bringing mock objects in my test code like this: public void testFoo() { doSomething(new FooBar() { public void overrideSomeMethod() { // ... } }); // ... } Note that FooBar is not even derived from TestCase. Se: NativeObject._nativeObject is transient but NativeObject isn't Serializable I get this for an abstract class where derived classes may well be Serializable, however, if they are, I want to be sure this particular field is not serialized (it holds a pointer to its JNI part). UrF: Unread field: NativeObject.finalizerGuardian I get this for a finalizer guardian as described in Effective Java item 6: class Foo { public void free() { // free some JNI native resources } private final Object finalizerGuardian = new Object() { protected void finalize() { free(); } }; } This ensures that even if derived classes override finalize() and don't call their base's finalize() it will still run when the finalizer guardian member is garbage collected. UwF: Field not initialized in constructor: PMTATransportTest.msg Here Findbugs does not see that this field is in fact initialized in the setUp() method. Generally, I think Findbugs should distinguish between production code and test code. Many things are fine in tests that are problems in production. Is there a way to disable specific checks only in some hand-picked places? Like a special comment or an annotation? A comment probably won't work since Findbugs works on class files, right? Any other way for Java 1.4 compatibility? Robert

Next Message by Date: click to view message preview

Re: False positives

On 11/7/06, Robert Wenner <robert.wenner@xxxxxx> wrote: CI: Class Message is final but declares protected field _calThis is true, but on purpose. Java allows derived classes AND classes inthe same package to access protected members.I'd like to not see this warning if there is in fact code in the current scope (project) that does access the protected field.Why isn't it defined with default (package) protection? Making it protected does not make sense if the class is final.

Previous Message by Thread: click to view message preview

Re: False positives

On Nov 7, 2006, at 4:36 PM, Robert Wenner wrote: Hi all, I started using Findbugs recently. It has produced some helpful warnings. I also got a lot of what I consider false positives like the ones below. CI: Class Message is final but declares protected field _cal This is true, but on purpose. Java allows derived classes AND classes in the same package to access protected members. I'd like to not see this warning if there is in fact code in the current scope (project) that does access the protected field. There are 4 access levels. From strictest to most relaxed: * private * package protected (default) * protected * public If you want to allow other classes in the same package to access _cal, you don't need to declare it as private. Just don't put any access qualifier on it, which makes it package protected. DE: PMTATransportTest.testGetMsgDataNoData() might ignore java.io.IOException This is the code that triggers this warning: public void testSomething() { try { doSomethingBad(); } catch (IOException expected) { /* Ok. */ } } This is a perfectly fine JUnit 3.8 test case and should go without complaint. Yeah, since FindBugs doesn't look at source code, we won't see the comment. Put in assert true and FindBugs will stop complaining. Dm: AccounterTest.testNativeObjectFreed() forces garbage collection; extremely dubious except in benchmarking code This is a test that makes sure the JNI native part gets cleaned up on garbage collection. This code itself is too complex to cite here, but the concept is having mock objects on both sides of the JNI layer and the test will query the mock whether the native code for releasing the resources was called. I agree this is a little brittle, but in practice it works fine on Windows, Solaris, and Linux with JDKs 1.4 and 5. OK, I put in something to supress this warning in test methods. DP: MessageCompatibilityTest.test7BitIsDefaultEncoding() invokes java.lang.reflect.Field.setAccessible(boolean), which should be invoked from within a doPrivileged block This is a test trying to access a private field in the class under test. I agree this is not nice, but I cannot change the code under test (it's a compatibility test to ensure the old code behaves as the new code). This way is described in the JUnit FAQ. Suppressed this in test methods. IJU: TestCase AccounterTest$1 has no tests I got this one for bringing mock objects in my test code like this: public void testFoo() { doSomething(new FooBar() { public void overrideSomeMethod() { // ... } }); // ... } Note that FooBar is not even derived from TestCase. I don't get that warning: public class InnerClassInTestCase extends TestCase { public void testFoo() { doSomething(new Object() { public void overrideSomeMethod() { // ... } }); // ... } private void doSomething(Object object) { // TODO Auto-generated method stub } } Se: NativeObject._nativeObject is transient but NativeObject isn't Serializable I get this for an abstract class where derived classes may well be Serializable, however, if they are, I want to be sure this particular field is not serialized (it holds a pointer to its JNI part). Suppressed this in test methods UrF: Unread field: NativeObject.finalizerGuardian I get this for a finalizer guardian as described in Effective Java item 6: class Foo { public void free() { // free some JNI native resources } private final Object finalizerGuardian = new Object() { protected void finalize() { free(); } }; } This ensures that even if derived classes override finalize() and don't call their base's finalize() it will still run when the finalizer guardian member is garbage collected. Fixed. UwF: Field not initialized in constructor: PMTATransportTest.msg Here Findbugs does not see that this field is in fact initialized in the setUp() method. There is some stuff to come out in 1.1.2 that should catch this in some situations. However, the UwF: Field not initialized in constructor is a low priority warning and you should only look at low priority warnings if you are willing to deal with a fair number of false positives. Generally, I think Findbugs should distinguish between production code and test code. Many things are fine in tests that are problems in production. Is there a way to disable specific checks only in some hand-picked places? Like a special comment or an annotation? A comment probably won't work since Findbugs works on class files, right? Any other way for Java 1.4 compatibility?

Next Message by Thread: click to view message preview

Re: False positives

On Wednesday 08 November 2006 03:10, Chris Nappin wrote: > You run FindBugs on your JUnit code? I run it on the whole project in Eclipse. The tests are part of my project. I also use the same compiler settings for test and production code, so it seemed natural to use Findbugs on both, too. Shouldn't Findbugs be used on tests? After all it has the InvalidJunitTest detector. > I hadn't considered doing that > and would be interested to hear what kind of bugs that catches? It caught some unnecessary code, where mock methods just called super.theMethod(...) > Take a look at FindBug's exclusions filter functionality: Hm. Not yet for Eclipse. Thanks though. Maybe I'll use a Makefile for that. Robert
Sign up for updates to this mailing list. email:
Loading Comments...
Home | News | Patents | Sitemap | FAQ | advertise

Advertising by