I statically recompiled a Java lib which used generics a lot, like Collection<?>
, but the emitted .NET dll only uses Collection
, not with type parameters. How come?
views:
247answers:
1Java generics are dealt by the Java compiler and are converted to non-generic version at compile time. This is different from .NET where the CLR has first class support for type parameters. At the bytecode level, ArrayList<T>
will just be a simple ArrayList
.
To quote Java docs:
Generics are implemented by the Java compiler as a front-end conversion called erasure, which is the process of translating or rewriting code that uses generics into non-generic code (that is, maps the new syntax to the current JVM specification). In other words, this conversion erases all generic type information; all information between angle brackets is erased. For example,
LinkedList<Integer>
will becomeLinkedList
. Uses of other type variables are replaced by the upper bound of the type variable (for example,Object
), and when the resulting code is not type correct, a cast to the appropriate type is inserted.