404-not-find / orika

Automatically exported from code.google.com/p/orika
2 stars 0 forks source link

Class cast exception to MapEntry #112

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Mapping List<MyObject<String,List<String>>> to Map<String, List<String>>
2. java.lang.ClassCastException: java.lang.String cannot be cast to 
ma.glasnost.orika.MapEntry

What is the expected output? What do you see instead?
I was expecting correct mapping.
But, I see:

Original instance: Class$1{list=[Class$2{name='abc', strings=[qwe, wer]}, 
Class$2{name='dfg', strings=[sdf, cvb]}]} 
Exception in thread "main" java.lang.ClassCastException: java.lang.String 
cannot be cast to ma.glasnost.orika.MapEntry
    at ma.glasnost.orika.generated.Orika_Class$1Binding_Class$1_Mapper360845151120717$0.mapAtoB(Orika_Class$1Binding_Class$1_Mapper360845151120717$0.java:30)
    at ma.glasnost.orika.impl.mapping.strategy.UseCustomMapperStrategy.map(UseCustomMapperStrategy.java:65)
    at ma.glasnost.orika.impl.DefaultBoundMapperFacade.map(DefaultBoundMapperFacade.java:137)
    at ma.glasnost.orika.impl.DefaultBoundMapperFacade.map(DefaultBoundMapperFacade.java:94)
    at orika.bug.OrikaBug.main(OrikaBug.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

What version of the product are you using? On what operating system?
1.4.3

Please provide any additional information below.

Test:

package orika.bug;

import ma.glasnost.orika.BoundMapperFacade;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.impl.generator.EclipseJdtCompilerStrategy;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author: ikrokhmalyov@griddynamics.com
 * @since: 7/1/13
 */

public class OrikaBug {
    public static void main(String[] args) {
        DefaultMapperFactory mapperFactory=  new DefaultMapperFactory.Builder().compilerStrategy(new EclipseJdtCompilerStrategy()).build();
        mapperFactory.classMap(Class$1.class, Class$1Binding.class)
                .field("list{name}","list{key}")
                .field("list{strings}", "list{value}")
                .byDefault()
                .register();
        BoundMapperFacade<Class$1, Class$1Binding> mapper = mapperFactory.getMapperFacade(Class$1.class, Class$1Binding.class);
        Class$1 inst = new Class$1(Arrays.asList(new Class$2("abc", Arrays.asList("qwe","wer")), new Class$2("dfg", Arrays.asList("sdf","cvb"))));
        System.out.printf("Original instance: %s \n", inst);
        System.out.printf("Mapped intance: %s \n" , mapper.map(inst));

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static class Class$1 {

        public Class$1() {
        }

        public Class$1(List<Class$2> list) {
            this.list = list;
        }

        private List<Class$2> list;

        public List<Class$2> getList() {
            return list;
        }

        public void setList(List<Class$2> list) {
            this.list = list;
        }

        @Override
        public String toString() {
            return "Class$1{" +
                    "list=" + list +
                    '}';
        }
    }

    public static class Class$2 {

        public Class$2() {
        }

        public Class$2(String name, List<String> strings) {
            this.name = name;
            this.strings = strings;
        }

        private String name;
        private List<String> strings;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<String> getStrings() {
            return strings;
        }

        public void setStrings(List<String> strings) {
            this.strings = strings;
        }

        @Override
        public String toString() {
            return "Class$2{" +
                    "name='" + name + '\'' +
                    ", strings=" + strings +
                    '}';
        }
    }

    public static class Class$1Binding {
        private Map<String, List> list;

        public Map<String, List> getList() {
            return list;
        }

        public void setList(Map<String, List> list) {
            this.list = list;
        }

        @Override
        public String toString() {
            return "Class$1Binding{" +
                    "list=" + list +
                    '}';
        }
    }
}

//Problem in generated code:
// Very interesting generated code. It seems to me that there are copying 
(mapping) twice.

package ma.glasnost.orika.generated;

public class Orika_Class$1Binding_Class$1_Mapper357630944150314$0 extends
        ma.glasnost.orika.impl.GeneratedMapperBase {

    public void mapAtoB(java.lang.Object a, java.lang.Object b,
            ma.glasnost.orika.MappingContext mappingContext) {

        super.mapAtoB(a, b, mappingContext);

        orika.bug.OrikaBug.Class$1 source = ((orika.bug.OrikaBug.Class$1) a);
        orika.bug.OrikaBug.Class$1Binding destination = ((orika.bug.OrikaBug.Class$1Binding) b);

        if (!(((java.util.List) source.getList()) == null)) {
            java.util.Map new_list = ((java.util.Map) new java.util.LinkedHashMap(
                    new java.util.LinkedHashMap()));
            for (java.util.Iterator entryIter = ((java.util.List) source
                    .getList()).iterator(); entryIter.hasNext();) {

                orika.bug.OrikaBug.Class$2 sourceListElement = ((orika.bug.OrikaBug.Class$2) entryIter
                        .next());
                ma.glasnost.orika.MapEntry sourceListEntry = ((ma.glasnost.orika.MapEntry) mapperFacade
                        .map(
                                sourceListElement,
                                ((ma.glasnost.orika.metadata.Type) usedTypes[0]),
                                ((ma.glasnost.orika.metadata.Type) usedTypes[1]),
                                mappingContext));

                new_list.put(((ma.glasnost.orika.MapEntry) sourceListEntry   // Try cast String MapEntry. There are no additional brackets.
                        .getKey()),
                        ((ma.glasnost.orika.MapEntry) sourceListEntry
                                .getValue()));
            }
            destination.setList(new_list);
        } else {
            destination.setList(null);
        }
        java.util.ArrayList new_destinationList0 = ((java.util.ArrayList) new java.util.ArrayList());
        ma.glasnost.orika.MapEntry list_destination0Element = null;
        boolean list_destination0ElementShouldBeAddedToCollector = false;
        java.lang.Object value_destination1Element = null;
        boolean value_destination1ElementShouldBeAddedToCollector = false;
        java.util.Iterator list_$_iter = ((java.util.List) source.getList())
                .iterator();
        while (list_$_iter.hasNext()) {
            orika.bug.OrikaBug.Class$2 list_source0Element = ((orika.bug.OrikaBug.Class$2) list_$_iter
                    .next());
            if (list_destination0Element == null
                    || !(((((java.lang.String) list_source0Element.getName()) != null && ((java.lang.String) list_source0Element
                            .getName())
                            .equals(((java.lang.String) list_destination0Element
                                    .getKey())))))) {

                list_destination0Element = ((ma.glasnost.orika.MapEntry) ((ma.glasnost.orika.BoundMapperFacade) usedMapperFacades[0])
                        .newObject(list_source0Element, mappingContext));
                list_destination0ElementShouldBeAddedToCollector = true;
            }
            list_destination0Element
                    .setKey(((java.lang.String) list_source0Element.getName()));
            if (list_destination0ElementShouldBeAddedToCollector) {
                new_destinationList0
                        .add(((ma.glasnost.orika.MapEntry) list_destination0Element));
                list_destination0ElementShouldBeAddedToCollector = false;
            }
            if (!(((java.util.List) list_source0Element.getStrings()) == null)) {

                java.util.List new_value = ((java.util.List) new java.util.ArrayList(
                        ((java.util.List) list_source0Element.getStrings())
                                .size()));

                new_value.addAll(mapperFacade.mapAsList(
                        ((java.util.List) list_source0Element.getStrings()),
                        ((ma.glasnost.orika.metadata.Type) usedTypes[2]),
                        ((ma.glasnost.orika.metadata.Type) usedTypes[3]),
                        mappingContext));
                list_destination0Element.setValue(new_value);
            } else {
                list_destination0Element.setValue(null);
            }
        }

        if (!(new_destinationList0 == null) && !new_destinationList0.isEmpty()) {
            destination.setList(((java.util.Map) listToMap(
                    new_destinationList0, java.util.LinkedHashMap.class)));
        }

        if (customMapper != null) {
            customMapper.mapAtoB(source, destination, mappingContext);
        }
    }

    public void mapBtoA(java.lang.Object a, java.lang.Object b,
            ma.glasnost.orika.MappingContext mappingContext) {

        super.mapBtoA(a, b, mappingContext);

        orika.bug.OrikaBug.Class$1Binding source = ((orika.bug.OrikaBug.Class$1Binding) a);
        orika.bug.OrikaBug.Class$1 destination = ((orika.bug.OrikaBug.Class$1) b);

        if (!(((java.util.Map) source.getList()).entrySet() == null)) {

            java.util.List new_list = ((java.util.List) new java.util.ArrayList(
                    ((java.util.Map) source.getList()).entrySet().size()));

            new_list.addAll(mapperFacade.mapAsList(((java.util.Map) source
                    .getList()).entrySet(),
                    ((ma.glasnost.orika.metadata.Type) usedTypes[4]),
                    ((ma.glasnost.orika.metadata.Type) usedTypes[0]),
                    mappingContext));
            destination.setList(new_list);
        } else {
            destination.setList(null);
        }
        java.util.List new_destinationList0 = ((java.util.List) new java.util.ArrayList(
                min(new int[] { ((java.util.Map) source.getList()).size() })));
        orika.bug.OrikaBug.Class$2 list_destination0Element = null;
        boolean list_destination0ElementShouldBeAddedToCollector = false;
        java.lang.String strings_destination1Element = null;
        boolean strings_destination1ElementShouldBeAddedToCollector = false;
        java.util.Iterator list_$_iter = ((java.util.Map) source.getList())
                .entrySet().iterator();
        while (list_$_iter.hasNext()) {
            java.util.Map.Entry list_source0Element = ((java.util.Map.Entry) list_$_iter
                    .next());
            if (list_destination0Element == null
                    || !(((((java.lang.String) list_source0Element.getKey()) != null && ((java.lang.String) list_source0Element
                            .getKey())
                            .equals(((java.lang.String) list_destination0Element
                                    .getName())))))) {

                list_destination0Element = ((orika.bug.OrikaBug.Class$2) ((ma.glasnost.orika.BoundMapperFacade) usedMapperFacades[1])
                        .newObject(list_source0Element, mappingContext));
                list_destination0ElementShouldBeAddedToCollector = true;
            }
            list_destination0Element
                    .setName(((java.lang.String) list_source0Element.getKey()));
            if (list_destination0ElementShouldBeAddedToCollector) {
                new_destinationList0
                        .add(((orika.bug.OrikaBug.Class$2) list_destination0Element));
                list_destination0ElementShouldBeAddedToCollector = false;
            }
            if (!(((java.util.List) list_source0Element.getValue()) == null)) {

                java.util.List new_strings = ((java.util.List) new java.util.ArrayList(
                        ((java.util.List) list_source0Element.getValue())
                                .size()));

                new_strings.addAll(mapperFacade.mapAsList(
                        ((java.util.List) list_source0Element.getValue()),
                        ((ma.glasnost.orika.metadata.Type) usedTypes[3]),
                        ((ma.glasnost.orika.metadata.Type) usedTypes[2]),
                        mappingContext));
                list_destination0Element.setStrings(new_strings);
            } else {
                list_destination0Element.setStrings(null);
            }
        }

        if (!(new_destinationList0 == null) && !new_destinationList0.isEmpty()) {
            if (((java.util.List) destination.getList()) == null) {
                destination.setList(((java.util.List) new java.util.ArrayList(
                        min(new int[] { ((java.util.Map) source.getList())
                                .size() }))));
            } else {

                ((java.util.List) destination.getList()).clear();
            }

            ((java.util.List) destination.getList())
                    .addAll(new_destinationList0);
        }

        if (customMapper != null) {
            customMapper.mapBtoA(source, destination, mappingContext);
        }
    }

}

Original issue reported on code.google.com by JAD7KII on 1 Jul 2013 at 1:47

Attachments:

GoogleCodeExporter commented 8 years ago
Fixed, when remove ArrayOrCollectionToMap from Specification List..

Original comment by JAD7KII on 1 Jul 2013 at 2:14

GoogleCodeExporter commented 8 years ago
It looks like this can also be fixed by removing the 'byDefault()' call--which 
adds the mapping of 'list' to 'list'--that's why you're seeing it mapping twice.
It seems like to correct this we'd need to make sure that 'list' gets excluded 
from the 'byDefault()' function if it has already been included in an aggregate 
spec.

Original comment by matt.deb...@gmail.com on 7 Jul 2013 at 2:29

GoogleCodeExporter commented 8 years ago
A fix has been uploaded to the current master; please take a look...

Original comment by matt.deb...@gmail.com on 7 Jul 2013 at 3:39

GoogleCodeExporter commented 8 years ago

Original comment by elaat...@gmail.com on 5 Dec 2013 at 10:50