Display Object Information with Generic and Reflection in Java


Create generic package. In this package, create new java class named AbstractModel as below:

package generic;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class AbstractModel<T extends Object> {

	private Class<T> entityClass;

	public AbstractModel(Class<T> entityClass) {
		this.entityClass = entityClass;
	}

	public void display() {

		System.out.println("Class Info");
		System.out.println("\tClass Name: " + entityClass.getSimpleName());
		System.out.println("\tPackage Name: " + entityClass.getPackage().getName());
		System.out.println("\tModifier: " + Modifier.toString(entityClass.getModifiers()));

		System.out.println("Declared Fields");
		for (Field field : entityClass.getDeclaredFields()) {
			System.out.println("\tField Name: " + field.getName());
			System.out.println("\tField Type: " + field.getType().getName());
			System.out.println("\t==================================");
		}

		System.out.println("Declared Methods");
		for (Method method : entityClass.getDeclaredMethods()) {
			System.out.println("\tMethod Name: " + method.getName());
			System.out.println("\tMethod Type: " + method.getReturnType().getName());
			System.out.println("\t==================================");
		}
	}

}




package generic;

import entities.Product;
import entities.Student;

public class Main {

	public static void main(String[] args) {

		System.out.println("Student Info");
		AbstractModel<Student> abstractModel1 = new AbstractModel<Student>(Student.class);
		abstractModel1.display();

		System.out.println("Product Info");
		AbstractModel<Product> abstractModel2 = new AbstractModel<Product>(Product.class);
		abstractModel2.display();

	}

}
Student Info
Class Info
	Class Name: Student
	Package Name: entities
	Modifier: public
Declared Fields
	Field Name: id
	Field Type: java.lang.String
	==================================
	Field Name: name
	Field Type: java.lang.String
	==================================
	Field Name: age
	Field Type: int
	==================================
Declared Methods
	Method Name: getName
	Method Type: java.lang.String
	==================================
	Method Name: getId
	Method Type: java.lang.String
	==================================
	Method Name: setName
	Method Type: void
	==================================
	Method Name: setId
	Method Type: void
	==================================
	Method Name: getAge
	Method Type: int
	==================================
	Method Name: setAge
	Method Type: void
	==================================

Product Info
Class Info
	Class Name: Product
	Package Name: entities
	Modifier: public
Declared Fields
	Field Name: id
	Field Type: java.lang.String
	==================================
	Field Name: name
	Field Type: java.lang.String
	==================================
	Field Name: price
	Field Type: double
	==================================
	Field Name: quantity
	Field Type: int
	==================================
Declared Methods
	Method Name: getName
	Method Type: java.lang.String
	==================================
	Method Name: getId
	Method Type: java.lang.String
	==================================
	Method Name: setName
	Method Type: void
	==================================
	Method Name: setId
	Method Type: void
	==================================
	Method Name: setQuantity
	Method Type: void
	==================================
	Method Name: getPrice
	Method Type: double
	==================================
	Method Name: setPrice
	Method Type: void
	==================================
	Method Name: getQuantity
	Method Type: int
	==================================