What is reflection in Java?
The word reflection implies that the properties of whatever being examined are displayed or reflected to someone who wants to observe those properties. Similarly, Reflection in Java is the ability to examine and/or
modify the properties or behavior of an object at run-time. It’s important to note that reflection specifically
applies to objects – so you need an object of a class to get information for that particular class.
Reflection is considered to be an advanced technique, and can be quite powerful when used correctly.Reflection in Java consists of 2 primary things that you should remember:
1. Metadata. Metadata literally means data about the data. In this case, metadata means
extra data that has to do with your Java program – like data about your Java classes,
constructors, methods, fields, etc.
2. Functionality that allows you to manipulate the metadata as well. So, functionality
that would allow you to manipulate those fields, methods, constructors, etc. You can
actually call methods and constructors using Java reflection – which is an important fact
to remember.
Java reflection is part of an API package
In order to use reflection in Java, you must include the necessary classes from the
java.lang.reflect package since that contains all the necessary classes for implementing
reflection. The one exception to this is when retrieving class metadata – for which you
would use the java.lang.Class class, which is not part of the java.lang.reflect package.
Let’s look at an example of Java reflection to help clarify exactly what it does and when
it can be useful.
A Simple Example of Java Reflection
Here is an easy to understand example of some code that searches up a class hierarchy
to see if a particular method is defined somewhere in the inheritance tree. In other
words, we are just looking for a method in the parent classes of a given class.
import java.lang.reflect.Method;
import java.lang.Class;
Method findInheritedMethod(Class classType,
String theMethodName, Class [] methodParamTypes)
{
Method inheritedMethod = null;
while(classType != null) {
try {
inheritedMethod =
classType.getDeclaredMethod(theMethodName, methodParamTypes);
break;
}
catch (NoSuchMethodException ex) {
classType = classType.getSuperclass( );
}
return inheritedMethod ;
}
An explanation of the Java reflection example
As you can see in our example above, the findInheritedMethod method takes as its
parameters a class name from which to start searching, a string that represents the
method name being searched for, and the parameter types that the method being
searched for accepts in case the method being searched for is overloaded and has
multiple versions that accept different parameter types.
The way the example works above is that the current class is searched to see if the
method being searched for resides in the current class. If it doesn’t then it keeps
searching the superclass (the parent class) until it reaches the very top of the hierarchy –
you can see that inside the catch block the parent class is retrieved and the process
continues in the while loop either until the method is found or we reach the top of the
inheritance hierarchy (at which point classType will be null).
Also notice that in the code above we import a class called Method. The object of the
Method class – called “inheritedMethod” – is what stores the method being searched for
if and when it is found. And an object of the Method class is also what’s returned by the
findInheritedMethod method, which is why you can clearly see the “Method” in the
method signature of findInheritedMethod. The class named “Class” is also imported and
used in the example above – it’s simply used to hold class types.
There are many other things that are possible with Java Reflection – our example above
is just one simple example of the many possibilities. With Reflection, we can also invoke
methods, constructors, set fields, and do many other things. The point of the example
above is to give you an idea of what Java Reflection is all about. A very high level
description is that Reflection allows you to get information about any particular class
object, and also allows you to manipulate that information.
Is Java reflection slow and/or expensive? What are some drawbacks of reflection in Java?
One of the main drawbacks of reflection in Java is the fact that Reflection can slow
down performance because reflection involves types that are resolved at run-time – and
not at compile time. In our example above, note how the findInheritedMethod method
accepts a “Class” type as one of its parameters – clearly, at run time that parameter will
be a concrete class when the method is actually invoked, but at compile time we do not
know what class will be passed for that method parameter. Because a lot of the JVM
optimizations are performed at compile time, this means that portions of code that use
reflection will run slower than if that same code did not contain “reflective” code. For
this reason, reflection should be avoided in sections of code that are important in
applications that need to be very efficient in their performance.
Reflection exposes the internals of a class
Another drawback of reflection is the fact that code which uses reflection can basically
access information that is usually not allowed in non-reflexive code. For instance,
reflection allows you to access private methods and fields in a class that would
otherwise be inaccessible in non-reflective code, and that could potentially ruin the code
or make it non-portable.
What are some practical uses of Java reflection?
One of the more common uses of Java reflection is with JUnit, which is a popular unit
testing framework for Java. One commonly used feature of JUnit uses annotations to
mark certain methods as test methods that which are meant to help you test your Java
code. Then, when you actually decide to run your unit tests, JUnit will look for the
methods which you marked with the “@Test” annotation, and will execute those
methods.
Visit our FORUM
The word reflection implies that the properties of whatever being examined are displayed or reflected to someone who wants to observe those properties. Similarly, Reflection in Java is the ability to examine and/or
modify the properties or behavior of an object at run-time. It’s important to note that reflection specifically
applies to objects – so you need an object of a class to get information for that particular class.
Reflection is considered to be an advanced technique, and can be quite powerful when used correctly.Reflection in Java consists of 2 primary things that you should remember:
1. Metadata. Metadata literally means data about the data. In this case, metadata means
extra data that has to do with your Java program – like data about your Java classes,
constructors, methods, fields, etc.
2. Functionality that allows you to manipulate the metadata as well. So, functionality
that would allow you to manipulate those fields, methods, constructors, etc. You can
actually call methods and constructors using Java reflection – which is an important fact
to remember.
Java reflection is part of an API package
In order to use reflection in Java, you must include the necessary classes from the
java.lang.reflect package since that contains all the necessary classes for implementing
reflection. The one exception to this is when retrieving class metadata – for which you
would use the java.lang.Class class, which is not part of the java.lang.reflect package.
Let’s look at an example of Java reflection to help clarify exactly what it does and when
it can be useful.
A Simple Example of Java Reflection
Here is an easy to understand example of some code that searches up a class hierarchy
to see if a particular method is defined somewhere in the inheritance tree. In other
words, we are just looking for a method in the parent classes of a given class.
import java.lang.reflect.Method;
import java.lang.Class;
Method findInheritedMethod(Class classType,
String theMethodName, Class [] methodParamTypes)
{
Method inheritedMethod = null;
while(classType != null) {
try {
inheritedMethod =
classType.getDeclaredMethod(theMethodName, methodParamTypes);
break;
}
catch (NoSuchMethodException ex) {
classType = classType.getSuperclass( );
}
return inheritedMethod ;
}
An explanation of the Java reflection example
As you can see in our example above, the findInheritedMethod method takes as its
parameters a class name from which to start searching, a string that represents the
method name being searched for, and the parameter types that the method being
searched for accepts in case the method being searched for is overloaded and has
multiple versions that accept different parameter types.
The way the example works above is that the current class is searched to see if the
method being searched for resides in the current class. If it doesn’t then it keeps
searching the superclass (the parent class) until it reaches the very top of the hierarchy –
you can see that inside the catch block the parent class is retrieved and the process
continues in the while loop either until the method is found or we reach the top of the
inheritance hierarchy (at which point classType will be null).
Also notice that in the code above we import a class called Method. The object of the
Method class – called “inheritedMethod” – is what stores the method being searched for
if and when it is found. And an object of the Method class is also what’s returned by the
findInheritedMethod method, which is why you can clearly see the “Method” in the
method signature of findInheritedMethod. The class named “Class” is also imported and
used in the example above – it’s simply used to hold class types.
There are many other things that are possible with Java Reflection – our example above
is just one simple example of the many possibilities. With Reflection, we can also invoke
methods, constructors, set fields, and do many other things. The point of the example
above is to give you an idea of what Java Reflection is all about. A very high level
description is that Reflection allows you to get information about any particular class
object, and also allows you to manipulate that information.
Is Java reflection slow and/or expensive? What are some drawbacks of reflection in Java?
One of the main drawbacks of reflection in Java is the fact that Reflection can slow
down performance because reflection involves types that are resolved at run-time – and
not at compile time. In our example above, note how the findInheritedMethod method
accepts a “Class” type as one of its parameters – clearly, at run time that parameter will
be a concrete class when the method is actually invoked, but at compile time we do not
know what class will be passed for that method parameter. Because a lot of the JVM
optimizations are performed at compile time, this means that portions of code that use
reflection will run slower than if that same code did not contain “reflective” code. For
this reason, reflection should be avoided in sections of code that are important in
applications that need to be very efficient in their performance.
Reflection exposes the internals of a class
Another drawback of reflection is the fact that code which uses reflection can basically
access information that is usually not allowed in non-reflexive code. For instance,
reflection allows you to access private methods and fields in a class that would
otherwise be inaccessible in non-reflective code, and that could potentially ruin the code
or make it non-portable.
What are some practical uses of Java reflection?
One of the more common uses of Java reflection is with JUnit, which is a popular unit
testing framework for Java. One commonly used feature of JUnit uses annotations to
mark certain methods as test methods that which are meant to help you test your Java
code. Then, when you actually decide to run your unit tests, JUnit will look for the
methods which you marked with the “@Test” annotation, and will execute those
methods.
Visit our FORUM




No comments:
Post a Comment