Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36211

Re: class parameter with generic type on a factory

From Andreas Leitgeb <avl@logic.at>
Newsgroups comp.lang.java.programmer
Subject Re: class parameter with generic type on a factory
Date 2017-01-22 11:02 +0000
Organization A noiseless patient Spider
Message-ID <slrno8949c.pvm.avl@logic.at> (permalink)
References <39e83ab1-6a6a-4211-8f3b-0237d56d4202@googlegroups.com> <o61avt$tmt$1@gioia.aioe.org>

Show all headers | View raw


Arne Vajhøj <arne@vajhoej.dk> wrote:
> Some experimentation ended up with this:
>    public static interface MyInterface<T> ...
>    public static class MyClass<T> implements MyInterface<T> ...
>    public static class MyFactory<T> {
>       public MyFactory(Class<? extends MyClass<T>> clz) ...
>    }

Indeed, the factory method can specify a type-parameterized class for
its argument, which removes the "dirty" unchecked cast from inside the
factory, but there is no way to completely get rid of the unchcked cast.

>    public static void main(String[] args) throws [...] {
>       @SuppressWarnings("unchecked")
>       MyFactory<String> mf = new MyFactory<String>(
>          (Class<? extends MyClass<String>>)MyClass.class
>       );

Actually, I think leaving the cast inside the factory makes for cleaner
code.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

class parameter with generic type on a factory Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2017-01-21 08:19 -0800
  Re: class parameter with generic type on a factory Andreas Leitgeb <avl@logic.at> - 2017-01-21 19:33 +0000
    Re: class parameter with generic type on a factory Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2017-01-21 11:51 -0800
      Re: class parameter with generic type on a factory Andreas Leitgeb <avl@logic.at> - 2017-01-21 20:11 +0000
  Re: class parameter with generic type on a factory Arne Vajhøj <arne@vajhoej.dk> - 2017-01-21 23:04 -0500
    Re: class parameter with generic type on a factory Andreas Leitgeb <avl@logic.at> - 2017-01-22 11:02 +0000
      Re: class parameter with generic type on a factory Arne Vajhøj <arne@vajhoej.dk> - 2017-01-22 14:00 -0500
  Re: class parameter with generic type on a factory Marcel Mueller <news.5.maazl@spamgourmet.org> - 2017-01-22 09:14 +0100

csiph-web