Chandra Sekar
Chandra Sekar

Reputation: 10843

Create Parameterized Type of another class from existing ParameterizedType

Consider this class structure.

class Class1<T> {
    Class2<T> field1;
}

If I have a ParameterizedType instance representing Class1<String> through reflection, how can I get/create a ParameterizedType instance representing Class2<String>?

Upvotes: 0

Views: 229

Answers (2)

newacct
newacct

Reputation: 122439

ParameterizedType is an interface. Simply write a class that implements it (or copy some existing implementation from the Internet) so that its methods return what you want.

Upvotes: 1

Ben Schulz
Ben Schulz

Reputation: 6181

In its current state the reflection API allows for (limited) inspection of generic type information. Since one could not leverage the newly constructed ParameterizedType there is little point in allowing its construction in the first place.

If you are building an API accepting ParameterizedTypes and need users to construct their own, you might consider switching to guava's TypeToken-Class instead.

Upvotes: 0

Related Questions