mercredi 6 janvier 2016

How to create class from class canonical name in java?

I have class canonical name like this dev.ashish.mvc.beans.Employee. Using this how can i create Class Employee at runtime in order access data members and member functions of Employee.

At runtime i want create class using its canonical name. At times it can be any entity Employee,Customer,User etc.

I tried this :

Class entityClass = Class.forName("dev.ashish.mvc.beans.Employee");

the above code does return class if i do entityClass.getName() it does return me dev.ashish.mvc.beans.Employee but how can i access methods of class Employee .

If i use java reflection like below :

Field field [] =  entityClass.getClass().getDeclaredFields();

it returns me declared fields of class java.lang.Class instead of dev.ashish.mvc.beans.Employee

How can i achieve this ???





Aucun commentaire:

Enregistrer un commentaire