mercredi 6 mars 2019

How to check with reflection type of generic class

I have interface for implement Stack data structure:

public interface Stack<T> {
    void push(T element);
    //other methods
}

And one or more implementation where it's implemented.

Аnd I want to write a tester for this, where any of exemplar of this class may be tested. For example:

class StackImpl<T> {
//...
}

For call test i want write this:

Tester.<Integer>test(new StackImpl<Integer>());

And in tester this:

public static <T> void test(Object any) {
        if (any instanceof Stack) {
            testQueue((Stack<T>) any);
        }
    }

    private static<T> void testStack(Stack<T> stack) {
        new TestStack<>(stack).invoke();
    }

And test stack:

public class TestStack<T> implements Test {

    private Stack<T> stack;

    TestStack(Stack<T> stack) {
        this.stack = stack;
    }

    @Override
    public void invoke() {
        //and here need help!
    }
}

And in method invoke() i want get Original generic type, in this case is Integer.

How to get need value for implement test?





Aucun commentaire:

Enregistrer un commentaire