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


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

Re: class parameter with generic type on a factory

From Marcel Mueller <news.5.maazl@spamgourmet.org>
Newsgroups comp.lang.java.programmer
Subject Re: class parameter with generic type on a factory
Date 2017-01-22 09:14 +0100
Organization MB-NET.NET for Open-News-Network e.V.
Message-ID <o61plf$5m2$1@gwaiyur.mb-net.net> (permalink)
References <39e83ab1-6a6a-4211-8f3b-0237d56d4202@googlegroups.com>

Show all headers | View raw


On 21.01.17 17.19, Philipp Kraus wrote:
> 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();
>      }
>
> }

I had a slightly similar problem some time ago and ended up like that:

<T> T JNDILookup(Class<T> type, String name) throws Event
{	try
	{	return type.cast(jndiContext.lookup(name));
	} catch (NamingException e)
	{	throw new Event(Events.EVF_JNDIObjectNotFound, name, type.getName(), 
jndiUrl, ExceptionFormatter.toString(e));
	} catch (ClassCastException e)
	{	throw new Event(Events.EVF_JNDIObjectWrongType, name, type.getName(), 
jndiUrl, ExceptionFormatter.toString(e));
	}
}

Call:

JNDILookup(QueueConnectionFactory.class, connectionName)

But no further type checks on T there because the executing code must 
not depend on the actual type in Java and AFAIK Java cannot deduce more 
than one type argument from a single function argument, i.e. your class 
name and the interface type.


> 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
> The problem seems to be with the generic parameter.
>
> How can I solve this problem?

Well, choose another language without type erasure. :-(
This is e.g. straight forward in .NET, although it uses reflection 
internally to create the instances in this case (slow).


Note also that this cyclic dependencies of a generic interface (or an 
abstract super class) on the implementing class break inheritance.

class A implements MyInterface<A>
{...}

class B extends A
{...}

The MyInterface implementation is broken for B because if misses the 
constraint MyInterface<B>.
This is probably related to the problem that Java has with Class<? 
extends MyInterface<T>>.


Marcel

Back to comp.lang.java.programmer | Previous | Next — Previous 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