java - Get generic type of class at runtime -
how can achieve this?
public class genericclass<t> { public type getmytype() { //how return type of t? } }
everything have tried far returns type object
rather specific type used.
as others mentioned, it's possible via reflection in circumstances.
if need type, usual (type-safe) workaround pattern:
public class genericclass<t> { private final class<t> type; public genericclass(class<t> type) { this.type = type; } public class<t> getmytype() { return this.type; } }
Comments
Post a Comment