Reflection API is part of core java. This is the most powerful API that java has ever
produced. Reflection is the powerful technique and can produce application which would
otherwise impossible. Other than very minor drawbacks of reflection it is always useful to
develop any kind of new configurable API. We can find hundreds of new Java
technologies, tools which are built on reflection.
Reflection API:
This API contains mainly the way to access and modify the class, field, methods and
constructors.
We are very much familiar with above core attributes of JAVA but we should know how to
access and modify them during runtime using reflection.
Class:
Following code tells you how to access/create a class during runtime.
Class c = Class.forName(someVar);
Here someVar is the fully qualified name of the class we wanted to load in runtime.
Similarly to create array of some primitive type we can also generate at runtime as
following
Class stringArray = Class.forName(“[java.lang.String”);
Primitive type class can be created in runtime as following.
Class primitive = Double.TYPE
This will create a double type primitive object.
Field:
Accessing or modifying a field.
Class c = Class.forName(someVar); Field someFieldName = c.getField(someField);
We can even get the class name of the above field as following.
Class classOfField = someFieldName.getDeclaringClass();
Method:
Following code is to access a method.
Method method = c.getMethod(someMethod, Object[] params);
Following code shows to invoke a method.
Object returnObject = method.invoke(Object objectWhoseMethodTobeInvoked, Object[] params);
Constructor:
Class c = Class.forName(someVar); Constructor constructor = c.getConstructor() ; constructor.newInstance();
Obejct hacking using reflection:
This does not really mean any kind of hacking concept. We can break the java security
concepts using reflection. This is called the object hacking concept.
As we all know java is built in some private accessor type. By declaring any field or
constructor or method by this accessor we can not really access or construct the same. But
using reflection we can break these concepts.
We can do that very well by following steps:
1. Get the private constructor or field or method.
2. Set value setAccesible(true).
3. Try accessing now in public way
Ex:
Method m = cls.getMethod(somePriveteMethod); m.setAccesible(true); m.invoke(object, null);
Use:
• Because of the ability to modify access the core attributes of java in runtime
reflection is widely used to develop configurable powerful tools. Like DOZER,
Springs, Hibernate and in most of the tools we use everyday to develop
applications in java.
For example say about DOZER API.
We can have one or many mapping configuration files. DOZER API reads those
mapping files using a XML parser and then creates list of maps of class and the
fields to be mapped. And again it loads the class in runtime and creates the get
and set methods of the fields and invokes the method to get value from source to
set in destination.
- Runtime implementation based on the identification of class, method, fields like complex operation is very easy to implement
- We can reduce thousand lines of code if reflection is used. These are the overhead use of Reflection.
- Reflection is a bit slower code as types are required to be solved dynamically by JVM
- Reflection sometimes can produce side effect implies can raise a portability issue.