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

Time:04-06

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.

CodePudding user response:

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.

CodePudding user response:

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);
    }
}
  • Related