Results 1 to 4 of 4
Thread: Typecasting with Class object
- 05-09-2011, 05:50 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
Typecasting with Class object
Hi guys
I am developing a JTable component that takes an entity class object (using hibernate) as a parameter. Basically it displays all of the database table corresponding to the class it receives as a parameter. To retrieve the data i am using a list that retrieves using the passed Class object, as such :
I get the correct list elements when doing that. The problem is that i dont know how to typecast the Objects that are read from this list to my Class parameter's class when i browse through the list. I have a basic skeleton :Java Code:public List getEntityList(Class entityClass) { Session session = factory.getCurrentSession(); session.beginTransaction(); List entityList = session.createQuery("FROM " + entityClass.getName()).list(); session.getTransaction().commit(); return entityList; }
and the SOP call prints the correct Hibernate.Contact class. The entityClass variable is the class object that is being passed to the JTable (in this case, Hibernate.Contact). What i need to do is typecast the element object to this class without explicitely writing Contact contact = (Contact)element; as this class could change depending on what i want to display in the table.Java Code:for (Iterator iter = entityList.iterator(); iter.hasNext();) { Object element = iter.next(); int listPos = 0; if (entityClass.isInstance(element)) { System.out.println(element.getClass()); } }
I have already used reflection to get the class fields to display in the JTable's column headers, as such :
}Java Code:private void initializeHeaders() { Field[] fields = entityClass.getDeclaredFields(); headers = new String[fields.length]; for (int i = 0; i < fields.length; i++) { headers[i] = fields[i].getName(); }
I looked on google for ways to achieve what i'm trying to do using reflections but found nothing.
Long story short, is it possible to typecast an Object object to the type of a Class paramter and then be able to use it's get/set methods?Last edited by d3n1s; 05-09-2011 at 06:39 PM.
- 05-09-2011, 07:08 PM #2
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
Put more simply, my question is : Can you use a Class object as a type to declare a variable of that type and use its methods?
- 05-09-2011, 07:46 PM #3
If I understand your question correctly, I think you'll find what you need if you read the javadoc for Class. For example, Class.getMethod(String name, Class... parameterTypes). returns a Method which you can invoke via Method.invoke(Object obj, Object... args).
- 05-09-2011, 08:20 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
That indeed worked like a charm! What i did is i looped through the methods and placed the getters (aka starts with get and is not the getClass() method) and i placed those in an arraylist. I then invoked each method passing the element object and it works just fine!
Thanks again!
Similar Threads
-
Drawing an object in my canvas class, the object is created in a separate class
By Hornfreak in forum AWT / SwingReplies: 3Last Post: 05-02-2011, 04:37 AM -
Insert class file as object in a table & read the object from the blob.
By facemeguru in forum New To JavaReplies: 1Last Post: 02-02-2011, 06:11 PM -
Create object of unknown class, based on existing object
By Zack in forum New To JavaReplies: 2Last Post: 06-22-2010, 04:29 AM -
Pblm during typecasting
By man4ish in forum New To JavaReplies: 2Last Post: 12-31-2009, 08:55 AM -
Creating object of Type Object class
By venkatv in forum New To JavaReplies: 3Last Post: 07-17-2007, 03:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks