Title: NotConstructable Exception and FieldHandler
Hi, Matthias:
 
I'm no Castor expert,but I can tell you that Class.newInstance() requires a no argument constructor be available on the
Class whose instance you are trying to construct.
 
From the Javadocs->
 
****************
Creates a new instance of the class represented by this Class object. The class is instantiatied as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized.
 
****************
 
 
As a workaround you could create a pass-through proxy that for CoordinateFormatEnum that knows how to construct
an instance of a CoordinateFormatEnum.   This proxy would implement all methods of CoordinateFormatEnum and just
delegate the calls to the embedded CoordinateFormatEnum instance.
 
That's one way...   maybe the Castor guys can suggest a better one..   I'd be interested in finding out as well since I find creation
of these proxies to be a hassle.. but they get the job done.
 
thanks
    Chris
 
 
 
 
 
 
 
----- Original Message -----
Sent: Monday, December 16, 2002 1:39 AM
Subject: [castor-dev] NotConstructable Exception and FieldHandler

Hi,

I have written a FieldHandler for a class (CoordinateFormatEnum) that has no public constructor. When Unmarshalling I get the following exception:

Nested error: org.exolab.castor.mapping.MappingException: The Java class CoordinateFormatEnum is not constructable -- it does not contain a default public constructor)

The problem is that the class CoordinateFormatEnum is a tool-generated class, so I have no chance to make the constructor public. So How can this problem be solved? I thought that the Unmarshaller does not need to call the constructor of the class since the FieldHandler is responsible for that, e.g. using the method "newInstance".

Here's the handler's code:

public class CoordinateFormatEnumFieldHandler implements FieldHandler {

    public Object getValue(Object object)
            throws IllegalStateException {
        return ((Coordinates)object).getFormat().getValue();
    }

    public void setValue(Object object, Object value)
            throws IllegalStateException, IllegalArgumentException {
        ((Coordinates)object).setFormat(CoordinateFormatEnum.fromString((String)value));
    }

    public void resetValue(Object object)
            throws IllegalStateException, IllegalArgumentException {
    }

    public void checkValidity(Object object)
            throws ValidityException, IllegalStateException {
    }

    public Object newInstance(Object parent)
            throws IllegalStateException {
        return CoordinateFormatEnum.Mercator;
    }
}

And here's the mapping:

    <class name="com.Coordinates" access="shared">
        <description>Default mapping for Coordinates</description>
        <map-to xml="Coordinates"/>
        <field name="format" type="com.CoordinateFormatEnum" handler="com.CoordinateFormatEnumFieldHandler">
            <bind-xml name="Format" node="attribute"/>
        </field>
    </class>

Any clues for that?

Thanks,
 Matthias.

Reply via email to