It is well known that generic types do not withstand the compilation process. They are replaced by classes.
But, nevertheless, type information is present in the class file and can be visible using reflection:
public class Demo { private List<String> list; public Demo() throws SecurityException, NoSuchFieldException { System.out.println(((Class<?>)((ParameterizedType) getClass().getDeclaredField("list").getGenericType()).getActualTypeArguments()[0]).getName()); } public static void main(String[] args) throws SecurityException, NoSuchFieldException { new Demo(); } }
When doing this, java.lang.String will be printed.
Can JIT use this for some kind of optimization? Or is this information from a JIT point of view useless?
java optimization jit
yankee
source share