Another related article by Gafter:

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

The unittest would then look like this.

public class CreateInstanceOfTypeParameter {
    @Test
    public void testCreateInstanceOfTypeParameter() {
      Point instance = new TypeReference<Point>() {}.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:[email protected]] 
Verzonden: donderdag 17 september 2009 0:44
Aan: [email protected]
Onderwerp: [OT] More "Java's generic type parameters are not reified"...

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.ht
ml


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


=============================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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to