and using it in this way Funnier <Foo,Bar>funnier=new Funnier<Foo,Bar>(Foo.class,Bar.class); or Funny <Foo>funny=new Funny<Foo>(Foo.class);
-- regards, Vineet Semwal On Thu, Sep 17, 2009 at 8:16 PM, vineet semwal <[email protected]>wrote: > I should have stated it clearly .. > i actually meant passing type Class in constructor itself like in > following code:- > > class Funny<T> { > private Class<T>typeClass; > public Funny(Class<T>typeClass){ > this.typeClass=typeClass; > > } > } > class Funnier<T,R> extends Funny<T>{ > private Class<T>typeClassT; > private Class<R>typeClassR; > public Funnier( Class<T>typeClassT,Class<R>typeClassR){ > super(typeClassT); > this.typeClassT=typeClassT; > this.typeClassR=typeClassR; > > } > } > > > -- > regards, > Vineet Semwal > > > On Thu, Sep 17, 2009 at 2:58 PM, jWeekend <[email protected]>wrote: > >> >> Vineet , >> >> Yes, this is a technique that, carefully and properly used, could help in >> building a generic DAO. >> Can you elaborate on "intializing the class type in constructor." ? >> >> Regards - Cemal >> jWeekend >> OO & Java Technologies, Wicket Training and Development >> http://jWeekend.com >> >> >> >> >> vineet semwal wrote: >> > >> > the first time I used genericsuperclass() was in generic daos after >> > reading https://www.hibernate.org/328.html, >> > though it has it's own quirks, it doesn't apply every where .. >> > abstract modifier in above code was needed,it's done so that one will >> > eventually subclass funnyfactory >> > and that genericsuperclass hack will work .. >> > >> > apart from that one another simple way for retrieving the generic >> > classtype >> > is intializing the class type in >> > constructor. >> > >> > conditions apply ;) >> > -- >> > regards, >> > Vineet Semwal >> > >> > >> > On Thu, Sep 17, 2009 at 4:13 AM, jWeekend >> > <[email protected]>wrote: >> > >> >> Since you can NOT do >> >> >> >> class S<T>{S(){T t = new T();}} // broken >> >> >> >> how would you create an object of type T somewhere in S? Think about >> this >> >> before you read on ... >> >> >> >> At the risk of reigniting the world-famous generics debates of >> >> yesteryear, >> >> just as our noble core-developers regroup to start work on making 1.5 >> >> even >> >> better than what is already the best Java web framework, I thought I'd >> >> share >> >> the idea I suggested to one of our developers who was having a bad day >> >> with >> >> generics (for several good reasons [1][2]) a couple of months ago, in >> >> case >> >> you can make use of it somewhere, or, find an even more convoluted >> >> solution >> >> - notice the innocent looking abstract modifier! >> >> // not real code >> >> // don't try this at home without adult supervision! >> >> public abstract class FunnyFactory<T> { >> >> private T instance = null; >> >> public T getInstance() { >> >> if (instance == null) { >> >> try { >> >> final ParameterizedType gsc = >> >> (ParameterizedType)getClass().getGenericSuperclass(); >> >> final Class<T> typeT = (Class<T>) >> >> gsc.getActualTypeArguments()[0]; >> >> instance = typeT.newInstance(); >> >> } catch (InstantiationException e) { >> >> e.printStackTrace(); >> >> } catch (IllegalAccessException e) { >> >> e.printStackTrace(); >> >> } >> >> } >> >> return instance; } >> >> } >> >> >> >> ... >> >> >> >> public class CreateInstanceOfTypeParameter { >> >> @Test >> >> public void testCreateInstanceOfTypeParameter() { >> >> FunnyFactory<Point> factory = new FunnyFactory<Point>() {}; >> >> factory .getInstance().x = 22; >> >> factory .getInstance().y = 47; >> >> assertEquals(new Point(22, 47), factory.getInstance()); >> >> } >> >> } >> >> >> >> Regards - Cemal jWeekend OO & Java Technologies, Wicket Training and >> >> Development http://jWeekend.com >> >> >> >> [1] http://gafter.blogspot.com/2006/11/reified-generics-for-java.html >> >> [2] >> >> >> http://weblogs.java.net/blog/arnold/archive/2005/06/generics_consid_1.html >> >> >> >> >> >> --------------------------------------------------------------------- >> >> To unsubscribe, e-mail: [email protected] >> >> For additional commands, e-mail: [email protected] >> >> >> >> >> > >> > >> >> -- >> View this message in context: >> http://www.nabble.com/-OT--More-%22Java%27s-generic-type-parameters-are-not-reified%22...-tp25482235p25487989.html >> Sent from the Wicket - User mailing list archive at Nabble.com. >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: [email protected] >> For additional commands, e-mail: [email protected] >> >> > > >
