Reputation: 556
If I have the following:
public abstract class Parameterized<T> {
protected abstract String foo();
}
Is it possible to do something like:
DynamicType.Unloaded<Parameterized<MyClass>> subclassed = new ByteBuddy()
.subclass(Parameterized.class)... //assign MyClass.java to T?
Or is it only possible to do:
DynamicType.Unloaded<Parameterized> subclassed = new ByteBuddy()
.subclass(Parameterized.class).make();
Upvotes: 3
Views: 1097
Reputation: 44150
Because generics in Java are implemented using type erasure, Parameterized<MyClass>
only really exists at compile time. At runtime there is only Parameterized
.
Therefore there are only a few uses for this. Please see K5_'s answer.
Upvotes: -2
Reputation: 5558
Only type information of instances is erased; not on references. Depending on usecase it might provide some benefits to actually extend with the correct parameters (instead of extending the raw type).
Thats how you do it with ByteBuddy:
Class<?> subclass = new ByteBuddy()
.subclass(TypeDescription.Generic.Builder.parameterizedType(Parameterized.class, MyClass.class).build())
.make()
.load(Parameterized.class.getClassLoader())
.getLoaded();
One way of using/extracting the type parameter at runtime would be:
Paramerized<MyClass> instance = (Paramerized<MyClass>) subclass.getConstructor().newInstance();
if (instance.getClass() instanceof ParameterizedType) {
ParameterizedType para = (ParameterizedType) instance.getClass().getGenericSuperclass();
System.out.println(para.getActualTypeArguments()[0]);
}
Upvotes: 4