How to determine the class of a generic type?

Jaap Coomans :

I'm creating a generic class and in one of the methods I need to know the Class of the generic type currently in use. The reason is that one of the method's I call expects this as an argument.

Example:

public class MyGenericClass<T> {
  public void doSomething() {
    // Snip...
    // Call to a 3rd party lib
    T bean = (T)someObject.create(T.class);
    // Snip...
  }
}

Clearly the example above doesn't work and results in the following error: Illegal class literal for the type parameter T.

My question is: does someone know a good alternative or workaround for this?

Nicolas :

Still the same problems : Generic informations are erased at runtime, it cannot be recovered. A workaround is to pass the class T in parameter of a static method :

public class MyGenericClass<T> {

    private final Class<T> clazz;

    public static <U> MyGenericClass<U> createMyGeneric(Class<U> clazz) {
        return new MyGenericClass<U>(clazz);
    }

    protected MyGenericClass(Class<T> clazz) {
        this.clazz = clazz;
    }

    public void doSomething() {
        T instance = clazz.newInstance();
    }
}

It's ugly, but it works.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to return the Class of a generic type

How to determine the type parameter of Bean implementing generic FunctionalInterface with a lambda?

Java Generic Class - Determine Type

How can I determine the type of a generic field in Java?

How to force type on return value in a generic class?

How to force type on return value in a generic class?

How to get Class for generic type?

How to get class of generic type when there is no parameter of it?

How to determine if a type implements a specific generic interface type

How to use a helper generic of type class of type

How to determine type in generic class VB.NET

How to get the runtime class of a generic type in Scala?

How to determine if the property belongs to Base class or sub class dynamically in generic type using reflection?

How to get class of generic type parameter in Kotlin

How to work with generic type array and generic type class as parameter in constructor?

Determine if a class member's type was defined as a generic parameter

Generic type parameters C# - How to generic class return type

How to determine the type of constructor based on the passed class?

How to pass a generic type object to a class constructor

How to define a generic type that is not a class?

How to get Class<?> object of a generic type

How to return class type of a generic class with generic?

How to infer class from generic type in Java?

how to write a java method with generic class type?

How to get (generic) type from class object

Determine if object is an instance of a generic base class, any generic type

Dart: how to determine nullable generic type at runtime?

how to reference the Type in the Concrete class of a generic class

How to wrap a class that has generic type methods with a class that has a generic type and no generic type arguments on the methods?