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: Sat, 21 Jan 2017 23:04:12 -0500 Organization: Aioe.org NNTP Server Lines: 92 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: 7bit 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:36208 On 1/21/2017 11:19 AM, Philipp Kraus wrote: > I have git an interface > > interface MyInterface {} > > based on this Interface I have a class > > public MyClass implements MyInterface {} > > and a factory class which generates MyInterface objects with a Class parameter at the constructor > > class MyFactory > { > private final Constructor> m_ctor; > > public MyFactor( final Class> p_class ) > { > m_ctor = p_class.getConstructor(); > } > > } > > If I try to create an object of the factory with > > new MyFactor( MyClass.class ); > > I get an error that MyClass.class is from the wrong type, the parameter should be a class from the type MyInterface. > > So I try to modify the ctor of the factory: > > public MyFactor( final Class p_class ) > { > m_ctor = p_class.getConstructor(); > } > > but I get the error on compiling: > > reason: cannot infer type-variable(s) T,G (actual and formal argument lists differ in length) > > I have tried it also with an abstract class, which will be used instead of the class MyInterface. > > So my goal is to create flexible factory, which get through the ctor > the class which will be constructed, but I will restrict the > constructed objects of an interface I am not sure that there is a good solution. :-( Some experimentation ended up with this: import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class GenFun { public static interface MyInterface { public T giveMe(); } public static class MyClass implements MyInterface { public T giveMe() { return null; // not easy to create a T but that is another problem } } public static class MyFactory { private final Constructor> ctor; public MyFactory(Class> clz) throws NoSuchMethodException, SecurityException { ctor = clz.getConstructor(); } public MyInterface create() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { return ctor.newInstance(new Object[0]); } } public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { @SuppressWarnings("unchecked") MyFactory mf = new MyFactory((Class>) MyClass.class); MyInterface mi = mf.create(); String s = mi.giveMe(); System.out.println(s == null); } } Arne