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..:)

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.

Thanks…:)

How to get Class Information with Java Reflection? Interfaces and Super Class

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 Interfaces and Super Class names.
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. To do that
   1: public class ClassReflector extends BasicItemsComponents{    
   2:     private Class myClass;
   3:     public ClassReflector(Class aClass){
   4:         this();
   5:         myClass = aClass;
   6:     }
   7:     ClassReflector(){//todo for next default initiation 
   8:     }
How to Get Interface Names ?
So, lets have a meted for getting all Interface names which will provide output as string array where every string represent name of the implemented interface.

   1:  public String[] getAllInterfaceNames(){
   2:          String[] interfaces = null;
   3:          int x=0;
   4:          for(Class aClass: this.myClass.getInterfaces())
   5:          {
   6:              interfaces[x]=new String(aClass.getSimpleName());
   7:              x++;
   8:          }
   9:          return interfaces;
  10:      }
How to Get Super Class Name ?
As we know, Java does not support multiple inheritance, so there will be one name. 

   1: public String getParentClassName(){
   2:         return this.myClass.getSuperclass().getName();
   3:     }
 
Thanks ..:)

What is Reflection? How it works in Java?

In this article we are going to know about Reflection. Reflection basic, How it works? how we can use it? I will use Java example with eclipse. But you can use C# also. I will provide separate post for Reflection with C#.

What is Reflection?
By name Reflection we know it reflect some thing. Actually, it reflects existing class, methods, fields, interfaces. We can get all information as well as we can access those, invoke those or some time manipulate those.  

In short we can call Reflection as a class manipulator. I have seen people get very afraid by the mane. It’s actually a very easy way to access class items instead of calling them. It is more over an API rather than technique to change existing class/packages. It will mainly work for meta data rather then main information.

How it works?
Actually, it works on JVM. That means, we can use reflection to access classes when they are loaded to JVM. Unlikely using in compile time, reflection access all items in a class during runtime in JVM. That means, we can apply dynamic logics, make dynamic and flexible application with reflection. And we should care about the complexity that it might cause.

Why we use it?
->I should say, it is mostly used when we need to take decision dynamically or runtime. That is is why it is mostly used with different patterns(like factory).
->We use it for testing, spatially in making stub or mocking.
->We use it with de compiler, spatially we want to know the artifact(source code)  from existing class or jar or war. (with help of other APIs)
->We can use in monitoring(what class loaded and what it is going to do)
->we use this for accessing private members or functions of a class.
->We can use for contracting source code with existing compiled code.
->Very handy when we make keyword driven test execution with framework.
->We can define source(as string) code at runtime.
->If we make a pluggable software where we can add feature while the main application is running, then reflection is the only way. It’s kind of responsible for new function and behaviors in Runtime.
->I found it very useful for monitoring(and debugging) client and server side running application from a third party point of view (or service).
->Runtime class creation, parameterize behavior , extensible, separate compilation, self-examinable are the major benefits.
->When we are writing adaptation based programming, this is the only way.

Why we should not use it?
->As, reflection works run time, the application might get slow and unstable if we use this. We need to be very precise and careful while using reflection.
->Reflection does not work with Applets.
->Definitely Reflection violates the security & integrity
->Some time, Reflection gets sloppy. It might give good performance some time, and some time bad.

So, hard to decide the root cause. So, super carefulness needed. I found some statistics in Net regarding using reflection for field access(comparing different Java version)
field-accesses
->Some time it is impossible to resolve an issue implemented using Reflection.  Program became more an more complex if we don’t take care before using.

How we can use it?
In Java, there is whole library for doing this. If we use import java.lang.reflect , then we get all access. Inside the reflect package, we get several classes that provide different access mechanism to access several artifacts of a target class. This is a small illustration of what we get with reflection API(source: internet)
java-reflection-api

I will write separate posts for accessing each of those.

Thanks .. :)