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


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

Re: class parameter with generic type on a factory

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: class parameter with generic type on a factory
Date 2017-01-21 23:04 -0500
Organization Aioe.org NNTP Server
Message-ID <o61avt$tmt$1@gioia.aioe.org> (permalink)
References <39e83ab1-6a6a-4211-8f3b-0237d56d4202@googlegroups.com>

Show all headers | View raw


On 1/21/2017 11:19 AM, Philipp Kraus wrote:
> I have git an interface
>
> interface MyInterface<T> {}
>
> based on this Interface I have a class
>
> public MyClass<T> implements MyInterface<T> {}
>
> and a factory class which generates MyInterface<T> objects with a Class parameter at the constructor
>
> class MyFactory<T>
> {
>      private final Constructor<? extends MyInterface<T>> m_ctor;
>
>     public MyFactor( final Class<? extends MyInterface<T>> p_class )
>     {
>             m_ctor = p_class.getConstructor();
>     }
>
> }
>
> If I try to create an object of the factory with
>
> new MyFactor<String>( MyClass.class );
>
> I get an error that MyClass.class is from the wrong type, the parameter should be a class from the type MyInterface<T>.
>
> So I try to modify the ctor of the factory:
>
> public <G extends MyInterface<T> MyFactor( final Class<G> 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<T> {
		public T giveMe();
	}
	public static class MyClass<T> implements MyInterface<T> {
		public T giveMe() {
			return null; // not easy to create a T but that is another problem
		}
	}
	public static class MyFactory<T>
	{
	     private final Constructor<? extends MyClass<T>> ctor;
	     public MyFactory(Class<? extends MyClass<T>> clz) throws 
NoSuchMethodException, SecurityException
	     {
	            ctor = clz.getConstructor();
	     }
	     public MyInterface<T> 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<String> mf = new MyFactory<String>((Class<? extends 
MyClass<String>>) MyClass.class);
		MyInterface<String> mi = mf.create();
		String s = mi.giveMe();
		System.out.println(s == null);
	}
}

Arne

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