0

geniuses.

I'm using the following code for extracting user-defined extract (T).

public class Extractor<T extends MostSimpleExtract> {

    private final Constructor<? extends T> extractConstructor;

    public Extractor(Class<T> ExtractClass) throws NoSuchMethodException {
        this.extractConstructor = ExtractClass.getConstructor();
    }

}

So I have to instantiate my Extractor like:

Extractor<MostSimpleExtract> extractor = new Extractor<>(MostSimpleExtract.class);

But it I do not need to pass a class argument (MostSimpleExtract.class) to use ArrayList:

ArrayList<MostSimpleExtract> extractList = new ArrayList<>();

Passing a class argument to a constructor seems redundant because the class information is already there inside brackets.

How should I declare the class to get a constructor using generic type T without passing class argument to a constructor?

edit:

What I meant was that if there is any way to get constructor without passing a class argument, thus, I want to instantiate my Extractor class like:

Extractor<MostSimpleExtract> extractor = new Extractor<>();
ghchoi
  • 4,812
  • 4
  • 30
  • 53
  • Possible duplicate of [How to get a class instance of generics type T](http://stackoverflow.com/questions/3437897/how-to-get-a-class-instance-of-generics-type-t) – shmosel Feb 10 '17 at 03:35
  • Because it's crazy frustrating; type erasure (the effect you're seeing, where the type information is not available at runtime) was introduced for backwards compatability – Richard Tingle Feb 10 '17 at 08:19
  • @Richard Tingle So is it impossible to declare a generic class like ArrayList? – ghchoi Feb 11 '17 at 05:44

1 Answers1

1

At the compile time you will not have any information about generic type(T). The Java compiler also erases type parameters in generic method arguments and in constructors.(https://docs.oracle.com/javase/tutorial/java/generics/genMethods.html)

The compiled generic code actually just uses java.lang.Object wherever you talk about T

I think, you have no choice and you should pass a class argument to a constructur.

i.merkurev
  • 465
  • 2
  • 8
  • The `Class` that gives one runtime access to generics type information is called a "runtime type token` (RTTT). It's the genius of Java that generics are used only at compile time out of the box, but you can still manually add a RTTT for those few times when you need type assertions at runtime. – Lew Bloch Feb 10 '17 at 07:14
  • 2
    @GyuHyeon pre generics there was just ArrayList. So people did this `(String)myArrayList.get(i)`. This works fine but isn't typesafe. So generics were introduced, but due to type erasure at runtime it returns to the casting version. So you can see at runtime the array list doesn't know what it contains beyond `Object` (but at compile time it was validated that only strings could get into it) – Richard Tingle Feb 10 '17 at 08:23
  • @RichardTingle that is likely the most concise explanation of type erasure I've ever seen. – errantlinguist Feb 10 '17 at 08:46