Results 1 to 5 of 5

Thread: Problem compiling with Generic

  1. #1
    Join Date
    Nov 2009
    Posts
    36

    Problem compiling with Generic

    I have a problem in compilation with the following code, I tried a lot of possibilities except the one I corrected my problem (I have missed something). The code in question, I deliberately simplify my code

    Code:
    import java.util.*;
     
    public class BugCompiler
    {
      interface IObjectFactory<T>
      {
        public T createObject();
        public Class<T> getObjectClass(); 
      }
     
      class StringObjectFactory implements IObjectFactory<String>
      {
        @Override
        public String createObject()
        {
          return "test";
        }
     
        @Override
        public Class<String> getObjectClass()
        {
          return String.class;
        }
      }
     
      class ArrayListObjectFactory implements IObjectFactory<ArrayList<String>>
      {
        @Override
        public ArrayList<String> createObject()
        {
          return new ArrayList<String>();
        }
     
        @Override
        public Class<ArrayList<String>> getObjectClass()
        {
          return ArrayList.class;   
        }
      }
    }

  2. #2
    Join Date
    May 2008
    Posts
    2,393

    Re: Problem compiling with Generic

    It's not very clean but the following code compiles
    Code:
    public Class<ArrayList<String>> getObjectClass()
        {
          return (Class<ArrayList<String>>) new ArrayList<String>().getClass();
        }

  3. #3
    Join Date
    Nov 2008
    Posts
    1,022

    Re: Problem compiling with Generic

    Indeed it is not very clean. In the present circumstances, Class <ArrayList <String>> does not exist!

    At runtime you can not know the typeage Generics of a class!

  4. #4
    Join Date
    Nov 2009
    Posts
    36

    Re: Problem compiling with Generic

    Is it not possible to change my interface to be cleaner code? Is there a way by changing my interface to define the class?
    Code:
    interface IObjectFactory<T>
    {
        public T createObject();
        public Class<T> getObjectClass();
    }

  5. #5
    Join Date
    Nov 2008
    Posts
    1,022

    Re: Problem compiling with Generic

    You can try by fiddling a bit:
    Code:
      @SuppressWarnings("unchecked")
      private <R> R unsafeCast(Object t) {
    	  return (R) t;
      }
     
      public Class<ArrayList<String>> getObjectClass() {
    	  return unsafeCast(ArrayList.class);
      }
    But in the type ArrayList<String> means nothing to the performance.

Similar Threads

  1. Problem in compiling Threadpool functions like QueUserWorkItem()
    By Laalamani in forum Software Development
    Replies: 6
    Last Post: 25-09-2010, 12:04 PM
  2. Generic PWS.y!brn Removal Problem
    By ramsun in forum Networking & Security
    Replies: 5
    Last Post: 21-01-2010, 09:57 AM
  3. Problem compiling Wine 1.1.36 on Mac OS X
    By L-cynthiya in forum Operating Systems
    Replies: 5
    Last Post: 12-01-2010, 06:42 PM
  4. Generic.dx!iuf virus problem
    By Indra Kanojia in forum Networking & Security
    Replies: 5
    Last Post: 25-12-2009, 12:46 PM
  5. Problem compiling OpenGL
    By Hakon in forum Software Development
    Replies: 3
    Last Post: 28-04-2009, 01:51 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
  •