Java Generics Nested Type Paramaters - java

I have a class defined like so:
public class AddRecordsToolbar<D extends IDataSource<T>, T extends Serializable>
extends AbstractToolbar<D, T>
which my IDE IntelliJ IDEA declares as legal. It looks and feels wrong to me.
I want to declare it like this:
public class AddRecordsToolbar<D extends IDataSource<T extends Serializable>, T>
extends AbstractToolbar<D, T>
however that syntax is illegal thanks to something to do with Javas type erasure.
D extends IDataSource<T> is required by the superclass.
My Class is using Serializable to do a deep copy. Hence the T extends Serializable.
So now on to the Question: If I specify T extends Serializable as the second type parameter for my class will it still enforce T extends Serializable for D as well?

Answering to your question, yes its do.
The order of generic parameter it only in your mind.
If we would rephrase that implementation to:
public class AddRecordsToolbar<T extends Serializable, D extends IDataSource<T>> extends AbstractToolbar<D, T>
you will be not so surprised, and looks that the way it should be.
I will try to find the explanation for this in Java Language Specification (when it will work) but for now that the way it is.

I believe that this is addressed in section 8.1.2 of the java language spec:
The scope of a class' type parameter is the entire declaration of the
class including the type parameter section itself. Therefore, type
parameters can appear as parts of their own bounds, or as bounds of
other type parameters declared in the same section.

Yes. The "T" in the second parameter is the same T as in the first. Nothing world work, otherwise.
This means that
class IFoo extends IDataSource<String>{};
AddRecordsToolbar<IFoo, Integer> x;
Is illegal. Integer and String are both serialisable, but the declaration of AddRecordsToolbar says that the data source has to be a source of the data type in your second parameter. And that second parameter says that it has to be serializable.

Related

Get the generic type of a generic type

I apologize in advance, I'm sure this has been asked before, but I'm a self-learned programmer so I can't for the life of me figure out the terms I need to search for.
I have a generic class:
public abstract class Directory<T extends Key>
It is a generic parameter of another class:
public class TagDescriptor<T extends Directory, U extends Key>
The U of TagDescriptor will always be the generic parameter of the Directory passed as the first parameter. It's fine to pass the T, but is there no way to infer the U? I've tried things like:
public class TagDescriptor<T extends Directory<U>>
But that's incorrect. At compile time it should know that Directory has a generic; can I leverage that?
You can declare multiple types parameters bounded with self-reference:
public class TagDescriptor<K extends Key, T extends Directory<K>>
NB: K is commonly used, as a name, for key types.
As for, why is it necessary to declare the type parameter K? Well it's quite simple : because otherwise the compiler has no way to tell what K is : a concrete class, an interface named K ? Or a type parameter named K ?
For instance, this would be perfectly legal:
public interface K {}
public class TagDescriptor<T extends Directory<K>>

Get Generic Type of Generic Type

If I have
public abstract class SomeType<T extends SomeOtherType> {}
public class WrapperType<U extends SomeType<T>> {}
Is there a way to modify the latter signature (which is an error 'cannot resolve symbol "T"') so that I can use the generic type in WrapperType, say for List<T> or something analogous?
(If I have posed this question using the wrong terminology, I'd appreciate the corrections.)
It's quite simple once you realize what's going on. You didn't define T in the scope of the class WrapperType. Declare it with at least the same bounds as in SomeType.
class WrapperType<T extends SomeOtherType, U extends SomeType<T>> {}

In Java, can I inherit a class supplied by a type parameter?

In java, can I do something -more or less- like this? and how?
public class SomeGenericClass<T> extends T{
}
I think this is the relevant quote from the JLS (§8.1.4):
The ClassType [provided in the extends clause] must name an accessible (§6.6) class type, or a compile-time error occurs.
(The bit about accessibility isn't relevant).
A class type is not the same thing as a type variable (§4.3) (which T is), so attempting to do this would be a compile-time error.
you can't do public class SomeGenericClass<T> extends T for multiple reasons:
Type erasure will erase T to Object., and it would be pointless to say public class SomeGenericClass<T> extends Object ,as Object is already the base class of all types.
Imagine if it were allowed and i did something like public class SomeGenericClass<T extends Comparable> extends T , type erasure will turn this into public class SomeGenericClass<Comparable> extends Comparable but interface is implemented not extended .
public class SomeGenericClass<T> extends T{
}
You must understand Generics better. SomeGenericClass is a template (C++ terminology). As it is, it means nothing to compiler. You cannot write
SomeGenericClass<T> obj = new SomeGenericClass<T>() // this is invalid
Instead you need to supply the type attribute. This information complements the above answer provided by #Andy Turner

Class instance of generics type T BUT which extends a super class with generics

I've use this method to retrieve the type of my generic, and no problem... until now
Now, I've got this kind of structure:
public class Toto<T> extends OtherOne<X,Y,Z> {...}`
and when I use the
((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];`
it's return me the type Class<X> of my OtherOne class, not Class<T>!
Has anyone ever encountered this problem? Whatever I've tried didn't work, and I don't want to use this solution of getting the class as an argument of my Constructor.
In order to learn what T is it will have to be captured in a subclass. Ex:
public class StringToto extends Toto<String> {}
Class<?> t = (Class<?>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
This is the case since types are only retrievable at runtime if they're part of a type definition.

Java generics for Enum types

I am trying to write a Java class, one part of which requires a mapping of the values of an unknown enum to another class. My class contains a field private Map<? extends Enum, Unit> myMap and is initialized with a factory method createMyClass:
public static MyClass <T extends Enum> myClass createMyClass(Class<T> x) {
MyClass theClass = new MyClass() //constructor is private
//...
myMap = new HashMap<T, Unit>();
for(T t : x.getEnumConstants())
myMap.put(t, theClass.new Unit(t));
//...
}
The class Unit is (and needs to be, as far as I can tell) an inner class of MyClass. When I put this into NetBeans it complains with this message:
method put in interface java.util.Map<K,V> cannot be applied to given types
required: capture #4 of ? extends java.lang.Enum, MyClass.Unit
found: T, MyClass.Unit
I understand (or at least I think I do) how the Collections need to be very careful about wildcard usage to preserve type safety, but I can't think of how T extends Enum fails to match ? extends Enum.
Try changing your method declaration to:
public static <T extends Enum<T>> MyClass createMyClass(Class<T> x) {
}
What Map<? extends Enum, Unit> means is "The key is some specific class that extends Enum, but I don't known which one". And since you don't know which class it is, you cannot add anything to such a Map, you can only get elements from it and be certain they are Enums.
You should probably just declare that field as Map<Enum, Unit> - it will allow all Enum subclasses just fine.
This seems to be the most common misunderstanding about Java enums by a huge margin - people see that ? wildcard and think they have to use it to allow subclasses.
You can read this Generics Tutorial. It explains why this doesn't work in section 4 (Wildcards).
As far as I understand, ? extends Enum can be any enum, not just a T or subclass of T.

Categories