Skip to main content

Checking/fixing lines that "FindBugs" gave warning

4 replies [Last post]
hideya
Offline
Joined: 2003-07-22
Points: 0

Hello Folks,

I just tried "FindBugs" NetBeans plugin against lg3d-core, and got report of 1085 warnings in the code.

FindBugs is a Java tool that performs pattern matching against coding patterns that can cause bugs, and report such lines:
http://fnleisurehacker.fn.funpic.de/wordpress/?page_id=102

I'll check out some of the most skeptical ones. Checking a few out of 1085, all of them were just OK. So, I don't expect that many real bugs in the core. However, I thought it is worthwhile to look through. I thought that you folks who owns incubator projects might be interested in the tool.

hideya

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Juan González

>From what FindBugs reports, I personnally like the anti-literal-equals-NPE,
because of it's simplicity:

[code]
public boolean someMethod(String string)
{
return string.equals("FOO");
}[/code]

This is bad as a NullPointerException can be thrown

[code]
public boolean someMethod(String string)
{
return "FOO".equals(string);
}[/code]

This simple code reorganization avoids any NPE to be thrown.
Some "STYLE" hint that you'll like to share?

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@lg3d.dev.java.net
For additional commands, e-mail: interest-help@lg3d.dev.java.net

hideya
Offline
Joined: 2003-07-22
Points: 0

> [code]
> public boolean someMethod(String string)
> {
> return string.equals("FOO");
> }[/code]
>
> This is bad as a NullPointerException can be thrown
>
> [code]
> public boolean someMethod(String string)
> {
> return "FOO".equals(string);
> }[/code]
>
> This simple code reorganization avoids any NPE to be
> thrown.

This is a nice one!

> Some "STYLE" hint that you'll like to share?

I'll report them if I find any while checking the warnings :)

hideya

hideya
Offline
Joined: 2003-07-22
Points: 0

Hi Folks,

I ran FindBugs against LG3D core and checked many of the warnings I got. Actually, the tool could found several real bugs. FYI, here are something actually found in LG3D:
A very text book bug...[code] public class ConfigData {
public void ConfigData() {
...[/code]And something like this too:[code] if (target == null) {
new IllegalArgumentException("target cannot be null");
}[/code]The tool helped me identify these kind of bugs. Other useful stuff the tool reported includes:
- Possible null pointer dereference in method
- Nullcheck of value previously dereferenced
- Consider returning a zero length array rather than null
- Self assignment of local variable
It also reported a part of code where a stream (e.g. file) may not be closed correctly (e.g. because of an exception).

Possibly the most critical bugs I found was related to synchronization. The tool reported inconsistency regarding use of synchronization against variable accesses (e.g. "75% of access to this variable is synchronized, but 25% is not").

This is a great tool. I'd encourage you to run it if you are developing a Java app.

hideya

imran84
Offline
Joined: 2009-06-15
Points: 0

Hi,
I ran a small program on FindBugs. i am doing a string concatenation in a loop. I thought findbugs might throw a warning but it didnt.
Please help me to analyze it further, why it didn't throw.

Code. is

public class FindBugsTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String name="test";

for(int i=0; i < 1000; i++)
name = name + name;
}

}

Thanks
Imran