Custom converter to avoid the exception of IllegalAccessException while mapping XMLGregorianCalendarImpl in dozer

1.6k views Asked by At

Initially I was getting the below exception: I am using dozer 5.4. I am having the xerces jar file in my classpath. I am new to dozer so any kind of help is very much appreciated.

org.dozer.MappingException: java.lang.IllegalAccessException: Class org.dozer.util.ReflectionUtils can not access a member of class org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl with modifiers "public"

I read from other posts from this site that the solution to the above is to write a custom converter for XmlGregorianCalender.

Below is the code for the custom converter. Currently the convertFrom method is getting invoked and all the values being passed are null.

Custom Converter:

 import javax.xml.datatype.XMLGregorianCalendar;
    
    import org.dozer.DozerConverter;
    
    public class XMLGregorianCalendarCustomConvertor extends
    DozerConverter<XMLGregorianCalendar, XMLGregorianCalendar>{
    public XMLGregorianCalendarCustomConvertor() {
        super(XMLGregorianCalendar.class, XMLGregorianCalendar.class);
        // TODO Auto-generated constructor stub
    }

    @Override
    public XMLGregorianCalendar convertTo(XMLGregorianCalendar source,
            XMLGregorianCalendar destination) {
        if (source == null) {
            return null;
            }
        else{
            return source;
        }
    }

    @Override
    public XMLGregorianCalendar convertFrom(XMLGregorianCalendar source,
            XMLGregorianCalendar destination) {
        if(destination == null){
            
            return null;
        }
        else{
            return destination;
        }
    }

}

Mapping xml

<configuration>
     <custom-converters> 
        <converter type="com.code.user.XMLGregorianCalendarCustomConvertor"  >
             <class-a>javax.xml.datatype.XMLGregorianCalendar</class-a>
              <class-b>javax.xml.datatype.XMLGregorianCalendar</class-b>
         </converter>
    </custom-converters>     
 </configuration>
2

There are 2 answers

1
rgrebski On

How about copying it by reference ? If its an option for you you can do it like this:

<configuration>
    <copy-by-references>
      <copy-by-reference>
        javax.xml.datatype.XMLGregorianCalendar
      </copy-by-reference>
    </copy-by-references>
  </configuration>
0
Piotr Ławniczak On

The problem is the class:

org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl
is not a public implementation of:
javax.xml.datatype.XMLGregorianCalendar

The solution is to simply add is-accessible="true" property to you destination class in Dozer converter configuration like that:

Mapping.xml

<configuration>
     <custom-converters> 
        <converter type="com.code.user.XMLGregorianCalendarCustomConvertor"  >
             <class-a>javax.xml.datatype.XMLGregorianCalendar</class-a>
              <class-b is-accessible="true">javax.xml.datatype.XMLGregorianCalendar</class-b>
         </converter>
    </custom-converters>     
 </configuration>

Hope it will help anyone!