Results 1 to 2 of 2
  1. #1
    juza is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default Generics collections and reflection

    Hi to all,

    I have a simple class, i.e

    Java Code:
    public class TestRun
    {
      private Set<Integer> set;
      private String string;
      public List<String> collection;
      private HashSet<Boolean> hs;
      ...
    }
    I want to make a factory for this class, so that each collection will be instantiated to a subclass I will define:

    Java Code:
    public class BusinessObjectFactory
    {
      public static Object create(Class<?> c) throws InstantiationException, IllegalAccessException, IllegalArgumentException, ClassNotFoundException
      {
        Object o = (Object) c.newInstance();
    
        for ( Field field : c.getDeclaredFields() )
        {
          Class<?> fieldClass = field.getType();
          
          Object fieldObject = null;
          
          if(fieldClass.getName().equals("java.util.Set"))
          {
            fieldObject = new HashSet();
          }
          else if(fieldClass.getName().equals("java.util.List"))
          {
            fieldObject = new ArrayList();
          }
          else if(fieldClass.getName().equals("java.util.Map"))
          {
            fieldObject = new HashMap();
          }
          else
          {
            fieldObject = fieldClass.newInstance();
          }
          
          field.setAccessible( true );
          field.set( o, fieldObject);
        }
    
        return o;
      }
    Factory call:

    TestRun tr = (TestRun) BusinessObjectFactory.create(TestRun.class);

    My question is:

    How Have I to instantiate the collection with the right parameter:
    new HashSet<Integer>(), new ArrayList<String>(), ...

    I tried with fieldObject = new HashSet<?>(); ecc
    but it' not possible...

    Can someone help me please?

    Thanx in advance

    Francesco
    Last edited by juza; 11-29-2010 at 02:04 PM.

  2. #2
    a_glassman is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default Not Possible, but alternate solutions available.

    I do not believe what you are trying to do is possible, but I do think there are some ways to accomplish what you want to do.

    It is possible to grab the type of the generic:
    Java Code:
    Type type = field.getGenericType();

    The problem comes from the fact that the compiler needs to know what type the generic will be at compile time, meaning you couldn't do the following (or some other variation):
    Java Code:
    Object fieldObject = null;
          
    if(fieldClass.getName().equals("java.util.Set"))
    {
          fieldObject = new HashSet<field.getGenericType()>();
    }

    If you have a limited number of types that these generics use, then I would use the following in your factory class:
    Java Code:
    Object fieldObject = null;
    Type type = field.getGenericType();
    if(fieldClass.getName().equals("java.util.Set"))
    {
          if(type.toString().equals("java.util.Set<java.lang.Integer>"))
          {
                fieldObject = new HashSet<Integer>();
          }
          else if(type.toString().equals("java.util.Set<OtherType>"))
          {
                fieldObject = new HashSet<OtherType>();
          }
    }
    Hope this helps.

Similar Threads

  1. Help with generics
    By shai in forum New To Java
    Replies: 0
    Last Post: 08-12-2010, 08:07 AM
  2. Generics
    By sakshamkum in forum Advanced Java
    Replies: 3
    Last Post: 06-03-2010, 11:12 PM
  3. Generics
    By bschmitt78 in forum Advanced Java
    Replies: 3
    Last Post: 03-16-2010, 03:21 AM
  4. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:08 PM
  5. Help w/ generics
    By Hollywood in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 04:08 AM

Posting Permissions

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