How to get Class Info in Java Reflection? Class Name and Signer Names

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 Class Name (all kinds of) , and the Signer Names(by which the class is signed while compiling)

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 Class Names?
Usually, when we talk about a class name, we mean its name that we declare after class keyword. But in compiler , a class is represented with its namespace or package information. That is why the term Class Name can provide different type of information. In the refection we get

Full Name/getName() = Full name of a class which includes package info , like java.util.ArrayList.
Canonical Name/getCanonicalName() = Is shows Full name with uniquely identifying element(often $<num value>) for inner classes.
Simple Name/getSimpleName()  = It means Only the name (what we declare with class keyword) like myClass
Type Name =  It will express the type, usually either primitive type or other class. Usually , a class type represents it self(Full Name).
toString() = This will provide  class keyword, and full name. like class <full name of the class>
toGenericString() = This will provide modifier name, class keyword, and full name. like <access modifier> class <full name of the class>

I am using a single string variable and adding each information with separate method call.
   1: public String getAllTypesOfClassNames(){
   2:         String allTypeNames; 
   3:         allTypeNames = "Name : "+ myClass.getName()+"\n";
   4:         allTypeNames+="Canonical Name : "+ myClass.getCanonicalName()+"\n";
   5:         allTypeNames+="Simple Name : "+ myClass.getSimpleName()+"\n";
   6:         allTypeNames+="Type Name : "+ myClass.getTypeName()+"\n";
   7:         allTypeNames+="To String Name : "+ myClass.toString()+"\n";
   8:         allTypeNames+="To Generic String Name : "+ myClass.toGenericString();
   9:         return allTypeNames;
  10:     }

How to get Class’s Signer Names?
What is Signer? I will be very brief as it is not Signer Specified post. When we sign a jar file (which is actually a class archive), java signer tool go through every class inside of the jar and sign every class with specific sign information. Usually, for every signing has purpose. That is why, a class may be signed by multiple signer tool with different signs.

By reflection , we can get all signer names for a particular class. Default a sign is represented as object, I will call for its toString() method to retrieve information.
   1: public String[] getAllSignerNames(){
   2:         String[] names=null;
   3:         int x=0;
   4:         for(Object a: myClass.getSigners()){
   5:             names[x]=a.toString();
   6:             x++;
   7:         }
   8:         return names;    
   9:     }

Thanks..:)

No comments:

Post a Comment