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 ???
You already have your class in entityClass
, so calling entityClass.getClass()
will give you java.lang.Class
and entityClass.getClass().getDeclaredFields()
will indeed give you methods of Class
not of your particular class.
You need:
Field field [] = entityClass.getDeclaredFields();