Package org.exolab.castor.mapping

Examples of org.exolab.castor.mapping.MappingException


    public void setAddMethod( Method method )
        throws MappingException
    {
        if ( ( method.getModifiers() & Modifier.PUBLIC ) == 0 ||
             ( method.getModifiers() & Modifier.STATIC ) != 0 )
            throw new MappingException( "mapping.accessorNotAccessible",
                                        method, method.getDeclaringClass().getName() );
        if ( method.getParameterTypes().length != 1 )
            throw new MappingException( "mapping.writeMethodNoParam",
                                        method, method.getDeclaringClass().getName() );
        _addMethod = method;
       
        //-- make sure add method is not the same as the set method
        if (_addMethod == _setMethod) _setMethod = null;
View Full Code Here


    {
       
        Class clazz = clsDesc.getJavaClass();
        if (_clsDescs.containsKey( clazz )) {
            if (!_allowRedefinitions) {
                throw new MappingException( "mapping.duplicateDescriptors", clazz.getName() );
            }
        }
        else {
            _javaClasses.addElement( clazz );
        }
View Full Code Here

        // Obtain the Java class.
        try {
            javaClass = resolveType( clsMap.getName() );
        } catch ( ClassNotFoundException except ) {
            throw new MappingException( "mapping.classNotFound", clsMap.getName() );
        }

        // If this class extends another class, need to obtain the extended
        // class and make sure this class indeed extends it.
        if ( clsMap.getExtends() != null ) {
            try {
                extend = getDescriptor( resolveType( ( (ClassMapping) clsMap.getExtends() ).getName() ) );
                if ( extend == null )
                    throw new MappingException( "mapping.extendsMissing",
                                                clsMap.getExtends(), javaClass.getName() );
                if ( extend == NoDescriptor )
                    throw new MappingException( "mapping.extendsNoMapping",
                                                clsMap.getExtends(), javaClass.getName() );
            } catch ( ClassNotFoundException except ) {
                throw new MappingException( except );
            }
        } else
            extend = null;


        // If this class depends another class, need to obtain the depended class
        if ( clsMap.getDepends() != null ) {
            try {
                depend = getDescriptor( resolveType( ( (ClassMappingclsMap.getDepends() ).getName() ) );
                if ( depend == null )
                    throw new MappingException( "Depends not found" +
                                                clsMap.getDepends() + " " + javaClass.getName() );
                if ( depend == NoDescriptor )
                    throw new MappingException( "Depends not found" +
                                                clsMap.getDepends() + " " + javaClass.getName() );
            } catch ( ClassNotFoundException except ) {
                throw new MappingException( except );
            }
        } else
            depend = null;


        // Get field descriptors first. Note: order must be preserved for fields,
        // but not for relations or container fields. Add all the container fields
        // in there.
        FieldMapping[] fm = clsMap.getFieldMapping();
        fields = createFieldDescs( javaClass, fm );

        // Make sure there are no two fields with the same name.
        // Crude but effective way of doing this.
        for ( int i = 0 ; i < fields.length ; ++i ) {
            for ( int j = i + 1 ; j < fields.length ; ++j ) {
                if ( fields[ i ].getFieldName().equals( fields[ j ].getFieldName() ) )
                    throw new MappingException( "The field " + fields[ i ].getFieldName() +
                                                " appears twice in the descriptor for " +
                                                javaClass.getName() );
            }
        }

        // Obtain the identity field from one of the above fields.
        // The identity field is removed from the list of fields.
        identities = null;
        boolean idfield = false;
        String[] ids;
        ClassMapping origin = clsMap;
        Vector fieldList = new Vector();

        while (origin.getExtends() != null) {
            origin = (ClassMapping) origin.getExtends();
        }
        ids = origin.getIdentity();
        if (( ids != null ) && ( ids.length > 0)) {

            // Check that an XML mapping file do not declare more identity
            // attributes for a given class than there are field elements
            // defined for that class.
            // (Patch submitted by Gabriel Richard Pack <gpack@electroneconomy.com>)
            if ( ids.length > fields.length && origin == clsMap ) {
                String badIdentities = "";
                String delimiter     = " or ";
                for ( int index = 0; index < ids.length; index++ ) {
                    badIdentities += ids[index];
                    if ( index != ids.length - 1 )
                        badIdentities += delimiter;
                }
                throw new MappingException( "mapping.identityMissing",
                badIdentities, javaClass.getName() );
            }

            identities = new FieldDescriptor[ids.length];
            // separates fields into identity fields and regular fields
            for ( int i=0; i < fields.length ; i++ ) {
                //System.out.println("MappingLoader.createClassDesc.for:id: " + i );
                idfield = false;
                for ( int k=0; k<ids.length; k++ ) {
                    //System.out.println(fields[i].getFieldName() + " " + ids[k] );
                    if ( fields[i].getFieldName().equals( ids[k] ) ) {
                        identities[k] = fields[i];
                        idfield = true;
                        break;
                    }
                }
                if ( idfield ) {
                    //System.out.println("Field["+i+"] is an id field");
                    if ( fields[i] instanceof FieldDescriptorImpl )
                        ( (FieldDescriptorImpl) fields[i] ).setRequired( true );
                    if ( fields[i].getHandler() instanceof FieldHandlerImpl )
                        ( (FieldHandlerImpl) fields[i].getHandler() ).setRequired( true );
                } else {
                    // copy non identity field from list of fields.
                    fieldList.addElement(fields[i]);
                }
            }

            if (extend != null) {
                // we allow identity fields to be re-defined in the extends
                // class mapping to override some properties of the field,
                // for example, <sql name="..."/>.
                if ( extend instanceof ClassDescriptorImpl ) {
                    ClassDescriptorImpl extendImpl = (ClassDescriptorImpl) extend;
                    for (int i = 0; i < identities.length; i++) {
                        if (identities[i] == null) {
                            identities[i] = extendImpl.getIdentities()[i];
                        }
                    }
                } else {
                    // we leave things in the old way for the XML side
                    if ( identities[0] == null )
                        if ( extend.getIdentity() != null ) {
                            identities = new FieldDescriptor[] {extend.getIdentity()};
                        } else {
                            identities = new FieldDescriptor[0];
                        }
                }
            }

            // convert fieldList into array
            fields = new FieldDescriptor[fieldList.size()];
            fieldList.copyInto(fields);

            // the following check only needed by JDO side, move it to JDOMappingLoader
            /*
            if ( identities == null || identities.length == 0 ) {
                throw new MappingException( "mapping.identityMissing", clsMap.getIdentity(),
                                            javaClass.getName() );
            }*/

            // do a more general test instead
            if ( ids != null && ids.length > 0
                    && (identities == null || identities.length <= 0 ) ) {
                StringBuffer sb = new StringBuffer();
                for ( int i=0; i < ids.length; i++ ) {
                    if ( i != 0 ) sb.append("/");
                    sb.append( ids[i] );
                }
                throw new MappingException("mapping.identityMissing", sb,
                        javaClass.getName() );
            }
        }


View Full Code Here

        Class fieldType = null;
        if ( fieldMap.getType() != null ) {
            try {
                fieldType = resolveType( fieldMap.getType() );
            } catch ( ClassNotFoundException except ) {
                throw new MappingException( "mapping.classNotFound", fieldMap.getType() );
            }
        }
       
        // If the field is declared as a collection, grab the collection type as
        // well and use it to locate the field/accessor.
        CollectionHandler colHandler = null;
        if ( fieldMap.getCollection() != null ) {
            Class colType = CollectionHandlers.getCollectionType( fieldMap.getCollection().toString() );
            colHandler = CollectionHandlers.getHandler( colType );
        }

        TypeInfo typeInfo = getTypeInfo( fieldType, colHandler, fieldMap );
           
        ExtendedFieldHandler exfHandler = null;
        FieldHandler handler = null;
       
        //-- check for user supplied FieldHandler
        if (fieldMap.getHandler() != null) {
           
            Class handlerClass = null;
            try {
                handlerClass = resolveType( fieldMap.getHandler() );
            }
            catch (ClassNotFoundException except) {
                throw new MappingException( "mapping.classNotFound", fieldMap.getHandler() );
            }
           
            if (!FieldHandler.class.isAssignableFrom(handlerClass)) {
                String err = "The class '" + fieldMap.getHandler() +
                    "' must implement " + FieldHandler.class.getName();
                throw new MappingException(err);
            }
           
            //-- get default constructor to invoke. We can't use the
            //-- newInstance method unfortunately becaue FieldHandler
            //-- overloads this method
            Constructor constructor = null;
            try {
                constructor = handlerClass.getConstructor(new Class[0]);
                handler = (FieldHandler)
                    constructor.newInstance(new Object[0]);
            }
            catch(java.lang.Exception except) {
                String err = "The class '" + handlerClass.getName() +
                    "' must have a default public constructor.";
                throw new MappingException(err);
            }
           
           
            //-- ExtendedFieldHandler?
            if (handler instanceof ExtendedFieldHandler) {
View Full Code Here

            getSetCollection = CollectionHandlers.isGetSetCollection( colType );
            if ( colType == Object[].class ) {
                if (fieldType == null) {
                    String error = "'type' is a required attribute for " +
                        "field that are array collections: " + fieldName;
                    throw new MappingException(error);
                }
                Object obj = Array.newInstance(fieldType, 0);
                colType = obj.getClass();
            }
        }
       
       
        // If get/set methods not specified, use field names to determine them.
        if ( fieldMap.getDirect() ) {
            // No accessor, map field directly.
            Field field;

            field = findField( javaClass, fieldName, ( colType == null ? fieldType : colType ) );
            if ( field == null )
                throw new MappingException( "mapping.fieldNotAccessible", fieldName, javaClass.getName() );
            if ( fieldType == null )
                fieldType = field.getType();
               
            typeInfoRef.typeInfo = getTypeInfo(fieldType, colHandler, fieldMap);
           
            handler = new FieldHandlerImpl( field, typeInfoRef.typeInfo );
        }
        else {
            //-- if both methods (get/set) are not specified, then
            //-- automatically determine them.
            if ( fieldMap.getGetMethod() == null && fieldMap.getSetMethod() == null ) {
                int    point;
                Vector getSeq = new Vector();
                Vector setSeq = new Vector();
                String methodName;
                Method method;

                if ( fieldName == null )
                    throw new MappingException( "mapping.missingFieldName", javaClass.getName() );
                   
                //-- get method normally starts with "get", but
                //-- may start with "is" if it's a boolean.
                String getPrefix = GET_METHOD_PREFIX;

                try {
                   
                    //-- handle nested fields
                    while ( true ) {
                       
                        Class last;

                        point = fieldName.indexOf( '.' );
                        if ( point < 0 )
                            break;
                        last = javaClass;
                       
                       
                        // * getter for parent field *
                        String parentField = fieldName.substring(0, point);
                        methodName = GET_METHOD_PREFIX + capitalize( parentField );
                        method = javaClass.getMethod( methodName, null );                       
                       
                        fieldName = fieldName.substring( point + 1 );
                       
                        // Make sure method is not abstract/static
                        // (note: Class.getMethod() returns only public methods).
                        if ( ( method.getModifiers() & Modifier.ABSTRACT ) != 0 ||
                             ( method.getModifiers() & Modifier.STATIC ) != 0 )
                            throw new MappingException( "mapping.accessorNotAccessible",
                                                        methodName, javaClass.getName() );
                        getSeq.addElement( method );
                        javaClass = method.getReturnType();
                        // setter;   Note: javaClass already changed, use "last"
                        methodName = "set" + methodName.substring(getPrefix.length());
                        try {
                            method = last.getMethod( methodName, new Class[] { javaClass } );
                            if ( ( method.getModifiers() & Modifier.ABSTRACT ) != 0 ||
                                 ( method.getModifiers() & Modifier.STATIC ) != 0 )
                                method = null;
                        } catch ( Exception except ) {
                            method = null;
                        }
                        setSeq.addElement( method );
                    } //-- end of nested fields
                   
                    //-- save method-call sequence for nested fields
                    if ( getSeq.size() > 0 ) {
                        getSequence = new Method[ getSeq.size() ];
                        getSeq.copyInto( getSequence );
                        setSequence = new Method[ setSeq.size() ];
                        setSeq.copyInto( setSequence );
                    }
                   
                   
                    //-- find get-method for actual field
                    methodName = getPrefix + capitalize( fieldName );
                    Class returnType = (colType == null) ? fieldType : colType;
                    getMethod = findAccessor( javaClass, methodName, returnType, true);
                                         
                    //-- If getMethod is null, check for boolean type
                    //-- method prefix might be "is".
                    if (getMethod == null) {
                        if ((fieldType == Boolean.class) ||
                            (fieldType == Boolean.TYPE))
                        {
                            getPrefix = IS_METHOD_PREFIX;
                            methodName = getPrefix + capitalize( fieldName );
                            getMethod = findAccessor(javaClass, methodName,
                                    returnType, true);
                        }
                    }
                } catch ( MappingException except ) {
                    throw except;
                } catch ( Exception except ) {
                  // log.warn ("Unexpected exception", except);
                }
                if ( getMethod == null )
                    throw new MappingException( "mapping.accessorNotFound",
                                                getPrefix + capitalize( fieldName ),
                                                ( colType == null ? fieldType : colType ),
                                                javaClass.getName() );
                if ( fieldType == null && colType == null )
                    fieldType = getMethod.getReturnType();


                // We try to locate a set method anyway and we complain only if we really need one.
                    setMethod = findAccessor( javaClass, "set" + capitalize( fieldName ),
                                              ( colType == null ? fieldType : colType ), false );

                // If we have a collection that need both set and get and that
                // we don't have a set method, we fail
                if ( setMethod == null && colType != null && getSetCollection )
                        throw new MappingException( "mapping.accessorNotFound",
                                                    "set" + capitalize( fieldName ),
                                                    ( colType == null ? fieldType : colType ),
                                                    javaClass.getName() );


            } else {
                // First look up the get accessors
                if ( fieldMap.getGetMethod() != null ) {
                    getMethod = findAccessor( javaClass, fieldMap.getGetMethod(),
                                              ( colType == null ? fieldType : colType ), true );
                    if ( getMethod == null )
                        throw new MappingException( "mapping.accessorNotFound",
                                                    fieldMap.getGetMethod(), ( colType == null ? fieldType : colType ),
                                                    javaClass.getName() );
                    if ( fieldType == null && colType == null )
                        fieldType = getMethod.getReturnType();
                }

                // Second look up the set/add accessor
                if ( fieldMap.getSetMethod() != null ) {
                   
                    String methodName = fieldMap.getSetMethod();
                    Class type = fieldType;
                    if (colType != null) {
                        if (!methodName.startsWith(ADD_METHOD_PREFIX))
                            type = colType;
                    }
                   
                    //-- set via constructor?
                    if (methodName.startsWith("%")) {
                        //-- validate index value
                        String sIdx = methodName.substring(1);
                        int index = 0;
                        try {
                            index = Integer.parseInt(sIdx);
                        }
                        catch(NumberFormatException nfe) {
                            throw new MappingException("mapping.invalidParameterIndex", sIdx);
                        }
                        if ((index < 1) || (index > 9)) {
                            throw new MappingException("mapping.invalidParameterIndex", sIdx);
                        }
                    }
                    else {
                        setMethod = findAccessor( javaClass, fieldMap.getSetMethod(),
                                                type , false );
                        if ( setMethod == null )
                            throw new MappingException( "mapping.accessorNotFound",
                                                        fieldMap.getSetMethod(), type,
                                                        javaClass.getName() );
                        if ( fieldType == null )
                            fieldType = setMethod.getParameterTypes()[ 0 ];
                    }
                }
            }

            typeInfoRef.typeInfo = getTypeInfo( fieldType, colHandler, fieldMap );
             
            fieldName = fieldMap.getName(); // Not the same for nested fields
            if ( fieldName == null )
                fieldName = ( getMethod == null ? setMethod.getName() : getMethod.getName() );
               
            //-- create handler
            handler = new FieldHandlerImpl( fieldName,
                                            getSequence,
                                            setSequence,
                                            getMethod,
                                            setMethod,
                                            typeInfoRef.typeInfo );

            if ((setMethod != null) && (setMethod.getName().startsWith(ADD_METHOD_PREFIX)))
                handler.setAddMethod(setMethod);
        }

        // If there is a create method, add it to the field handler
        if ( fieldMap.getCreateMethod() != null ) {
            try {
                Method method;

                method = javaClass.getMethod( fieldMap.getCreateMethod(), null );
                handler.setCreateMethod( method );
            } catch ( Exception except ) {
                // No such/access to method
                throw new MappingException( "mapping.createMethodNotFound",
                                            fieldMap.getCreateMethod(), javaClass.getName() );
            }
        } else if ( fieldName != null && ! Types.isSimpleType( fieldType ) ) {
            try {
                Method method;
View Full Code Here

            // Look up the field based on its name, make sure it's only modifier
            // is public. If a type was specified, match the field type.
            field = javaClass.getField( fieldName );
            if ( field.getModifiers() != Modifier.PUBLIC &&
                 field.getModifiers() != ( Modifier.PUBLIC | Modifier.VOLATILE ) )
                throw new MappingException( "mapping.fieldNotAccessible", fieldName, javaClass.getName() );
            if ( fieldType == null )
                fieldType = Types.typeFromPrimitive( field.getType() );
            else if ( fieldType != java.io.Serializable.class
                    && Types.typeFromPrimitive( fieldType ) != Types.typeFromPrimitive( field.getType() ) )
                throw new MappingException( "mapping.fieldTypeMismatch", field, fieldType.getName() );
            return field;
        } catch ( NoSuchFieldException except ) {
        } catch ( SecurityException except ) {
        }
        return null;
View Full Code Here

                    if (fieldType.isInterface() ||
                        ((fieldType.getModifiers() & Modifier.ABSTRACT) != 0) ||
                        (fieldType == java.io.Serializable.class))
                    {
                        if ( ! fieldType.isAssignableFrom( returnType ) )
                        throw new MappingException("mapping.accessorReturnTypeMismatch",
                                                    method, fieldType.getName() );
                    }
                    else {
                        if ( ! returnType.isAssignableFrom( fieldType ) )
                        throw new MappingException("mapping.accessorReturnTypeMismatch",
                                                    method, fieldType.getName() );
                    }
                }                   
            }
            else {
                method = null;
                fieldTypeFromPrimitive = null;
                // Set method: look for the named method or prepend set to the
                // method name. If the field type is know, look up a suitable
                // method. If the fielf type is unknown, lookup the first
                // method with that name and one parameter.
                if ( fieldType != null ) {
                    fieldTypeFromPrimitive = Types.typeFromPrimitive( fieldType );
                    try {
                        method = javaClass.getMethod( methodName, new Class[] { fieldType } );
                    } catch ( Exception except ) {
                        try {
                            method = javaClass.getMethod( methodName, new Class[] { fieldTypeFromPrimitive } );
                        } catch ( Exception except2 ) {
                          // log.warn ("Unexpected exception", except2);
                        }
                    }
                }
                if ( null == method ) {
                    methods = javaClass.getMethods();
                    method = null;
                    for ( i = 0 ; i < methods.length ; ++i ) {
                        if ( methods[ i ].getName().equals( methodName ) ) {
                            parameterTypes = methods[ i ].getParameterTypes();
                            if (parameterTypes.length != 1) continue;
                           
                            Class paramType = Types.typeFromPrimitive( parameterTypes[0] );
                           
                            //-- check straight match
                            if ((fieldType == null) ||
                                 paramType.isAssignableFrom( fieldTypeFromPrimitive ))
                            {
                                method = methods[ i ];
                                break;
                            }
                            //-- Check against whether the declared type is
                            //-- an interface or abstract class.
                            else if (fieldType.isInterface() ||
                                    ((fieldType.getModifiers() & Modifier.ABSTRACT) != 0))
                            {
                                if (fieldTypeFromPrimitive.isAssignableFrom( paramType ))
                                {
                                    method = methods[i];
                                    break;
                                }
                                   
                            }
                        }
                    }
                    if ( method == null )
                        return null;
                }
            }
            // Make sure method is public and not static.
            // (note: Class.getMethod() returns only public methods).
            //if ( ( method.getModifiers() & Modifier.ABSTRACT ) != 0 ||
            if ( ( method.getModifiers() & Modifier.STATIC ) != 0 )
                throw new MappingException( "mapping.accessorNotAccessible",
                                            methodName, javaClass.getName() );
            return method;
        } catch ( MappingException except ) {
            throw except;
        } catch ( Exception except ) {
View Full Code Here

                        ((XMLClassDescriptorImpl)referenceDesc).setExtends(null);
                    }
                } catch (MarshalException mx) {
                    String error = "unable to introspect class '" +
                        type.getName() + "' for auto-complete: ";
                    throw new MappingException(error + mx.getMessage());
                }
            }

            //-- check for identity
            String identity = "";
View Full Code Here

        String namespace = null;
        if ((xmlName != null) && (xmlName.length() > 0)){
            if (xmlName.charAt(0) == '{') {
                int idx = xmlName.indexOf('}');
                if (idx < 0) {
                    throw new MappingException("Invalid QName: " + xmlName);
                }
                namespace = xmlName.substring(1, idx);
                xmlName = xmlName.substring(idx+1);
            }
            else if (xmlName.startsWith(XML_PREFIX)) {
                namespace = Namespaces.XML_NAMESPACE;
                xmlName = xmlName.substring(4);
            }
        }

        if (nodeType == null) {
            if (isPrimitive(javaClass))
                nodeType = _primitiveNodeType;
            else
                nodeType = NodeType.Element;
        }

        //-- Create XML name if necessary. Note if name is to be derived
        //-- by class..we just make sure we set the name to null...
        //-- the Marshaller does this during runtime. This allows
        //-- Collections to be handled properly.
        if ((!deriveNameByClass) && ((xmlName == null) && (match == null)))
        {
            xmlName = _naming.toXMLName( fieldDesc.getFieldName() );
            match = xmlName + ' ' + fieldDesc.getFieldName();
        }

        xmlDesc = new XMLFieldDescriptorImpl( fieldDesc, xmlName, nodeType, _primitiveNodeType );

        //-- transient?
        xmlDesc.setTransient(isXMLTransient);

        //--set a default fieldValidator
        xmlDesc.setValidator(new FieldValidator());
       
        //-- enable use parent namespace if explicit one doesn't exist
        xmlDesc.setUseParentsNamespace(true);

        //-- If deriveNameByClass we need to reset the name to
        //-- null because XMLFieldDescriptorImpl tries to be smart
        //-- and automatically creates the name.
        if (deriveNameByClass) {
            xmlDesc.setXMLName(null);
        }

        //-- namespace
        if (namespace != null) {
            xmlDesc.setNameSpaceURI(namespace);
        }

        //-- matches
        if (match != null) {
            xmlDesc.setMatches(match);
            //-- special fix for xml-name since XMLFieldDescriptorImpl
            //-- will create a default name based off the field name
            if (xmlName == null) xmlDesc.setXMLName(null);
        }

        //-- reference
        xmlDesc.setReference(isReference);

        xmlDesc.setContainer(fieldMap.getContainer());

        if (xml != null) {
           
            //-- has class descriptor for type specified
            if (xml.getClassMapping() != null) {
                ClassDescriptor cd = createDescriptor(xml.getClassMapping());
                xmlDesc.setClassDescriptor((XMLClassDescriptor)cd);
            }
           
            //-- has location path?
            if (xml.getLocation() != null) {
                xmlDesc.setLocationPath(xml.getLocation());
            }
            //is the value type needs specific handling
            //such as QName or NCName support?
            String xmlType = xml.getType();
            xmlDesc.setSchemaType(xmlType);
            xmlDesc.setQNamePrefix(xml.getQNamePrefix());
            TypeValidator validator = null;
            if (NCNAME.equals(xmlType)) {
                validator = new NameValidator(NameValidator.NCNAME);
                xmlDesc.setValidator(new FieldValidator(validator));
            }
           
            //-- special properties?
            Property[] props = xml.getProperty();
            if ((props != null) && (props.length > 0)) {
                for (int pIdx = 0; pIdx < props.length; pIdx++) {
                    Property prop = props[pIdx];
                    xmlDesc.setProperty(prop.getName(), prop.getValue());
                }
            }
        }

        //-- Get collection type
        if (colType == null) {
            //-- just in case user forgot to use collection="..."
            //-- in the mapping file
            Class type = fieldDesc.getFieldType();
            if (CollectionHandlers.hasHandler(type)) {
                String typeName = CollectionHandlers.getCollectionName(type);
                colType = CollectionType.valueOf(typeName);
            }
        }
       
        //-- isMapped item
        if (colType != null) {   
            if ((colType == CollectionType.HASHTABLE) ||
                (colType == CollectionType.MAP))
            {
                //-- Make sure user is not using an addMethod
                //-- before setting the mapped field to true.
                String methodName = fieldMap.getSetMethod();
                if (methodName != null) {
                    if (!methodName.startsWith("add")) {
                        xmlDesc.setMapped(true);
                    }
                }
                else xmlDesc.setMapped(true);
            }

           
            //-- special NodeType.Namespace handling
            //-- prevent FieldHandlerImpl from using CollectionHandler
            //-- during calls to #getValue
            if ((nodeType == NodeType.Namespace) || (xmlDesc.isMapped())) {
                Object handler = xmlDesc.getHandler();
                if (handler instanceof FieldHandlerImpl) {
                    FieldHandlerImpl handlerImpl = (FieldHandlerImpl)handler;
                    handlerImpl.setConvertFrom(new IdentityConvertor());
                }
            }
            //-- wrap collection in element?
            if (nodeType == NodeType.Element) {
                if (fieldMap.hasContainer() && (!fieldMap.getContainer())) {
                    xmlDesc = wrapCollection(xmlDesc);
                }
            }
        }
        //-- is Type-Safe Enumeration?
        //-- This is not very clean, we should have a way
        //-- to specify something is a type-safe enumeration
        //-- without having to guess.
        else if ((!isReference) && (!isXMLTransient)) {
            Class fieldType = xmlDesc.getFieldType();
            if (!isPrimitive(fieldType)) {
                //-- make sure no default constructor
                Constructor cons = null;
                try {
                    cons = fieldType.getConstructor(EMPTY_ARGS);
                    if (!Modifier.isPublic(cons.getModifiers())) {
                        cons = null;
                    }
                }
                catch(NoSuchMethodException nsmx) {
                    //-- Do nothing
                }
                try {
                    if (cons == null) {
                        //-- make sure a valueOf factory method
                        //-- exists and no user specified handler exists
                        Method method = fieldType.getMethod(VALUE_OF, STRING_ARG);
                        Class returnType = method.getReturnType();
                        if ((returnType != null) && fieldType.isAssignableFrom(returnType)) {
                            if (fieldMap.getHandler() == null) {
                                //-- Use EnumFieldHandler
                                FieldHandler handler = xmlDesc.getHandler();
                                handler = new EnumFieldHandler(fieldType, handler);
                                xmlDesc.setHandler(handler);
                                xmlDesc.setImmutable(true);
                            }
                        }
                    }
                }
                catch(NoSuchMethodException nsmx) {
                    //-- Do nothing
                }
            }
        }
       
        //-- constructor argument?
        String setter = fieldMap.getSetMethod();
        if (setter != null) {
            if (setter.startsWith("%")) {
                int index = 0;
                setter = setter.substring(1);
                index = Integer.parseInt(setter);
                if ((index < 1) || (index > 9)) {
                    throw new MappingException("mapper.invalidParameterIndex", setter);
                }
                //-- adjust index to base zero
                --index;
                xmlDesc.setConstructorArgumentIndex(index);
            }
View Full Code Here

        Method method = null;
        try {
            method = type.getMethod(METHOD_NAME, ARGS);
        }
        catch(java.lang.NoSuchMethodException nsme) {
            throw new MappingException(nsme);
        }
        if (!Modifier.isStatic(method.getModifiers())) {
            String err = "No static method '" + METHOD_NAME;
            err += "' found in class: " + type.getName();
            throw new MappingException(err);
        }
       
        //-- check return type?
       
        _valueOf = method;
View Full Code Here

TOP

Related Classes of org.exolab.castor.mapping.MappingException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.