Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Problem compiling with Generic

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 12-12-2009
Member
 
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;   
    }
  }
}

Reply With Quote
  #2  
Old 12-12-2009
Member
 
Join Date: May 2008
Posts: 2,381
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();
    }
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #3  
Old 12-12-2009
Member
 
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!
Reply With Quote
  #4  
Old 12-12-2009
Member
 
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();
}
Reply With Quote
  #5  
Old 12-12-2009
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem compiling with Generic"
Thread Thread Starter Forum Replies Last Post
Problem in compiling Threadpool functions like QueUserWorkItem() Laalamani Software Development 6 25-09-2010 12:04 PM
Generic PWS.y!brn Removal Problem ramsun Networking & Security 5 21-01-2010 09:57 AM
Problem compiling Wine 1.1.36 on Mac OS X L-cynthiya Operating Systems 5 12-01-2010 06:42 PM
Generic.dx!iuf virus problem Indra Kanojia Networking & Security 5 25-12-2009 12:46 PM
Problem compiling OpenGL Hakon Software Development 3 28-04-2009 01:51 PM


All times are GMT +5.5. The time now is 12:01 AM.