由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Generics应该怎么得到它的class?
相关主题
问个 Generic 的问题[转载] Java 1.5 Generic 问题
extending generic class , but not mentioning its parameterized type?An interesting thing about java generics-do not laugh at me if u think it too basic
出个简单题,看你Java APi熟悉到什么程度如何定义这样的数组?
Java大侠们:Hashtable help please!一个关于generics的问题
simple java question这玩意是啥?
请问protected的使用问一个Java的问题,关于create generic array
Classpath questions问两个语法问题
设计模式的问题如何造Array of Generic Type
相关话题的讨论汇总
话题: list话题: class话题: string话题: wrapper话题: generics
进入Java版参与讨论
1 (共1页)
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-

1 (共1页)
进入Java版参与讨论
相关主题
如何造Array of Generic Typesimple java question
系统class请问protected的使用
get full class nameClasspath questions
why doesn't replaceAll work?设计模式的问题
问个 Generic 的问题[转载] Java 1.5 Generic 问题
extending generic class , but not mentioning its parameterized type?An interesting thing about java generics-do not laugh at me if u think it too basic
出个简单题,看你Java APi熟悉到什么程度如何定义这样的数组?
Java大侠们:Hashtable help please!一个关于generics的问题
相关话题的讨论汇总
话题: list话题: class话题: string话题: wrapper话题: generics