Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: class parameter with generic type on a factory Date: Sun, 22 Jan 2017 14:00:03 -0500 Organization: Aioe.org NNTP Server Lines: 30 Message-ID: References: <39e83ab1-6a6a-4211-8f3b-0237d56d4202@googlegroups.com> NNTP-Posting-Host: PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.6.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:36216 On 1/22/2017 6:02 AM, Andreas Leitgeb wrote: > 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. It was the best that my limited creativity and the Java compiler could agree on. :-) Arne