Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36207
| 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 20:11 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <slrno87g3s.pvm.avl@logic.at> (permalink) |
| References | <39e83ab1-6a6a-4211-8f3b-0237d56d4202@googlegroups.com> <slrno87drj.pvm.avl@logic.at> <7049a3a0-7c09-41fa-97c0-82d54b06061e@googlegroups.com> |
Philipp Kraus <philipp.kraus.flashpixx@gmail.com> wrote:
> Am Samstag, 21. Januar 2017 20:35:07 UTC+1 schrieb Andreas Leitgeb:
>> 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.
>
> Thanks for your answer, I know that generic (or in C++ templates) are
> only exist on compile-time, but not on runtime.
You know partially wrong. It is true for Java, but C++ templates are
different. Unlike Java, C++ does know the type parameters at runtime:
it even creates a copy of the template code for each parameterization
(with some gory exceptions that I don't want to go deeper into, here).
> But the questian is, is there a possibility to pass the template
> argument to the class object. I'm working with Java 8.
Maybe my previous answer was "tl;dr", but it was meant to clarify: "no".
> For the compiler I need not a MyClass.class, but I need a
> MyClass<String>.class object, but if this is not possible
> can I do it without a type-cast?
No, you cannot specify MyClass<String>.class - Yes, you need to cast,
and yes, you can squelsh the generics-cast-warning, and you need not
even feel bad about it for this particular "class-factory" usecase.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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