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


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

Re: class parameter with generic type on a factory

From Andreas Leitgeb <avl@logic.at>
Newsgroups comp.lang.java.programmer
Subject Re: class parameter with generic type on a factory
Date 2017-01-21 19:33 +0000
Organization A noiseless patient Spider
Message-ID <slrno87drj.pvm.avl@logic.at> (permalink)
References <39e83ab1-6a6a-4211-8f3b-0237d56d4202@googlegroups.com>

Show all headers | View raw


Philipp Kraus <philipp.kraus.flashpixx@gmail.com> wrote:
> I have got 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
> [...]
> If I try to create an object of the factory with
> new MyFactor<String>( MyClass.class );

It is important to understand that in Java generics are primarily a
compile-time thing. If the compiler can see that all types fit, then it
can guarantee that no ClassCastException will possibly surprise you at
runtime, (except where you do an explicit cast).

Now, if you pass MyClass.class to the factory, there is no way for
the *compiler* to know that these instances will consistently have
the used factory's type param as their type param.

*You* on the other hand *can* tell for sure, that the instances you
create will only ever be used with the factory's type parameter, like
String.

This is the situation, where it is ok to "help" the compiler and tell
it that the instance you create (which originally is of type MyClass<?>,
and thus only implements MyInterface<?>) can safely be assumed to be
always e.g. a MyClass<String> (if String was the used factory's type
param).

You do this by using the annotation @SuppressWarnings("unchecked")
on the method that calls newInstance on the provided constructor and
casts(!) the MyClass<?> to MyClass<T> (where T here is the factory's
type param). It's then your responsibility that no ref to such an 
instance will ever be cast to a different type parameter, but that's
not likely to happen in your factory method, anyway.

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