简体   繁体   中英

Java - Class<? extends Collection>

I would like to add multiple class in below jackson deserialize. But how do i do that? below is the sample code.

XmlMapper mapper = new XmlMapper();
JaxbAnnotationModule module = new JaxbAnnotationModule();
mapper.registerModule(module);
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory());
mapper.setAnnotationIntrospector(introspector);
TypeFactory typeFactory = mapper.getTypeFactory();
Class<? extends Collection> list;
MapType mapType = typeFactory.constructRawCollectionType(list);
File is = new File("myXMLFile.xml");
ArtworkContentMessageType je = mapper.readValue(is,mapType);

below part is confusing me. How to add class to list?

Class<? extends Collection> list;
MapType mapType = typeFactory.constructRawCollectionType(list);

how do i add multple class(ex: Body.class and P.class). I try below and its not the right object because its not a Class type.

List<Class> clazz = new ArrayList<>();
clazz.add(ArtworkContentMessageType.class);
clazz.add(Body.class);
clazz.add(P.class);
MapType mapType = typeFactory.constructRawCollectionType(clazz);

java.lang.Class is a generic class, meaning it can take parameters, as per this syntax:

Class<T>

where T is the type of the class modeled by the Class object. Taking the same example as in the documentation, the type of String.class is Class<String> .

As such, by using the wildcard syntax , you can declare an instance of a Class without knowing its type: Class<?> .

If you don't know the type but you know it will extend a given class, you can use this syntax: Class<? extends Superclass> Class<? extends Superclass> .

So this syntax:

Class<? extends Collection> list;

declares a class whose type is a subclass of Collection .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM