Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding Java Type Erasure

Is there a way one could avoid type erasure and get access to a type parameter?

public class Foo<T extends Enum<?> & Bar> {
    public Foo() {
        // access the template class here?
        // i.e. :
        baz(T.class); // obviously doesn't work
    }

    private void baz(Class<T> qux) { 
        // do stuff like
        T[] constants = qux.getEnumConstants();
        ...
    } 
}

I need to know about T, and do things with it. Is it possible, and if so, how can it be done without passing in the class in the constructor or anywhere besides the parameter?

EDIT: The main purpose of this question is to find out if there is any practical way around type erasure.

like image 784
Chris Cashwell Avatar asked Dec 09 '11 23:12

Chris Cashwell


People also ask

Why does Java do type erasure?

The reason for using type erasure for this was to allow for backwards compatibility for migrating code and to allow for reuse of existing classes. The Java compiler uses type erasure to replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded.

What are the two 2 types of Erasure?

- Erasure is a type of alteration in document. It can be classified as chemical erasure and physical erasure.

What is the following class converted to after type erasure?

What is the following method converted to after type erasure? public static <T extends Comparable<T>> int findFirstGreaterThan(T[] at, T elem) { // ... } Answer: public static int findFirstGreaterThan(Comparable[] at, Comparable elem) { // ... }

What is type erasure and when would you use it?

Type-erasure simply means "erasing" a specific type to a more abstract type in order to do something with the abstract type (like having an array of that abstract type).


1 Answers

AFACT, there is no practical way around type erasure because you can't ask for something which the runtime doesn't have access to. Assuming of course you agree that sub-classing generic classes for each enum which implements Bar interface is a practical work around.

enum Test implements Bar {
    ONE, TWO
}

class Foo<T> extends FooAbstract<Test> {
    public Foo() {
        ParameterizedType genericSuperclass =
                (ParameterizedType) getClass().getGenericSuperclass();
        baz((Class<T>) genericSuperclass.getActualTypeArguments()[0]);
    }

    private void baz(Class<T> qux) {
        T[] constants = qux.getEnumConstants();
        System.out.println(Arrays.toString(constants)); // print [ONE, TWO]
    }
}

interface Bar {
}

class FooAbstract<T extends Enum<?> & Bar> {
}
like image 102
Sanjay T. Sharma Avatar answered Oct 01 '22 02:10

Sanjay T. Sharma