简体   繁体   中英

PMD: Is there a way for rule writer to get the package name of the current node?

Well I have the Node, and I can get what class Name it in, but I cannot get package of this class.

Is there any way to achieve this?

Thanks!

BTW: I'm talking about PMD, so people who don't know what PMD be, don't answer about how a java object can get its package again, thanks.

use this.getClass().getName() to get packageName.className and use this.getClass().getSimpleName() to get only class name. If ou want full name use this.getClass().getCanonicalName() to get the full class name.

If you have java.lang.Class then you can call.getPackage().getName() on it to get its package name.

package stackoverflow;
public class Clazz {
    public static void main(String[] args) {
        String packageName = Clazz.class.getPackage().getName();
        System.out.println("packageName = " + packageName);
    }
}

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