Failed to map enumeration and NoSuchMethodException thrown

1,400 views
Skip to first unread message

col...@gmail.com

unread,
Jan 31, 2013, 7:41:45 PM1/31/13
to dozer-...@googlegroups.com

We have some fields defined as enumeration in source object and destination object, we need to map enum to enum. But it failed to map. (all enum to enum mapping failed). And the following exception thrown out

 
java.lang.NoSuchMethodExceptiopackagename.FunctionCodeEnum$Enum.<init>()
 
Any insights will be highly appreciated!
 
 

Miguel C.

unread,
Mar 23, 2013, 1:48:56 AM3/23/13
to dozer-...@googlegroups.com
Can you post your code? and the complete trace.

Leszek Zając

unread,
Apr 22, 2013, 5:27:18 PM4/22/13
to dozer-...@googlegroups.com
I does not work. Use a custom converter:

public class EnumConverter implements CustomConverter {

    @Override
    public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue,
            Class<?> destinationClass, Class<?> sourceClass) {

        if (!destinationClass.isEnum()) {
            throw new IllegalArgumentException("destinationClass must be an enum");
        }
        if (!sourceClass.isEnum()) {
            throw new IllegalArgumentException("sourceClass must be an enum");
        }

        Enum<?> sourceValue = (Enum<?>) sourceFieldValue;
        Object[] values = destinationClass.getEnumConstants();
        for (Object value : values) {
            Enum<?> enumValue = (Enum<?>) value;
            if (enumValue.name().equals(sourceValue.name())) {

                return enumValue;
            }
        }
        throw new IllegalArgumentException("Enum constant [" + sourceValue + "] not found in "
                + destinationClass);
    }

}


<custom-converters>
<!-- This is a trick to make Dozer convert enums -->
<converter type="pl.webcad.clientservice.mapping.EnumConverter">
<class-a>java.lang.Enum</class-a>
<class-b>java.lang.Enum</class-b>
</converter>
</custom-converters>

Roberto Socrates

unread,
Jan 30, 2015, 2:06:38 AM1/30/15
to dozer-...@googlegroups.com
Let me "generify" your code:

    public <T extends Enum<T>, R extends Enum<R>> T enums(
        /*Object existingDestinationFieldValue, */
        Class<T> destinationClass,
        R sourceFieldValue, Class<R> sourceClass) {

        if (!destinationClass.isEnum()) {
            throw new IllegalArgumentException("destinationClass must be an enum");
        }
        if (!sourceClass.isEnum()) {
            throw new IllegalArgumentException("sourceClass must be an enum");
        }

        R sourceValue = sourceFieldValue;
        T[] values = destinationClass.getEnumConstants();
        for (T value : values) {
            if (value.name().equals(sourceValue.name())) {

                return value;
Reply all
Reply to author
Forward
0 new messages