Menu

Abstract java rule -> start() and end() method

John
2016-05-20
2016-05-26
  • John

    John - 2016-05-20

    Hi all, i seem to have a problem with one of the code that was mentioned in the pmd guide which basically give a step by step instruction on creating a custom rule set. Below dictates the particular piece of code that i had a problem with which was obtained from https://pmd.github.io/pmd-5.3.6/customizing/howtowritearule.html

    package net.sourceforge.pmd.rules;
    
    import java.util.concurrent.atomic.AtomicLong;
    
    import net.sourceforge.pmd.RuleContext;
    import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
    import net.sourceforge.pmd.lang.java.ast.ASTExpression;
    
    public class CountRule extends AbstractJavaRule {
    
           private static final String COUNT = "count";
    
           @Override
           public void start(RuleContext ctx) {
                   ctx.setAttribute(COUNT, new AtomicLong());
                   super.start(ctx);
           }
    
           @Override
           public Object visit(ASTExpression node, Object data) {
                   // How many Expression nodes are there in all files parsed!  I must know!
                   RuleContext ctx = (RuleContext)data;
                   AtomicLong total = (AtomicLong)ctx.getAttribute(COUNT);
                   total.incrementAndGet();
                   return super.visit(node, data);
           }
    
           @Override
           public void end(RuleContext ctx) {
                   AtomicLong total = (AtomicLong)ctx.getAttribute(COUNT);
                   addViolation(ctx, null, new Object[] { total });
                   ctx.removeAttribute(COUNT);
                   super.end(ctx);
           }
    }
    

    As Mentioned in the guide, start() should be called before the visit method is called. Correspondingly, when all the ASTExpression nodes have been visited, the end method would be called. However when i ran this piece of code, this did not seem to be the case as the visit method was called first before the start method. This led to a null pointer exception at "total.incrementAndGet()" as the count variable was not set into the rule context. Does anyone have any solution to this problem?

     

    Last edit: John 2016-05-20
  • Andreas Dangel

    Andreas Dangel - 2016-05-20

    start() should be called before any visit methods - if this is not the case, then you've found a bug in PMD...
    Which version of PMD are you using? How to you execute your rule? (via command line, ant, maven, gradle, ...)

     
  • John

    John - 2016-05-21

    Hi i'm actually using pmd eclipse plugin 5.4.1.

     
  • Andreas Dangel

    Andreas Dangel - 2016-05-21
     
    • John

      John - 2016-05-21

      Yes i would assume so, i am switching over to the stand alone pmd instead. I am also thinking of looking at pmd maven

       
  • Andreas Dangel

    Andreas Dangel - 2016-05-26

    FYI - the mentioned bug in the eclipse plugin is fixed now.
    You can test the new version by installing the latest plugin version from
    https://sourceforge.net/projects/pmd/files/pmd-eclipse/update-site-latest/

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.