Results 1 to 4 of 4
  1. #1
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default 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 :

    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;
        }
    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:
    for (Iterator iter = entityList.iterator(); iter.hasNext();)
                {
                    Object element = iter.next();
                    int listPos = 0;
                    if (entityClass.isInstance(element))
                    {
                        System.out.println(element.getClass());
                    }
                }
    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.

    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.

  2. #2
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    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?

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default

    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).

  4. #4
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by kjkrum View Post
    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).
    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

  1. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  2. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  3. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  4. Pblm during typecasting
    By man4ish in forum New To Java
    Replies: 2
    Last Post: 12-31-2009, 08:55 AM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •