Why is casting the class of a generic to Class unsafe?

后端 未结 3 1199
再見小時候
再見小時候 2021-01-13 05:24

I\'m making a MethodPointer class in order to simulate the functionality of function pointers from C++. At first, I was doing everything with just Object

3条回答
  •  我在风中等你
    2021-01-13 05:56

    The problem is that getClass() ignores type parameters.

    The Javadocs say:

    The actual result type is Class where |X| is the erasure of the static type of the expression on which getClass is called.

    For example, new ArrayList().getClass() returns a Class, not Class>.

    This also means that getClass() called on a type parameter decays to a Class.

提交回复
热议问题