Re: [OT] More "Java's generic type parameters are not reified"...

2009-09-17 Thread vineet semwal
and using it in this way
Funnier funnier=new Funnier(Foo.class,Bar.class);
or
Funny funny=new Funny(Foo.class);


-- 
regards,
Vineet Semwal

On Thu, Sep 17, 2009 at 8:16 PM, vineet semwal
wrote:

> I should have stated it clearly ..
> i actually meant  passing type Class in constructor itself like in
> following code:-
>
>   class Funny {
> private ClasstypeClass;
> public Funny(ClasstypeClass){
> this.typeClass=typeClass;
>
> }
> }
> class Funnier extends Funny{
> private ClasstypeClassT;
> private ClasstypeClassR;
> public Funnier( ClasstypeClassT,ClasstypeClassR){
> super(typeClassT);
> this.typeClassT=typeClassT;
> this.typeClassR=typeClassR;
>
> }
> }
>
>
> --
> regards,
> Vineet Semwal
>
>
> On Thu, Sep 17, 2009 at 2:58 PM, jWeekend 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
>> > wrote:
>> >
>> >> Since you can NOT do
>> >>
>> >> class S{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 {
>> >>  private T instance = null;
>> >>  public T getInstance() {
>> >>   if (instance == null) {
>> >> try {
>> >>   final ParameterizedType gsc =
>> >> (ParameterizedType)getClass().getGenericSuperclass();
>> >>   final Class typeT = (Class)
>> >> 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 factory = new FunnyFactory() {};
>> >> 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: users-unsubscr...@wicket.apache.org
>> >> For additional commands, e-mail: users-h...@wicket.apache.org
>> >>
>> >>
>> >
>> >
>>
>> --
>> 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: users-unsubscr...@wicket.apache.org
>> For additional commands, e-mail: users-h...@wicket.apache.org
>>
>>
>
>
>


Re: [OT] More "Java's generic type parameters are not reified"...

2009-09-17 Thread vineet semwal
I should have stated it clearly ..
i actually meant  passing type Class in constructor itself like in following
code:-

  class Funny {
private ClasstypeClass;
public Funny(ClasstypeClass){
this.typeClass=typeClass;

}
}
class Funnier extends Funny{
private ClasstypeClassT;
private ClasstypeClassR;
public Funnier( ClasstypeClassT,ClasstypeClassR){
super(typeClassT);
this.typeClassT=typeClassT;
this.typeClassR=typeClassR;

}
}


-- 
regards,
Vineet Semwal

On Thu, Sep 17, 2009 at 2:58 PM, jWeekend 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
> > wrote:
> >
> >> Since you can NOT do
> >>
> >> class S{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 {
> >>  private T instance = null;
> >>  public T getInstance() {
> >>   if (instance == null) {
> >> try {
> >>   final ParameterizedType gsc =
> >> (ParameterizedType)getClass().getGenericSuperclass();
> >>   final Class typeT = (Class)
> >> 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 factory = new FunnyFactory() {};
> >> 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: users-unsubscr...@wicket.apache.org
> >> For additional commands, e-mail: users-h...@wicket.apache.org
> >>
> >>
> >
> >
>
> --
> 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: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>


RE: [OT] More "Java's generic type parameters are not reified"...

2009-09-17 Thread jWeekend

Yes indeed! This (see Neal Gafter's code below) is neat and getting much
closer to being actually usable, as long as its implementation is hidden
well away. I'd probably mark some of those methods final too (it's a shame
the class itself can't be - a subclasser could mess up the type parameter
list, as per Igor's post).

public abstract class TypeReference 
private final Type type;
private volatile Constructor constructor;

protected TypeReference() {
Type superclass = getClass().getGenericSuperclass();
if (superclass instanceof Class) {
throw new RuntimeException("Missing type parameter.");
}
this.type = ((ParameterizedType)
superclass).getActualTypeArguments()[0];
}

/**
 * Instantiates a new instance of {...@code T} using the default, no-arg
 * constructor.
 */
@SuppressWarnings("unchecked")
public T newInstance() throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException,
InstantiationException {
if (constructor == null) {
Class rawType = type instanceof Class ? (Class) type :
(Class) ((ParameterizedType) type)
.getRawType();
constructor = rawType.getConstructor();
}
return (T) constructor.newInstance();
}

/**
 * Gets the referenced type.
 */
public Type getType() {
return this.type;
}

public static void main(String[] args) throws Exception {
List l1 = new TypeReference>() {
}.newInstance();
List l2 = new TypeReference() {
}.newInstance();
}
}

Regards - Cemal 
jWeekend 
OO & Java Technologies, Wicket Training and Development 
http://jWeekend.com




Minto.van.der.Sluis wrote:
> 
> Another related article by Gafter:
> 
> http://gafter.blogspot.com/2006/12/super-type-tokens.html
>  
> class S{
>  S() {
>// T t = new T();
>T t = new TypeReference() {}.newInstance();
>  }
> }
> 
> The unittest would then look like this.
> 
> public class CreateInstanceOfTypeParameter {
> @Test
> public void testCreateInstanceOfTypeParameter() {
>   Point instance = new TypeReference() {}.newInstance();
>   instance.x = 22;
>   instance.y = 47;
>   assertEquals(new Point(22, 47), instance);
> }
> }
> 
> Looks pretty powerful to me ;-)
> 
> Regards
> 
> misl
> 
> -Oorspronkelijk bericht-
> Van: jWeekend [mailto:jweekend_for...@cabouge.com] 
> Verzonden: donderdag 17 september 2009 0:44
> Aan: users@wicket.apache.org
> Onderwerp: [OT] More "Java's generic type parameters are not reified"...
> 
> Since you can NOT do
>  
> class S{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 {
>   private T instance = null;
>   public T getInstance() {
> if (instance == null) {
>   try {
> final ParameterizedType gsc =
>   (ParameterizedType)getClass().getGenericSuperclass();
> final Class typeT = 
>   (Class) 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 factory = new FunnyFactory() {};
>   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.ht
> ml
> 
> 
> 

RE: [OT] More "Java's generic type parameters are not reified"...

2009-09-17 Thread Sluis, M. van der (Minto)
Another related article by Gafter:

http://gafter.blogspot.com/2006/12/super-type-tokens.html
 
class S{
 S() {
   // T t = new T();
   T t = new TypeReference() {}.newInstance();
 }
}

The unittest would then look like this.

public class CreateInstanceOfTypeParameter {
@Test
public void testCreateInstanceOfTypeParameter() {
  Point instance = new TypeReference() {}.newInstance();
  instance.x = 22;
  instance.y = 47;
  assertEquals(new Point(22, 47), instance);
}
}

Looks pretty powerful to me ;-)

Regards

misl

-Oorspronkelijk bericht-
Van: jWeekend [mailto:jweekend_for...@cabouge.com] 
Verzonden: donderdag 17 september 2009 0:44
Aan: users@wicket.apache.org
Onderwerp: [OT] More "Java's generic type parameters are not reified"...

Since you can NOT do
 
class S{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 {
  private T instance = null;
  public T getInstance() {
if (instance == null) {
  try {
final ParameterizedType gsc =
  (ParameterizedType)getClass().getGenericSuperclass();
final Class typeT = 
  (Class) 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 factory = new FunnyFactory() {};
  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.ht
ml


-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org


=DISCLAIMER

De informatie in deze e-mail is vertrouwelijk en uitsluitend bestemd 
voor de geadresseerde. Indien u niet de geadresseerde bent, wordt u 
er hierbij op gewezen, dat u geen recht heeft kennis te nemen van de 
rest van deze e-mail, deze te gebruiken, te kopieren of te verstrekken
aan andere personen dan de geadresseerde. Indien u deze e-mail 
abusievelijk hebt ontvangen, brengt u dan alstublieft de afzender 
op de hoogte, waarbij u bij deze gevraagd wordt het originele bericht 
te vernietigen. Politie Amsterdam-Amstelland is niet verantwoordelijk 
voor de inhoud van deze e-mail en wijst iedere aansprakelijkheid af 
voor en/of in verband met alle gevolgen en/of schade van een onjuiste 
of onvolledige verzending ervan. Tenzij uitdrukkelijk het tegendeel 
blijkt, kunnen aan dit bericht geen rechten worden ontleend. Het 
gebruik van Internet e-mail brengt zekere risico?s met zich mee. 
Daarom wordt iedere aansprakelijkheid voor het gebruik van dit medium 
door de Politie Amsterdam-Amstelland van de hand gewezen.

-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: [OT] More "Java's generic type parameters are not reified"...

2009-09-17 Thread jWeekend

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
> wrote:
> 
>> Since you can NOT do
>>
>> class S{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 {
>>  private T instance = null;
>>  public T getInstance() {
>>   if (instance == null) {
>> try {
>>   final ParameterizedType gsc =
>> (ParameterizedType)getClass().getGenericSuperclass();
>>   final Class typeT = (Class)
>> 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 factory = new FunnyFactory() {};
>> 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: users-unsubscr...@wicket.apache.org
>> For additional commands, e-mail: users-h...@wicket.apache.org
>>
>>
> 
> 

-- 
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: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: [OT] More "Java's generic type parameters are not reified"...

2009-09-17 Thread jWeekend

... or, if you subclass the subclass ...
In the form I gave it's very fragile - just a trick really, but it tells you
a bit about what is actually in the bytecode and contradicts (as
conclusively as using javap) what some would have you believe.

Regards - Cemal 
jWeekend OO & Java Technologies, Wicket Training and Development 
http://jWeekend.com



igor.vaynberg wrote:
> 
> what happens if down the line the index of the type changes?: )
> 
> class FunnierFactory extends FunnyFactory
> 
> -igor
> 
> On Wed, Sep 16, 2009 at 3:43 PM, jWeekend 
> wrote:
>> Since you can NOT do
>>
>> class S{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 {
>>  private T instance = null;
>>  public T getInstance() {
>>   if (instance == null) {
>>     try {
>>       final ParameterizedType gsc =
>>         (ParameterizedType)getClass().getGenericSuperclass();
>>       final Class typeT =         (Class)
>> 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 factory = new FunnyFactory() {};
>>     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: users-unsubscr...@wicket.apache.org
>> For additional commands, e-mail: users-h...@wicket.apache.org
>>
>>
> 
> -
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/-OT--More-%22Java%27s-generic-type-parameters-are-not-reified%22...-tp25482235p25487838.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: [OT] More "Java's generic type parameters are not reified"...

2009-09-17 Thread Igor Vaynberg
what happens if down the line the index of the type changes?: )

class FunnierFactory extends FunnyFactory

-igor

On Wed, Sep 16, 2009 at 3:43 PM, jWeekend  wrote:
> Since you can NOT do
>
> class S{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 {
>  private T instance = null;
>  public T getInstance() {
>   if (instance == null) {
>     try {
>       final ParameterizedType gsc =
>         (ParameterizedType)getClass().getGenericSuperclass();
>       final Class typeT =         (Class)
> 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 factory = new FunnyFactory() {};
>     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: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>

-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: [OT] More "Java's generic type parameters are not reified"...

2009-09-17 Thread vineet semwal
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 wrote:

> Since you can NOT do
>
> class S{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 {
>  private T instance = null;
>  public T getInstance() {
>   if (instance == null) {
> try {
>   final ParameterizedType gsc =
> (ParameterizedType)getClass().getGenericSuperclass();
>   final Class typeT = (Class)
> 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 factory = new FunnyFactory() {};
> 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: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>


[OT] More "Java's generic type parameters are not reified"...

2009-09-16 Thread jWeekend

Since you can NOT do

class S{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 {
 private T instance = null;
 public T getInstance() {
   if (instance == null) {
 try {
   final ParameterizedType gsc =
 (ParameterizedType)getClass().getGenericSuperclass();
   final Class typeT = 
 (Class) 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 factory = new FunnyFactory() {};
 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: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org