Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.

Reflection is powerful, but should not be used indiscriminately. So if you find reflection is necessity then only go for it else try to avoid it.

Here I am introducing basic things required by developers concerned to reflection:

Getting the Name of a Member Object

Class GetNameByReflection{ Class cls = java.lang.String.class; Method method = cls.getMethods()[0]; Field field = cls.getFields()[0]; Constructor constructor = cls.getConstructors()[0]; String name;// Fully-qualified names name = cls.getName(); // java.lang.String name = cls.getName()+"."+field.getName(); // java.lang.String.CASE_INSENSITIVE_ORDER name = constructor.getName(); // java.lang.String name = cls.getName()+"."+method.getName(); // java.lang.String.hashCode name = cls.getName().substring( cls.getPackage().getName().length()+1); // Unqualified names String name = field.getName(); // CASE_INSENSITIVE_ORDER name = constructor.getName().substring( cls.getPackage().getName().length()+1); // String name = method.getName(); // hashCode }

Overriding Access

By default, a reflected object enforces the access as per the Java language. For example, you can’t retrieve the value from a Field object if the Field object is an private field. To overcome this checking, you call setAccessible() on the reflected object. If the program is not having the accede to call setAccessible(), in that case SecurityException is thrown.

field.setAccessible(true); constructor.setAccessible(true); method.setAccessible(true);

Modifiers List of a Class Object

int mod = cls.getModifiers(); if (Modifier.isPublic(mod)) { // class is public }

Listing the Modifiers of a Member Object

Field, Constructor, and methods are subclasses of Member.


// Modifiers from a field.
int mod = member.getModifiers();
if (Modifier.isPublic(mod)) {
    // member is public
}

Getting the Field Objects of a Class Object

In all there are three ways to get Field object from a Class object.


Class cls = in.com.MyClass.class;// By obtaining a list of all declared fields.
Field[] fields = cls.getDeclaredFields();

// By obtaining a list of all public fields, both declared and inherited.
fields = cls.getFields();
for (int i=0; i<fields.length; i++) {
    Class type = fields[i].getType();
    //further code
}

// By obtaining a particular Field object.
// This example retrieves in.com.MyClass.x.
try {
     Field field = cls.getField("x");
     //further code
} catch (NoSuchFieldException e) {}

Getting and Setting the Value of a Field

This example assumes that the field has the type String

try { field.getString(object); // Get value field.setString(object, "str"); // Set value field.getString(null); // Get value of a static field field.setString(null, "str"); // Set value of a static field } catch (IllegalAccessException e) {}

Getting a Constructor of a Class Object

There are two ways of obtaining a Constructor object from a Class object.

// By obtaining a list of all Constructors object.
Constructor[] cons = cls.getDeclaredConstructors();
for (int i=0; i<cons.length; i++) {
      Class[] paramTypes = cons[i].getParameterTypes();
     //further code
}

// By obtaining a particular Constructor object.
// This example retrieves in.com.MyClass(int, int).
try {
     Constructor con = in.com.MyClass.class.getConstructor(new Class[]{int.class, int.class});
      //process the code
} catch (NoSuchMethodException e) {
}

Object Creation Using Constructor Object

This example creates a new MyClass object from the constructor MyClass(String, String).

try { in.com.MyClass obj = (in.com.MyClass)con.newInstance( new Object[]{new String("str"), new String("str1")}); } catch (InstantiationException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { }

Getting the Methods of a Class Object

In all there are three ways of obtaining a Method object from a Class object.

Class cls = java.lang.String.class; // By obtaining a list of all declared methods. Method[] methods = cls.getDeclaredMethods(); // By obtaining a list of all public methods, both declared and inherited. methods = cls.getMethods(); for (int i=0; i<methods.length; i++) { Class returnType = methods[i].getReturnType(); Class[] paramTypes = methods[i].getParameterTypes(); //further code } // By obtaining a particular Method object. // This example retrieves MyClass.myMethod(int). try { Method method = cls.getMethod("myMethod", new Class[] {in.com.MyClass.class}); //further code } catch (NoSuchMethodException e) { }

Invoking a Method Using a Method Object

try { Object result = method.invoke(object, new Object[] {param1, param2, ....., paramN}); } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { }

So this are the things in reflection I have found on upper layer, there are lot many things you can do with reflection.