views:

68

answers:

1

Hi all, I want my findbugs report not show the following error:

DM_NUMBER_CTOR: Method invokes inefficient Number constructor; use static valueOf instead

The problem is that this happens in groovy-generated code files, so I can't control the source code - that is why I want to exclude it and add it to my exclude filter.

I do not want to add explicitly class (since I make API that many tools will use, I want my filter to be generic). I would not like to completely remove this bug from the report by type, I would really like to only exclude this bug from appearing if it happenned in "static initializer" methods. Any idea? I tried the filter below but no luck, maybe somebody has better idea?

<Match>
    <Method name="~.*static initializer.*" />
    <Bug pattern="DM_NUMBER_CTOR" />
</Match>

Here is the "stacktrace" of FindBugs in that case:

In class net.milanaleksic.cuc.tools.sound.SoundPlayerTool In method net.milanaleksic.cuc.tools.sound.SoundPlayerTool.() Called method new Long(long) Should call Long.valueOf(long) instead In SoundPlayerTool.groovy

+1  A: 

I would guess the static initializer code would be reported as taking place in method called <clinit>. Could you try setting a <Method name='<clinit>'/> filter? (I am not sure about the XML escaping). Totally untested, just some random thoughts.

My clue was this part of some FindBugs internal tests:

 String methodName = m.getMethodName();
 ...
 if (...  methodName.equals("<clinit>")) ) ...

I am not sure but I think the same method name (<clinit>) is mentioned if the bugs actually happen...

Grzegorz Oledzki
Thanks! I confirm that following filter really does what I wanted: <Match> <Method name="<clinit>" /> <Bug pattern="DM_NUMBER_CTOR" /> </Match>
MilanAleksic
Grzegorz Oledzki
<clinit> is the internal class file format name for a static block, as <init> is the internal name for a constructor.
MeBigFatGuy