Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Andreas Leitgeb Newsgroups: comp.lang.java.programmer Subject: Re: class parameter with generic type on a factory Date: Sun, 22 Jan 2017 11:02:18 -0000 (UTC) Organization: A noiseless patient Spider Lines: 21 Message-ID: References: <39e83ab1-6a6a-4211-8f3b-0237d56d4202@googlegroups.com> Reply-To: avl@logic.at Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Sun, 22 Jan 2017 11:02:18 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="3c9ab00deec5f49fd49821abc74efa11"; logging-data="13166"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/62y2CKlQxuUVhX4MhKnDR" User-Agent: slrn/1.0.2 (Linux) Cancel-Lock: sha1:8g8+3vVmh0mxZ6wssX518r0/E94= Xref: csiph.com comp.lang.java.programmer:36211 Arne Vajhøj wrote: > Some experimentation ended up with this: > public static interface MyInterface ... > public static class MyClass implements MyInterface ... > public static class MyFactory { > public MyFactory(Class> 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 mf = new MyFactory( > (Class>)MyClass.class > ); Actually, I think leaving the cast inside the factory makes for cleaner code.