Cast generic type T into Class<T>

Frithjof

How do I cast a generic type T into an object as Class<T>?

Something like Class<T> T.class or Class<T> T.getClass() does not work, because T is no variable.

A function should look like this:

public Class<T> getType() {
 return // Class of T
}

Background info: I have some classes Class1<T>, Class2<T>, ... which all extend ParentClass<T>. ParentClass has the function getType(). I'm looking for an more elegant way to get the type then just creating a variable Class<?> type and saving the type that way.

Arnaud Denoyelle

You don't know T at runtime (because of Type erasure).

Basically, MyWonderfulClass<String> will become MyWonderfulClass<Object> at runtime so you don't have any chance to retrieve it except if you directly give the class in the constructor or in a setter :

//class is a reserved keyword so I call it clazz instead.
public MyClass(Class<T> clazz) {
  this.clazz = clazz;
}

public Class<T> getType() {
  return this.clazz;
}

You can also force your subclasses to implement getType() : Class<T>.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to cast a generic object of type A<T> to A<T'>?

From Dev

C# - Cast generic interface to generic class that pass type <T> that is determined at runtime

From Dev

Cannot cast delegate to a generic type T

From Dev

Why can't I cast `this` to a generic type?

From Dev

Cast to generic type (T) is never checked by the compiler?

From Dev

Cast to generic type (T) gives "unchecked cast" warning

From Dev

C# cast generic T in abstract class<T> to dynamic

From Dev

Get type of Class from generic Class(of T)

From Dev

Cast generic T to Object[]

From Dev

Cast generic T to Object[]

From Dev

Cast to a type from a generic class

From Dev

Why is class of T not allowed in a generic type?

From Dev

Jackson DeSerialize Class with <T> (Generic) Type

From Dev

Why is class of T not allowed in a generic type?

From Dev

Generic Class:: Type Parameter <T> Property Inheritance

From Dev

Why can't I cast one instantiation of a generic type to another?

From Dev

How to cast a variable of specific type to generic T in Delphi?

From Dev

How to cast generic T to a TObject?

From Dev

Generic Function: Cast T[] to Complex[]

From Dev

How to identify the <T> type for Int,String,Generic Type or class

From Dev

Get the type of generic T

From Dev

Generic classes and Class<T>

From Dev

extending a class with a generic T

From Dev

Generic classes and Class<T>

From Dev

Why can't I create an array of an inner class of a generic type?

From Java

How do I get a class instance of generic type T?

From Java

How to get the type of T from a member of a generic class or method?

From Dev

How to trigger a Generic Class method recursively changing the type of T?

From Dev

Create a generic class with an array of type Comparable<T> in kotlin?

Related Related

  1. 1

    How to cast a generic object of type A<T> to A<T'>?

  2. 2

    C# - Cast generic interface to generic class that pass type <T> that is determined at runtime

  3. 3

    Cannot cast delegate to a generic type T

  4. 4

    Why can't I cast `this` to a generic type?

  5. 5

    Cast to generic type (T) is never checked by the compiler?

  6. 6

    Cast to generic type (T) gives "unchecked cast" warning

  7. 7

    C# cast generic T in abstract class<T> to dynamic

  8. 8

    Get type of Class from generic Class(of T)

  9. 9

    Cast generic T to Object[]

  10. 10

    Cast generic T to Object[]

  11. 11

    Cast to a type from a generic class

  12. 12

    Why is class of T not allowed in a generic type?

  13. 13

    Jackson DeSerialize Class with <T> (Generic) Type

  14. 14

    Why is class of T not allowed in a generic type?

  15. 15

    Generic Class:: Type Parameter <T> Property Inheritance

  16. 16

    Why can't I cast one instantiation of a generic type to another?

  17. 17

    How to cast a variable of specific type to generic T in Delphi?

  18. 18

    How to cast generic T to a TObject?

  19. 19

    Generic Function: Cast T[] to Complex[]

  20. 20

    How to identify the <T> type for Int,String,Generic Type or class

  21. 21

    Get the type of generic T

  22. 22

    Generic classes and Class<T>

  23. 23

    extending a class with a generic T

  24. 24

    Generic classes and Class<T>

  25. 25

    Why can't I create an array of an inner class of a generic type?

  26. 26

    How do I get a class instance of generic type T?

  27. 27

    How to get the type of T from a member of a generic class or method?

  28. 28

    How to trigger a Generic Class method recursively changing the type of T?

  29. 29

    Create a generic class with an array of type Comparable<T> in kotlin?

HotTag

Archive