paddes
paddes

Reputation: 1

How do I create a custom PMD rule to check if a specific method is called?

I am completely new with the PMD. I am trying to write some basic rules to get used to it. I have this very basic code:

public class Main {
    static void print() {
        System.out.println("I am not suppose to be here!");
    }

    public static void main(String[] args) {
        print();
    }
}

I want to get a warning if print() method is called (so, I need to get in this case).

I know the structure is like that:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="customRuleSet">

    <description>
        This is a custom ruleset that focuses on not calling a method
    </description>

    <rule name="dontCallThisMethod"
          language="java"
          message="This method is not supposed to be here!"
          class="net.sourceforge.pmd.lang.rule.XPathRule">
        <description>
            This is only for learning purposes. So, this custom rule's
            goal is giving warning when a specific method is called while
            it is not supposed to be called.
        </description>
        <priority>3</priority>
        <properties>
            <property name="xpath">
                <value>
<![CDATA[

]]>
                </value>
            </property>
        </properties>
    </rule>
</ruleset>

As I understand so far the rule determining part is this:

<![CDATA[

]]>

I did not understand the logic behind it. If you help me I would be very happy. Also if you have suggestions for me to learn it faster like where should I look (I am also not familar with XPath and so on) I will appreciate because I am not doing perfect with the documentation in PMD page I guess... So, thank you!

Upvotes: 0

Views: 71

Answers (0)

Related Questions