Reflection Example for 100% Dummies

Reflection Example for 100% Dummies:

I know in all the recent interviews reflection relation question are asked along with the other tough and popular core java interview questions mentioned here.

 

As mentioned in the title this is purely for 100% dummies who just heard only the term “Reflection”. I was also in the list of 100% dummies, after reading some blogs and tutorials about “Reflection” & “Reflection Examples”, I got some good understanding.

 

I am going to share here the few simple examples with ArrayList class using reflection.

 

What is Reflection ?

As per the oracle documentation,

Reflection enables Java code to discover information about the fields, methods and constructors of loaded classes, and to use reflected fields, methods, and constructors to operate on their underlying counterparts, within security restrictions.

 

 

Java Reflection Example to get the Class Name:

Here I am just doing,

  • Create arraylist with some values.
  • Assign the created arraylist to some object reference.
  • Convert this object reference to class reference using .getClass() method.
  • Now the class object will be able to help us with getName()/get methods()/get constructors() etc.,
  • getName() method prints the name of the arraylist with full package details.

package in.javadomain;

import java.util.ArrayList;

public class ReflectionExample {

public static void main(String[] args) {
ArrayList<String> tech2018List = new ArrayList<String>();
tech2018List.add("Machine Learning");
tech2018List.add("Cloud");
tech2018List.add("Big data");

Object listObj = tech2018List;
Class arrayListClass = listObj.getClass();

System.out.println("Class Name Through Reflection ==> " + arrayListClass.getName());

}

}

 

Output:

Class Name Through Reflection ==>java.util.ArrayList

 

 

Getting the ArrayList Method Names using Reflection:

  • Create arraylist with some values.
  • Assign the created arraylist to some object reference.
  • Convert this object reference to class reference using .getClass() method.
  • getMethods() method prints all the methods of arraylist.

package in.javadomain;

import java.lang.reflect.Method;
import java.util.ArrayList;

public class ReflectionExample {

public static void main(String[] args) {
ArrayList<String> tech2018List = new ArrayList<String>();
tech2018List.add("Machine Learning");
tech2018List.add("Cloud");
tech2018List.add("Big data");

Object listObj = tech2018List;
Class arrayListClass = listObj.getClass();

/* Printing all the methods present in the ArrayList class */
Method[] listOfArrayListMethods = arrayListClass.getMethods();
System.out.println("Arraylist total methods through Reflection : \n" + listOfArrayListMethods.length);
System.out.println("\nArrayList Methods: ");
for (Method eachMethod : listOfArrayListMethods) {
System.out.println(eachMethod);
}

}

}

 

Output:

Arraylist total methods through Reflection :
42

ArrayList Methods:
public void java.util.ArrayList.add(int,java.lang.Object)
public boolean java.util.ArrayList.add(java.lang.Object)
public boolean java.util.ArrayList.remove(java.lang.Object)
public java.lang.Object java.util.ArrayList.remove(int)
public java.lang.Object java.util.ArrayList.get(int)
public java.lang.Object java.util.ArrayList.clone()
public int java.util.ArrayList.indexOf(java.lang.Object)
public void java.util.ArrayList.clear()
public boolean java.util.ArrayList.contains(java.lang.Object)
public boolean java.util.ArrayList.isEmpty()
public java.util.Iterator java.util.ArrayList.iterator()
public int java.util.ArrayList.lastIndexOf(java.lang.Object)
public void java.util.ArrayList.replaceAll(java.util.function.UnaryOperator)
public int java.util.ArrayList.size()
public java.util.List java.util.ArrayList.subList(int,int)
public java.lang.Object[] java.util.ArrayList.toArray()
public java.lang.Object[] java.util.ArrayList.toArray(java.lang.Object[])
public java.util.Spliterator java.util.ArrayList.spliterator()
public boolean java.util.ArrayList.addAll(int,java.util.Collection)
public boolean java.util.ArrayList.addAll(java.util.Collection)
public void java.util.ArrayList.forEach(java.util.function.Consumer)
public java.lang.Object java.util.ArrayList.set(int,java.lang.Object)
public void java.util.ArrayList.ensureCapacity(int)
public void java.util.ArrayList.trimToSize()
public java.util.ListIterator java.util.ArrayList.listIterator(int)
public java.util.ListIterator java.util.ArrayList.listIterator()
public boolean java.util.ArrayList.removeAll(java.util.Collection)
public boolean java.util.ArrayList.removeIf(java.util.function.Predicate)
public boolean java.util.ArrayList.retainAll(java.util.Collection)
public void java.util.ArrayList.sort(java.util.Comparator)
public boolean java.util.AbstractList.equals(java.lang.Object)
public int java.util.AbstractList.hashCode()
public java.lang.String java.util.AbstractCollection.toString()
public boolean java.util.AbstractCollection.containsAll(java.util.Collection)
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
public default java.util.stream.Stream java.util.Collection.stream()
public default java.util.stream.Stream java.util.Collection.parallelStream()

 

 

Getting the ArrayList Constructors using Reflection:

  • Create arraylist with some values.
  • Assign the created arraylist to object type reference.
  • Convert this object reference to class reference using .getClass() method.
  • getConstructors() method prints all the constructors of arraylist.

package in.javadomain;

import java.lang.reflect.Constructor;
import java.util.ArrayList;

public class ReflectionExample {

public static void main(String[] args) {
ArrayList<String> tech2018List = new ArrayList<String>();
tech2018List.add("Machine Learning");
tech2018List.add("Cloud");
tech2018List.add("Big data");

Object listObj = tech2018List;
Class arrayListClass = listObj.getClass();

// Printing all the constructors present in the ArrayList class
Constructor[] listOfArrayListConstructors = arrayListClass.getConstructors();
System.out.println("Arraylist total constructors : " + listOfArrayListConstructors.length);
System.out.println("\nArrayList all the constructors:");
for (Constructor constructor : listOfArrayListConstructors) {
System.out.println(constructor);
}

}

}

 

Output:

Arraylist total constructors : 3

ArrayList all the constructors:
public java.util.ArrayList(java.util.Collection)
public java.util.ArrayList()
public java.util.ArrayList(int)

 

 

Getting the ArrayList Declared Fields using Reflection:

  • Create arraylist with some values.
  • Assign the created arraylist to object type reference.
  • Convert this object reference to class reference using .getClass() method.
  • getDeclaredFields() method prints all the declared fields of arraylist.

package in.javadomain;

import java.lang.reflect.Field;
import java.util.ArrayList;

public class ReflectionExample {

public static void main(String[] args) {
ArrayList<String> tech2018List = new ArrayList<String>();
tech2018List.add("Machine Learning");
tech2018List.add("Cloud");
tech2018List.add("Big data");

Object listObj = tech2018List;
Class arrayListClass = listObj.getClass();

/* Printing all the fields present in the ArrayList class */
Field[] listOfArrayListFields = arrayListClass.getDeclaredFields();
System.out.println("Arraylist total fields : " + listOfArrayListFields.length);
System.out.println("\nArraylist Fields : ");
for (Field eachField : listOfArrayListFields) {
System.out.println(eachField);
}

}

}

 

Output:

Arraylist total fields : 7

Arraylist Fields :
private static final long java.util.ArrayList.serialVersionUID
private static final int java.util.ArrayList.DEFAULT_CAPACITY
private static final java.lang.Object[] java.util.ArrayList.EMPTY_ELEMENTDATA
private static final java.lang.Object[] java.util.ArrayList.DEFAULTCAPACITY_EMPTY_ELEMENTDATA
transient java.lang.Object[] java.util.ArrayList.elementData
private int java.util.ArrayList.size
private static final int java.util.ArrayList.MAX_ARRAY_SIZE

 

You would have got the below question, when you have come across the getFields() and getDeclaredFields() methods in Reflection,

What is the difference between getDeclaredFields() and getFields() ? and the same discussed in stackoverflow already.

 

228 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *