You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by Narve Saetre <na...@machina.no> on 2002/05/03 18:34:27 UTC

deserialization of final fields

It seems the BeanDeserializer does not take into account that fields may 
be final (you get a java.lang.IllegalAccessException when you try to set 
the value). I see that several people (well, at least two) have asked 
about this in the user mailing list.

An easy fix is to add

             } catch (java.lang.IllegalAccessException ex) {
             	// Final field - Ignore


on line 113 of BeanPropertyTarget.java. (From beta-2, that is, the 
complete method is shown at the bottom).

This is probably not the best solution (one should check whether the 
field is final before one even try to set the value), and it may not 
even be suitable for all purposes, but it works for me and I'll keep the 
change locally until this issue is resolved.

Please, if anybody has better solutions post them here (or update the 
source).

Regards,


Narve


--- from BeanPropertyTarget.java ---

     public void set(Object value) throws SAXException {
         try {
             if (index < 0)
                 pd.set(object, value);
             else
                 pd.set(object, index, value);
         } catch (Exception e) {
             Class type = pd.getType();
             value = JavaUtils.convert(value, type);
             try {
                 if (index < 0)
                     pd.set(object, value);
                 else
                     pd.set(object, index, value);
             } catch (java.lang.IllegalAccessException ex) {
             	// Final field - Ignore
             } catch (Exception ex) {
                 String field= pd.getName();
                 int i = 0;
                 if (index >=0) {
                     field += "[" + index + "]";
                     i = 1;
                 }
                 if (log.isErrorEnabled()) {
                     String valueType = "null";
                     if (value != null)
                         valueType = value.getClass().getName();
                     log.error(JavaUtils.getMessage("cantConvert02",
                                                    new String[] {
                                                        valueType,
                                                        field, 
                                            		pd.getType().getName()}));
                 }
                 throw new SAXException(ex);
             }
         }
     }