How to get Class Info in Java Reflection? Packages and Class Modifier.

This is continuing article of my previous post. In this article we will see how can we retrieve Class Related Information using Java Reflection .We will see only Package information (where the class belong) and Class’s access modifier (public/private/default/protected).

Spatial Note : I will make a separate class reflector utility class where we input a target class in its constructor and we will retrieve information using separate method. In this way ,we can isolate our needs. Please see this before start
How to get Package Information?
   1: public String getPackageName(){

   2:         return "Package Name : "+myClass.getPackage().getName()+", Version :"+myClass.getPackage().getSpecificationVersion() ;

   3:     }
How to get Access Modifier Name ?
   1: public String getModifierName() {

   2:         return "Modfiers : "+Modifier.toString(myClass.getModifiers());

   3:     }
JVM assumes the static, abstract, final as modifiers and with reflection we get int value for those. We use Modifier class to translate the int into proper modifier string.

In the same way we will see more information in my next posts.


No comments:

Post a Comment