Hi All,

Enclosed is my class implementing "CORBA IDL Union" handling.  Please feel
free to provide any suggestions.

--------------------------------------------------------------------------------------------------------------------------------
<class cst:name="MyPackage.MyUnionType">
  <map-to cst:xml="my-union"/>
        <field cst:name="typeOne" get-method="typeOne" set-method="typeOne"
type="int" cst:lazy="true"
                   handler="castor.CORBA.CastorIDLUnionHandler">
            <bind-xml name="type-one" node="element"/>
        </field>
        <field cst:name="typeTwo" get-method="typeTwo" set-method="typeTwo"
type="MyPackage.TypeTwo" cst:lazy="true"
                   handler="castor.CORBA.CastorIDLUnionHandler">
            <bind-xml name="type-two" node="element"/>
        </field>
</class>
--------------------------------------------------------------------------------------------------------------------------------

Thanks,

Scott


package castor.CORBA;
//
// Reflection API's
//
import java.lang.reflect.Method;

public class CastorIDLUnionHandler extends AbstractFieldHandler {

    /** Creates a new instance of CastorIDLUnionHandler */
    public CastorIDLUnionHandler() {
    }


    public Object getValue(Object obj) throws IllegalStateException {
        String fieldName = this.getFieldDescriptor().getFieldName();

        try {
            Method meth = obj.getClass().getDeclaredMethod(fieldName, new
Class[0]);
            return meth.invoke(obj, new Object[0]);
            //
            // IDL Unions will throw RuntimeExceptions if
            // you attempt to access a field that is "current"
            // in the context of the descriminator.  So,
            // we will return null which will cause processing
            // of this field to cease.
            //
        } catch(Exception e) {
            // e.printStackTrace();
            // System.err.println(e);
            return null;
        }
    }

    public Object newInstance(Object parent)
        throws IllegalStateException
    {
        return newInstance(parent, null);
    }

    public Object newInstance(Object parent, Object[] args)
         throws IllegalStateException
    {
        String fieldName = this.getFieldDescriptor().getFieldName();
        try {
            Method meth = parent.getClass().getDeclaredMethod(fieldName,
new Class[0]);
            Class type = meth.getReturnType();
            return type.newInstance();
        } catch(Exception e) {
            throw new IllegalStateException("newInstance(2):" + e);
        }

    }

    public void resetValue(Object obj)
         throws IllegalStateException, IllegalArgumentException
    {
        throw new IllegalStateException("NotSupported:resetValue(obj)");
    }

    public void setValue(Object obj, Object value)
        throws IllegalStateException, IllegalArgumentException
    {
        String fieldName = this.getFieldDescriptor().getFieldName();

        if(obj == null || value == null) {
            throw new IllegalArgumentException("Either obj or value was
null!");
        }

        try {
            Class [] classes = { value.getClass() };
            Method meth = obj.getClass().getDeclaredMethod(fieldName,
classes);
            Object [] parameters = { value };
            meth.invoke(obj, parameters);
        } catch(Exception e) {
           throw new IllegalStateException("setValue(" + e + ")");
        }
    }

}

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to