|
| ||||||||||
| Tags: compilation, generic, java, java programming |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Problem compiling with Generic
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
| ||||
| ||||
| 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 |
|
#3
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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);
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |