d****i 发帖数: 4809 | 1 一般的普通的class,可以通过MyClass.class得到,但是如果有Generics的情况,由于
type erasure,就没法得到它的class,比如我想用List.class,就会报错,
但是有些class method只接受这样的input argument:
public void foo(Class> class)
如果我想传入一个List的类的话,怎么办?没法用
foo(List.class) |
g*****g 发帖数: 34805 | 2 Depends on whether you have control on the API, you may pass in a
GenericEntity. |
d****i 发帖数: 4809 | 3 多谢,如果是第三方库没有办法改method呢?比如这个:
public static JAXBContext newInstance(Class... classesToBeBound)
我没法传入List.class
【在 g*****g 的大作中提到】 : Depends on whether you have control on the API, you may pass in a : GenericEntity.
|
f*******n 发帖数: 12623 | 4 List.class就行了。只有一个List class object。
【在 d****i 的大作中提到】 : 一般的普通的class,可以通过MyClass.class得到,但是如果有Generics的情况,由于 : type erasure,就没法得到它的class,比如我想用List.class,就会报错, : 但是有些class method只接受这样的input argument: : public void foo(Class> class) : 如果我想传入一个List的类的话,怎么办?没法用 : foo(List.class)
|
g*****g 发帖数: 34805 | 5 you can always write a listofstr class, ugly but works anyway.
【在 d****i 的大作中提到】 : 多谢,如果是第三方库没有办法改method呢?比如这个: : public static JAXBContext newInstance(Class... classesToBeBound) : 我没法传入List.class
|
g******m 发帖数: 46 | 6 There is a solution,
create a generic list wrapper to wrap your String List, whatever List, and
JAXB always unmarshall xml message to the wrapper, and from this wrapper
object, you can get the wrapped List object.
I have used this approach a lot in my projects, it works well.
http://blog.bdoughan.com/2012/11/creating-generic-list-wrapper- |
d****i 发帖数: 4809 | 7 Thanks. This is a nice solution. I think it is similar to goodbug's solution
but it uses a generic List in the wrapper class.
wrapper
【在 g******m 的大作中提到】 : There is a solution, : create a generic list wrapper to wrap your String List, whatever List, and : JAXB always unmarshall xml message to the wrapper, and from this wrapper : object, you can get the wrapped List object. : I have used this approach a lot in my projects, it works well. : http://blog.bdoughan.com/2012/11/creating-generic-list-wrapper-
|