JAVA REFLECTION
Java
reflection is a class in java from package java.lang.reflect we can
get all the properties and explore from other classes such as its
constructors, methods, and fields. let’s try.
First
we create super class
abstract
public
class
Person {
abstract
void
yourName();
}
and
then we create the class which is extends Person Class
public
class
Myself extends
Person{
@Override
void
yourName() {
System.out.println("my
name is Iman");
}
}
now we create demo class how java reflection work, in our demo class we just try to retrieve all the methods in a class and its super class .
One thing that we should aware that when to call the name of class when using java reflection Class for “Class”, if we didn’t put our java file in package we can invoke Class.forName(“class_name”) but if we put our class in package Class.forName(“package_name.class_name”)
import
java.lang.reflect.Field;
import
java.lang.reflect.Method;
public
class
ReflectionDemo {
public
static
void
main(String[] args) {
try
{
Class
demo = Class.forName("demo.Myself");
//
this for retrieving method name
Method
method[] =demo.getDeclaredMethods();
for
(int
i = 0; i < method.length;
i++) {
System.out.println("
the method name is --"
+method[i].toString());
}
//
retrieving super class from Myself Class
Class
superClass = demo.getSuperclass();
System.out.println("Name
of super class for Myself is --"
+superClass.getName());
//
this for retrieving method name in super class
Method
superMethods[] = superClass.getDeclaredMethods();
for
(int
i = 0; i < superMethods.length;
i++) {
System.out.println("
the method name is -- "
+superMethods[i].toString());
}
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
the console will show bellow :
the
method name is -- void demo.Myself.yourName()
Name
of super class for Myself is -- demo.Person
the
method name is -- abstract void demo.Person.yourName()
java reflection can retrieve all methods or fields even in private mode on, one unique things that we can access private method using reflection, let’s try how to access the private method that in real OOP we can’t access.
in MySelf class we add method :
private
String
sayHello(String hello){
return
hello;
}
and in DemoReflection class we add few line code for accessing private method
//
try to access private method
Myself
myself = new
Myself();
//
get the method which is private
Method
privateMethod = demo.Myself.class.getDeclaredMethod("sayHello",
String.class);
//
this the key for making us access the private method
privateMethod.setAccessible(true);
String
returnValue = (String)privateMethod.invoke(myself,
"hello...guys!!!");
System.out.println("AHHA
!!!!! we got the method .... "+returnValue);
if we run DemoReflection the console will look like bellow :
the
method name is -- private java.lang.String
demo.Myself.sayHello(java.lang.String)
the
method name is -- void demo.Myself.yourName()
Name
of super class for Myself is -- demo.Person
the
method name is -- abstract void demo.Person.yourName()
AHHA
!!!!! we got the method .... hello...guys!!!
Now we access the private field using reflection, by giving value to private field, execute method that not private method which invoke private field.
Add code bellow to Myself
private
String name;
String
yourNameIs(){
return
name;
}
And then we add code to our DemoReflection to accessing private field
//
get the field which is private
Field
field = Myself.class.getDeclaredField("name");
//
this the key let us accessing the private field
field.setAccessible(true);
field.set(myself,
"iman");
System.out.println("
--- "+myself.yourNameIs());
and the console we show like bellow :
the
method name is -- java.lang.String demo.Myself.yourNameIs()
Name
of super class for Myself is -- demo.Person
the
method name is -- abstract void demo.Person.yourName()
AHHA
!!!!! we got the method .... hello...guys!!!
---
iman
as seen above we can access field private and private method, what about with OOP concept, private it means the others class cannot access fields or methods, because of java has reflection class we can access all private mode.
one
thing that we should note that private is intended to prevent misuse,
but not security mechanis