Menu

#330 Use annotations to suppress warnings

closed
None
5
2012-10-07
2005-06-14
No

i.e., use java.lang.SuppressWarning.

Discussion

  • Tom Copeland

    Tom Copeland - 2005-09-29

    Logged In: YES
    user_id=5159

    First step would be to make current //NOPMD functionality
    available via an annotation. But can an annotation apply to
    just one line? It can apply to classes, methods, and local
    variables...

    tom

     
  • Tom Copeland

    Tom Copeland - 2005-09-30

    Logged In: YES
    user_id=5159

    Here's what @SuppressWarnings looks like:

    @SuppressWarnings({"rule1","rule2"})

    Here's the Javadoc:

    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/SuppressWarnings.html

    Hm, so it can target any one of {TYPE, FIELD, METHOD,
    PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}. Cool, well, that
    should about cover it.

    Hm, now how best to integrate it. Intercept the warnings as
    they are added, like NOPMD does? Or wait until the end and
    postprocess the Report object? Hm.

    Tom

     
  • Tom Copeland

    Tom Copeland - 2005-09-30

    Logged In: YES
    user_id=5159

    More data; this code:

    @SuppressWarnings({"hi","hey"})
    public class Foo {
    }

    produces this AST:

    CompilationUnit
    TypeDeclaration
    Annotation
    SingleMemberAnnotation
    Name:SuppressWarnings
    MemberValue
    MemberValueArrayInitializer
    MemberValue
    PrimaryExpression
    PrimaryPrefix
    Literal:"hi"
    MemberValue
    PrimaryExpression
    PrimaryPrefix
    Literal:"hey"
    ClassOrInterfaceDeclaration(Foo)(class)
    ClassOrInterfaceBody

     
  • Tom Copeland

    Tom Copeland - 2005-10-03

    Logged In: YES
    user_id=5159

    OK, this is started, you can now suppress warnings for an
    entire class like this:

    @SuppressWarnings()
    public class Foo {
    // lots of code, no PMD reports!
    }

    And the old way - //NOPMD - still works as well.

    Can you give it a whirl and see if you notice any problems?

    Thanks,

    Tom

     
  • Tom Copeland

    Tom Copeland - 2005-10-03

    Logged In: YES
    user_id=5159

    Oops, I should mention, there's an updated pmd-3.3.jar file
    here:

    http://infoether/com/~tom/pmd-3.3.jar

    that contains this feature.

    tom

     
  • Tom Copeland

    Tom Copeland - 2005-10-03

    Logged In: YES
    user_id=5159

    Ack, make that:

    http://infoether.com/~tom/pmd-3.3.jar

    Tom

     
  • Wim Deblauwe

    Wim Deblauwe - 2005-10-05

    Logged In: YES
    user_id=172636

    Very nice feature Tom! But to avoid missing things you want
    to see, you should really specify the exact rules that
    should be ignored. Currently NOPMD is just too broad. You
    can have a perfectly good reason for putting NOPMD for 1
    rule, but later another rule can be violated in that piece
    of code, and you will not have noticed it.

    keep up the good work!

     
  • Tom Copeland

    Tom Copeland - 2005-10-05

    Logged In: YES
    user_id=5159

    you should really specify the exact rules
    that should be ignored

    Definitely, right on, I agree. We want more granularity,
    like this:

    @SuppressWarnings({"rule1","rule2"})

    We'll keep this RFE open until that's implemented...

    Yours,

    Tom

     
  • Wim Deblauwe

    Wim Deblauwe - 2005-10-06

    Logged In: YES
    user_id=172636

    I was about to write "don't forget to define constants/enum
    for those rules to avoid typo's" but that would lead to
    PMD-specific imports in your code and I'm not sure that is
    appropriate. Maybe something to be discussed.

     
  • Tom Copeland

    Tom Copeland - 2005-10-06

    Logged In: YES
    user_id=5159

    Yup, some folks have suggested variations on that here:

    http://www.javalobby.org/java/forums/t49508.html

    I'm thinking something like "pmd.UnusedMethod" might be the
    way to go for now...

    Yours,

    Tom

     
  • Tom Copeland

    Tom Copeland - 2005-10-06

    Logged In: YES
    user_id=5159

    Now the XML report for suppressions looks like this:
    $ ./pmd.sh Foo.java xml unusedcode -targetjdk 1.5

    <pmd version="3.3" timestamp="2005-10-06T09:47:50.090" elapsedtime="0s">
    <suppressedviolation filename="/home/tom/pmd/pmd/bin/Foo.java" suppressiontype="annotation" msg="Avoid unused local variables such as 'x'">
    </suppressedviolation></pmd>

    So it contains the method of suppression...

    tom

     
  • Tom Copeland

    Tom Copeland - 2005-10-18

    Logged In: YES
    user_id=5159

    Implemented in CVS, and you can download a new pmd-3.3.jar
    file here:

    http://infoether.com/~tom/pmd-3.3.jar

    that contains this feature. Thanks to everyone for the
    feedback... bug reports, comments, questions welcome!

    Yours,

    Tom

     

Log in to post a comment.

MongoDB Logo MongoDB