0

Is this the only way to get the class of a generic collection at runtime?

Class<T> clazz = new HashSet<Task>() {
            private static final long serialVersionUID = 8252429191034335636L;
        }.getClass()

My IDE says:

"The serializable class does not declare a static final serialVersionUID field of type long"

nimo23
  • 5,170
  • 10
  • 46
  • 75
  • 1
    may be for part of question https://stackoverflow.com/questions/15477169/java-should-serializable-inner-anonymous-classes-have-serialversionuid – Ryuzaki L Aug 02 '19 at 08:48
  • 1
    Your question is not clear in terms of what you're trying to achieve. Serialization and type erasure are not strictly related. The reason you can get (some) data about the generic parameter is because you created a (anonymous) subclass, not because of serialization. – lscoughlin Aug 02 '19 at 08:58
  • okay, as suggested, I leave the serialVersionUID within my inner class. – nimo23 Aug 02 '19 at 09:00
  • @Iscoughlin I only want to get the class of a generic collection at runtime. So I cannot find an easier way to do..or is there a better way (independent of serialization)? – nimo23 Aug 02 '19 at 09:02
  • Why don't you just use a regular `HashSet` (that is serializable) – Maurice Perry Aug 02 '19 at 09:29
  • 1
    Note that when you serialize an instance of an inner class, the outer class instance will also be serialized. Does your outer class also have a `serialVersionUID`? – Maurice Perry Aug 02 '19 at 09:33
  • @MauricePerry I need the "type information" (Task.class) of the hashSet. – nimo23 Aug 02 '19 at 09:43
  • @MauricePerry no the outer class has no serializable..I guess, I will delete the serializable field of my inner class..it works withouth..I dont know why I need it.. – nimo23 Aug 02 '19 at 09:44
  • @nimo23 what do you mean by "type information"? the generic type? you're not going to obtain it this way. – Maurice Perry Aug 02 '19 at 10:43
  • @MauricePerry sure, `new HashSet() {}.getClass()` returns the needed class information. Any other way? – nimo23 Aug 02 '19 at 10:45
  • @nimo23 you could, for instance, declare a static field of type `HashSet` (no instanciation needed), and then get the genericType of that field. – Maurice Perry Aug 02 '19 at 10:52
  • I tried with `private static Set test; test.getClass();`. However, does not work as returned runtime type information is not the same as with `new HashSet() {}.getClass()`. – nimo23 Aug 02 '19 at 11:06
  • @nimo23 I posted an answer for that particular point (so that it is correctly formatted) – Maurice Perry Aug 02 '19 at 11:16

1 Answers1

0

Nothing to do with serialization, but:

private static Set<Task> myField;

...

        Field field = MyClass.class.getDeclaredField("myField");
        Type type = field.getGenericType();
        ParameterizedType ptype = (ParameterizedType)type;
        Type[] argTypes = ptype.getActualTypeArguments();
        Type atype = argTypes[0];
        System.out.println(atype.getTypeName());
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24