10

我有以下两个课程

class A {
    class InnerA {
        private String field;

        // getters/setters
    }

    private Collection<InnerA> collection;

    // getters/setters
}

class B {
    private String field;

    // getters/setters
}

是否可以将 A 映射到 B 的集合(A.collection.field 应该映射到 B.field 的集合)?

我尝试使用自定义转换器,但我只需要管理 java.lang.VerifyError:

    mapperFactory.getConverterFactory().registerConverter(new CustomConverter<A, Collection<B>>() {

        @Override
        public Collection<B> convert(
                A arg0, Type<? extends Collection<B>> arg1) {

            Collection<B> result = new ArrayList<B>();

            Iterator<Item> it = arg0.getCollection().iterator();
            while(it.hasNext()){
                it.next();
                result.add(new B());
            }

            return result;
        }

    });

结果是:

java.lang.VerifyError: Inconsistent args count operand in invokeinterface in method    ma.glasnost.orika.generated.Orika_ArrayList_A_Mapper845657274.mapAtoB(Ljava/lang/Object;Ljava/lang/Object;Lma/glasnost/orika/MappingContext;)V at offset 74  
at java.lang.Class.getDeclaredConstructors0(Native Method)  
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2404)  
at java.lang.Class.getConstructor0(Class.java:2714)     
at java.lang.Class.newInstance0(Class.java:343)     
at java.lang.Class.newInstance(Class.java:325)  
4

1 回答 1

6

当然你可以映射它,你只需要公开 InnerA:

List<B> dest = mapper.mapAsList(sourceA.getCollection(), B.class);

如果 InnerA 不是公开的,则 Orika 不能使用它

MapperFacade mapper = new ConfigurableMapper() {
    @Override
    protected void configure(MapperFactory factory) {
    factory.registerMapper(new CustomMapper<A, Collection<B>>() {
        @Override
        public void mapAtoB(A a, Collection<B> b, MappingContext context) {                     b.addAll(mapperFacade.mapAsList(a.collection, B.class));

            for(B item : b) {
                item.propertyA = a.propertyA;
            }
        }               
    });             
    factory.registerConcreteType(Collection.class, ArrayList.class);
        }           
};  
final Type<Collection<B>> collectionOfB = new TypeBuilder<Collection<B>>() {}.build();
Collection<B> dest = mapper.map(source, TypeFactory.valueOf(A.class), collectionOfB);           
于 2013-03-13T17:03:39.907 回答