Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Generics and Reflection

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Generics and Reflection

    i've been trying to create a generic who's type is defined at runtime but so far have failed, what I want to do is something like this
    public void define(object obj){
        Class c = obj.getClass();
        List<c> list = new ArrayList<c>;
    }
    I know the above does not compile, but so far is the best example of what I want to do, first of all question is, is this even possible? if so, how do you assign a generic its type through reflection?
    Last edited by helloworld922; March 11th, 2010 at 10:43 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Generics and Reflection

    As far as I'm aware, generics are resolved at compile-time, so they can't be changed once your program is compiled. What you can do is use a super class for the generic parameter, and then any inheriting classes can be added to your List via polymorphism.
    Last edited by helloworld922; March 11th, 2010 at 10:48 AM.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Generics and Reflection

    I was fearing that was the only way, since I wanted to create a simple DAO object (Using hibernateTemplates from spring) that would save,update,delete,getAll and getById procedure since so far I have seen the only thing that changes is the Type of object they receive, Thanks for the answer and should I find out any work afound to this I'll post it here.
    oh and Now that thas has come up, if I send an object of Type X (X being any used defined class) hidden behind an object due to polymorfism, will it still find the right type of class to be mapped? the following code should illustrate this.

     
    ...
    User us = new User("username","password");
    Object obj = (Object) us;
    hibernateTemplate.save(obj);

    The above implies the hbm.xml file or annotation are correct. would that work and save the user? or an object of any other class for that matter?

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Generics and Reflection

    Lists are just lists, you can put whatever you want into them, generics is an extension to the collection interfaces which lets you have type safety at compile time.

    Just do something like this.

    List list = new ArrayList();

    Now you can shove whatever objects in there you want and if you really need it to use generics you can just do something like this.

    List<Object> list = new ArrayList<Object>();

    // Json

Similar Threads

  1. Custom Java stack class (with generics) problem
    By TBBucs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2010, 01:25 AM
  2. Implementing Multiple Interfaces with Generics
    By darkestfright in forum Collections and Generics
    Replies: 5
    Last Post: February 10th, 2010, 07:44 PM
  3. Identify and avoid some of the pitfalls in learning to use generics
    By JackyRock in forum Java Theory & Questions
    Replies: 0
    Last Post: February 6th, 2010, 04:12 AM
  4. running a class from another projects, reflection
    By led1433 in forum Object Oriented Programming
    Replies: 7
    Last Post: July 29th, 2009, 01:47 PM