How can I get the package name (in general, the class) of a generic type argument? I have a class similar to this:
public class MyClass<T> {}
What I'd like to do is something like this (non-working pseudo-Java):
return T.class.getPackage().getName();
I need this, s开发者_开发问答ince JAXB needs this as the context path (JAXBContext.newInstance(String)
method). Of course I'm open to more elegant ways of working with JAXBContext
.
This is a possible solution (if you can change MyClass):
public class MyClass<T> {
private Class<T> clazz;
public MyClass (Class<T> clazz) {
this.clazz = clazz;
}
public String getPackageNameOfGenericClass() {
return clazz.getPackage().getName();
}
}
精彩评论