简体   繁体   中英

Custom PMD rule not being highlighted by PMD Eclipse plugin

I'm trying to create a custom rule that flags up the use of return statements in void methods, for example:

public class MyClass
{
    public void myMethod(Object someObj)
    {
        if (someObj == null)
        {
            return;
        }
        ...
    }
}

I have created a custom XPath rule with the following XPath:

//MethodDeclaration[not(ResultType/Type) and (count(//ReturnStatement) > 0)]

Which returns the ASTMethodDeclaration element in the RuleDesigner, however when I import this rule into Eclipse and run PMD against the code, it doesn't flag a method that I expected it to, ie one that matches the above description (void method containing return).

Anyone got any ideas what the problem could be here? Is there something wrong with my XPath or could it be something else?

I'm using v3.2.6 of the plugin, which contains pmd14-4.2.5.jar.

Thanks, Chris

Is that the exact XPath you used? It's not well formed. (It's missing a close paren.) I would recommend writing a test harness to test the xpath independent of PMD. You can generate the AST for a class in Eclipse (or I think standalone) so you have the XML to run it against.

Ok, solved the problem. The XPath logic was wrong, which I realised after running the Ant task and it returned >2000 violations of this rule! Mostly on void methods with no return statement at all.

This XPath works, is simpler than the original one and highlights the return statement itself, rather than the method, which is probably better than my original intention of highlighting the method:

//MethodDeclaration[not(ResultType/Type)]//ReturnStatement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM