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 3 of 3

Thread: Generics collections and reflection

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

    Default Generics collections and reflection

    Hi to all,

    I have a simple class, i.e

    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:

    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?

    Thanks in advance

    Francesco


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Generics collections and reflection

    Can you use public setters for each field? This might better and more easily enforce the generics...create a new instance of the Set/List/Map, then set the value via the setter method (iterate over the Methods rather than Fields). There is also a getGenericType method in the Field class but I haven't yet played with this enough to recommend anything.

  3. #3
    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 collections and reflection

    I think the best course of action would be to define a default constructor for your class. Declaring setters/getters is also a good practice.

    public class TestRun
    {
      private Set<Integer> set;
      private String string;
      public List<String> collection;
      private HashSet<Boolean> hs;
     
       public TestRun()
        {
            // initialize all the variables here
        }
     
        public void setSet(Set<Integer> set)
        {
            this.set = set;
        }
     
        public Set<Integer> getSet()
        {
            return set;
         }
        // ... etc. etc.
    }

    You've declared the fields private, and I believe even when using Reflection Java will enforce the visibility rules.

Similar Threads

  1. how to sort objects with collections???
    By kyros in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 02:21 PM
  2. Reflection and finalization
    By Dastagiri in forum Java Theory & Questions
    Replies: 1
    Last Post: July 6th, 2010, 11:20 PM
  3. Generics and Reflection
    By Kassiuz in forum Collections and Generics
    Replies: 3
    Last Post: March 15th, 2010, 09:32 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
  5. Replies: 10
    Last Post: June 22nd, 2009, 07:45 AM