Wednesday, 25 October 2017

How to get the metadata information of a class using java reflection | Reflection in java


Student.java
public class Student
{
    private String first; // first name
    private String last; // last name
    private String email; // email address
    private int section; // section number

    // construct a new student with given fields
    public Student(String first, String last, String email, int section)
    {
        this.first = first;
        this.last = last;
        this.email = email;
        this.section = section;
    }

    public void showStudentFullName()
    {
        System.out.println(first+last);
    }

    // return a string representation of the invoking object
    public String toString()
    {
        return section + " " + first + " " + last + " " + email;
    }
}
ReflectionDemo.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * How to display the metadata of a class.
 *
 */
public class ReflectionDemo
{
    public static void main(String[] args)
    {

        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter the ClassName: ");
            String className= br.readLine();
            br.close();
            
            Class<?> classObj = Class.forName(className);
            System.out.println("Fields: ");

            /*
             * Returns:the array of Field objects representing all the declared
             * fields of this class
             */
            Field[] fieldArray = classObj.getDeclaredFields();
            for (Field field : fieldArray)
            {
                System.out.println(field);
            }

            System.out.println("---------------------------------------");

            System.out.println("\nConstructors:");
            /*
             * Returns:the array of Constructor objects representing all the
             * declared constructors of this class
             */
            Constructor<?>[] constructorArray = classObj.getDeclaredConstructors();
            for (Constructor<?> constructor : constructorArray)
            {
                System.out.println(constructor);
            }
            System.out.println("---------------------------------------");

            System.out.println("\nMethods:");
            /*
             * Returns:the array of Method objects representing all the declared
             * methods of this class
             */
            Method[] methodArray = classObj.getDeclaredMethods();

            for (Method method : methodArray)
            {
                System.out.println(method);
            }
        }
        catch (ClassNotFoundException | IOException e)
        {
            e.printStackTrace();
        }

    }

}
Output
Enter the ClassName: java.util.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
---------------------------------------

Constructors:
public java.util.ArrayList(java.util.Collection)
public java.util.ArrayList()
public java.util.ArrayList(int)
---------------------------------------

Methods:
public boolean java.util.ArrayList.add(java.lang.Object)
public void java.util.ArrayList.add(int,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(java.lang.Object[])
public java.lang.Object[] java.util.ArrayList.toArray()
public java.util.Spliterator java.util.ArrayList.spliterator()
static int java.util.ArrayList.access$100(java.util.ArrayList)
public boolean java.util.ArrayList.addAll(int,java.util.Collection)
public boolean java.util.ArrayList.addAll(java.util.Collection)
private void java.util.ArrayList.readObject(java.io.ObjectInputStream) 
 throws java.io.IOException,java.lang.ClassNotFoundException
private void java.util.ArrayList.writeObject(java.io.ObjectOutputStream) 
 throws java.io.IOException
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()
private void java.util.ArrayList.ensureCapacityInternal(int)
private static int java.util.ArrayList.hugeCapacity(int)
java.lang.Object java.util.ArrayList.elementData(int)
private void java.util.ArrayList.grow(int)
public java.util.ListIterator java.util.ArrayList.listIterator()
public java.util.ListIterator java.util.ArrayList.listIterator(int)
public boolean java.util.ArrayList.removeAll(java.util.Collection)
public boolean java.util.ArrayList.removeIf(java.util.function.Predicate)
protected void java.util.ArrayList.removeRange(int,int)
public boolean java.util.ArrayList.retainAll(java.util.Collection)
public void java.util.ArrayList.sort(java.util.Comparator)
private java.lang.String java.util.ArrayList.outOfBoundsMsg(int)
private void java.util.ArrayList.rangeCheckForAdd(int)
private boolean java.util.ArrayList.batchRemove(java.util.Collection,boolean)
private void java.util.ArrayList.ensureExplicitCapacity(int)
private void java.util.ArrayList.fastRemove(int)
private void java.util.ArrayList.rangeCheck(int)
static void java.util.ArrayList.subListRangeCheck(int,int,int)

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_display_metadata.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_display_metadata

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/6c95796df09abfb09587d07613e2e6d5f9ad024e/BasicJava/ReflectionDemo_display_metadata/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment