Search in sources :

Example 56 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class Java5TypeCreator method getOrCreateParameterizedType.

protected AegisType getOrCreateParameterizedType(TypeClassInfo generic, int index, boolean map) {
    Type paramType;
    Map<String, Type> pm = generic.getTypeVars();
    if (map) {
        if (pm == null) {
            pm = new HashMap<>();
        } else {
            pm = new HashMap<>(pm);
        }
        paramType = getComponentTypeForMap(generic.getType(), pm, index == 0);
    } else {
        paramType = getComponentType(generic.getType(), index);
    }
    if (paramType instanceof WildcardType) {
        WildcardType wct = (WildcardType) paramType;
        paramType = wct.getUpperBounds()[0];
    }
    if (paramType instanceof TypeVariable) {
        TypeVariable<?> v = (TypeVariable<?>) paramType;
        LOG.log(Level.WARNING, "Could not map TypeVariable named {0} from {1} with initial mapping {2} to " + "a known class.  Using Object.", new Object[] { v.getName(), generic.getType().toString(), generic.getTypeVars() });
    }
    if (paramType == null) {
        return createObjectType();
    }
    /* null arises when the index-th parameter to generic is something list List<T> */
    Class<?> clazz = TypeUtil.getTypeRelatedClass(paramType);
    if (clazz == null) {
        return createObjectType();
    }
    if (!Collection.class.isAssignableFrom(clazz) && !Map.class.isAssignableFrom(clazz)) {
        return getTopCreator().createType(clazz);
    }
    TypeClassInfo info = createBasicClassInfo(clazz);
    info.setDescription(clazz.toString());
    info.setType(paramType, paramType instanceof ParameterizedType ? pm : null);
    return createTypeForClass(info);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) BeanType(org.apache.cxf.aegis.type.basic.BeanType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) AegisType(org.apache.cxf.aegis.type.AegisType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) TypeClassInfo(org.apache.cxf.aegis.type.TypeClassInfo)

Example 57 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class ObjectType method readObject.

@Override
public Object readObject(MessageReader reader, Context context) throws DatabindingException {
    if (isNil(reader.getAttributeReader(XSI_NIL))) {
        while (reader.hasMoreElementReaders()) {
            reader.getNextElementReader();
        }
        return null;
    }
    MessageReader typeReader = reader.getAttributeReader(XSI_TYPE);
    if (null == typeReader && !readToDocument) {
        throw new DatabindingException("Missing 'xsi:type' attribute");
    }
    String typeName = null;
    if (typeReader != null) {
        typeName = typeReader.getValue();
    }
    if (null == typeName && !readToDocument) {
        throw new DatabindingException("Missing 'xsi:type' attribute value");
    }
    final QName typeQName;
    if (typeName != null) {
        typeName = typeName.trim();
        typeQName = extractQName(reader, typeName);
    } else {
        typeQName = reader.getName();
    }
    TypeMapping tm = context.getTypeMapping();
    if (tm == null) {
        tm = getTypeMapping();
    }
    AegisType type = tm.getType(typeQName);
    if (type == null) {
        type = tm.getType(getSchemaType());
    }
    if (type == this) {
        throw new DatabindingException("Could not determine how to read type: " + typeQName);
    }
    if (type == null && readToDocument) {
        type = getTypeMapping().getType(Document.class);
    }
    if (null == type) {
        throw new DatabindingException("No mapped type for '" + typeName + "' (" + typeQName + ")");
    }
    return type.readObject(reader, context);
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) TypeMapping(org.apache.cxf.aegis.type.TypeMapping) MessageReader(org.apache.cxf.aegis.xml.MessageReader) Document(org.w3c.dom.Document)

Example 58 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class ObjectType method writeObject.

@Override
public void writeObject(Object object, MessageWriter writer, Context context) throws DatabindingException {
    if (null == object) {
        MessageWriter nilWriter = writer.getAttributeWriter(XSI_NIL);
        nilWriter.writeValue("true");
        nilWriter.close();
    } else {
        AegisType type = determineType(context, object.getClass());
        if (null == type) {
            TypeMapping tm = context.getTypeMapping();
            if (tm == null) {
                tm = getTypeMapping();
            }
            type = tm.getTypeCreator().createType(object.getClass());
            tm.register(type);
        }
        writer.writeXsiType(type.getSchemaType());
        boolean nextIsBeanType = type instanceof BeanType;
        if (nextIsBeanType) {
            ((BeanType) type).writeObjectFromObjectType(object, writer, context, true);
        } else {
            type.writeObject(object, writer, context);
        }
    }
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter) TypeMapping(org.apache.cxf.aegis.type.TypeMapping)

Example 59 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class ObjectType method determineType.

public AegisType determineType(Context context, Class<?> clazz) {
    TypeMapping tm = context.getTypeMapping();
    if (tm == null) {
        tm = getTypeMapping();
    }
    AegisType type = tm.getType(clazz);
    if (null != type) {
        return type;
    }
    Class<?>[] interfaces = clazz.getInterfaces();
    for (int i = 0; i < interfaces.length; i++) {
        Class<?> anInterface = interfaces[i];
        type = tm.getType(anInterface);
        if (null != type) {
            return type;
        }
    }
    Class<?> superclass = clazz.getSuperclass();
    if (null == superclass || Object.class.equals(superclass)) {
        return null;
    }
    return determineType(context, superclass);
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType) TypeMapping(org.apache.cxf.aegis.type.TypeMapping)

Example 60 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class SoapArrayType method getComponentType.

/**
 * Get the <code>AegisType</code> of the elements in the array.  This is only used for writing an array.
 * When reading the type is solely determined by the required arrayType soap attribute.
 */
public AegisType getComponentType() {
    Class<?> compType = getTypeClass().getComponentType();
    AegisType type;
    if (componentName == null) {
        type = getTypeMapping().getType(compType);
    } else {
        type = getTypeMapping().getType(componentName);
        // below instead.
        if (type == null) {
            LOG.finest("Couldn't find array component type " + componentName + ". Creating one instead.");
        }
    }
    if (type == null) {
        type = getTypeMapping().getTypeCreator().createType(compType);
        getTypeMapping().register(type);
    }
    return type;
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType)

Aggregations

AegisType (org.apache.cxf.aegis.type.AegisType)97 QName (javax.xml.namespace.QName)43 Test (org.junit.Test)40 AbstractAegisTest (org.apache.cxf.aegis.AbstractAegisTest)35 DatabindingException (org.apache.cxf.aegis.DatabindingException)18 AegisContext (org.apache.cxf.aegis.AegisContext)16 TypeMapping (org.apache.cxf.aegis.type.TypeMapping)11 Element (org.w3c.dom.Element)11 Context (org.apache.cxf.aegis.Context)10 BeanType (org.apache.cxf.aegis.type.basic.BeanType)10 Method (java.lang.reflect.Method)9 MessageReader (org.apache.cxf.aegis.xml.MessageReader)9 TypeCreationOptions (org.apache.cxf.aegis.type.TypeCreationOptions)8 BeanTypeInfo (org.apache.cxf.aegis.type.basic.BeanTypeInfo)8 CollectionType (org.apache.cxf.aegis.type.collection.CollectionType)8 HashSet (java.util.HashSet)7 MapType (org.apache.cxf.aegis.type.collection.MapType)7 MessageWriter (org.apache.cxf.aegis.xml.MessageWriter)7 ElementReader (org.apache.cxf.aegis.xml.stax.ElementReader)7 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)5