Get class name with generic information

There is a problem in java serialization, I want to keep the name of some classes, and I have some problems with generic classes. For example:
- If I have ArrayList<String> listToDump = new ArrayList<String>();
- If I take the name: listToDump.getName();or listToDump.getCanonicalName();
- I will have java.util.ArrayListor ArrayList
- And I want to have java.util.ArrayList<String>orArrayList<String>

Any ideas on how I can do this?
Damien.

+3
source share
5 answers
  • The case you described may not work, maybe you listToDump.getClass().getName()

  • Unable to get runtime type information due to type erasure :

    , , - a , , .

  • , , , . , .

+6

listToDump - , Field.getGenericType(). Java - ArrayList. , , , , .

?

+3

- .

+2

, .

, , , , Java.

+1

I think that if you extract any element from List, try to extract its class name, you will get the result you are looking for.

+1
source

Source: https://habr.com/ru/post/1740934/


All Articles