Search in sources :

Example 61 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project herd by FINRAOS.

the class DefinitionGenerator method processField.

/**
 * Processes a Field of a model class which can be converted into a Swagger definition property. The property is added into the given model. This method may
 * be called recursively.
 *
 * @param field the field to process.
 * @param model model the model.
 *
 * @throws MojoExecutionException if any problems were encountered.
 */
private void processField(Field field, ModelImpl model) throws MojoExecutionException {
    log.debug("Processing field \"" + field.getName() + "\".");
    if (!Modifier.isStatic(field.getModifiers())) {
        Property property;
        Class<?> fieldClass = field.getType();
        if (Collection.class.isAssignableFrom(fieldClass)) {
            property = new ArrayProperty(getPropertyFromType(FieldUtils.getCollectionType(field)));
        } else {
            property = getPropertyFromType(fieldClass);
        }
        // Set the required field based on the XmlElement that comes from the XSD.
        XmlElement xmlElement = field.getAnnotation(XmlElement.class);
        if (xmlElement != null) {
            property.setRequired(xmlElement.required());
        }
        if (xsdParser != null) {
            property.setDescription(xsdParser.getAnnotation(model.getName(), field.getName()));
        }
        // Set the property on model.
        model.property(field.getName(), property);
    }
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) XmlElement(javax.xml.bind.annotation.XmlElement) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) DateTimeProperty(io.swagger.models.properties.DateTimeProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) LongProperty(io.swagger.models.properties.LongProperty) RefProperty(io.swagger.models.properties.RefProperty) DecimalProperty(io.swagger.models.properties.DecimalProperty) Property(io.swagger.models.properties.Property)

Example 62 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project ibas-framework by color-coding.

the class Serializer method getSerializedElements.

private List<SchemaElement> getSerializedElements(Field[] fields) {
    List<SchemaElement> elements = new ArrayList<>();
    for (Field field : fields) {
        Class<?> elementType = field.getType();
        String elementName = field.getName();
        String wrapperName = null;
        XmlElementWrapper xmlWrapper = field.getAnnotation(XmlElementWrapper.class);
        if (xmlWrapper != null) {
            // 首先判断是否为数组元素
            wrapperName = xmlWrapper.name();
        }
        XmlElement xmlElement = field.getAnnotation(XmlElement.class);
        if (xmlElement != null) {
            if (!xmlElement.name().equals("##default")) {
                elementName = xmlElement.name();
            }
            if (xmlElement.type() != null && !xmlElement.type().getName().startsWith(XmlElement.class.getName())) {
                elementType = xmlElement.type();
            }
        } else {
            continue;
        }
        if (elementName == null) {
            continue;
        }
        if (elementType == null) {
            continue;
        }
        elements.add(new SchemaElement(elementName, wrapperName, elementType));
    }
    return elements;
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) XmlElement(javax.xml.bind.annotation.XmlElement) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper)

Example 63 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project ddf by codice.

the class AdaptedSourceResponse method getMetacard.

@XmlElement(namespace = METACARD_URI)
public List<MetacardElement> getMetacard() {
    List<MetacardElement> metacards = new ArrayList<MetacardElement>();
    for (Result r : delegate.getResults()) {
        Metacard metacard = r.getMetacard();
        if (metacard == null) {
            continue;
        }
        MetacardElement element = new MetacardElement();
        element.setId(metacard.getId());
        element.setSource(metacard.getSourceId());
        if (metacard.getMetacardType() != null) {
            String metacardTypeName = MetacardImpl.BASIC_METACARD.getName();
            if (isNotBlank(metacard.getMetacardType().getName())) {
                metacardTypeName = metacard.getMetacardType().getName();
            }
            element.setType(metacardTypeName);
            AttributeAdapter attributeAdapter = new AttributeAdapter(metacard.getMetacardType());
            for (AttributeDescriptor descriptor : metacard.getMetacardType().getAttributeDescriptors()) {
                try {
                    element.getAttributes().add(attributeAdapter.marshal(metacard.getAttribute(descriptor.getName())));
                } catch (CatalogTransformerException e) {
                    LOGGER.info("Marshalling error with attribute", e);
                }
            }
        }
        metacards.add(element);
    }
    return metacards;
}
Also used : Metacard(ddf.catalog.data.Metacard) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardElement(ddf.catalog.transformer.xml.binding.MetacardElement) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Result(ddf.catalog.data.Result) XmlElement(javax.xml.bind.annotation.XmlElement)

Example 64 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project tomee by apache.

the class JAXBDataBinding method createWrapperHelper.

public WrapperHelper createWrapperHelper(Class<?> wrapperType, QName wrapperName, List<String> partNames, List<String> elTypeNames, List<Class<?>> partClasses) {
    List<Method> getMethods = new ArrayList<>(partNames.size());
    List<Method> setMethods = new ArrayList<>(partNames.size());
    List<Method> jaxbMethods = new ArrayList<>(partNames.size());
    List<Field> fields = new ArrayList<>(partNames.size());
    Method[] allMethods = wrapperType.getMethods();
    String packageName = PackageUtils.getPackageName(wrapperType);
    // if wrappertype class is generated by ASM, getPackage() always return null
    if (wrapperType.getPackage() != null) {
        packageName = wrapperType.getPackage().getName();
    }
    String objectFactoryClassName = packageName + ".ObjectFactory";
    Object objectFactory = null;
    try {
        objectFactory = wrapperType.getClassLoader().loadClass(objectFactoryClassName).newInstance();
    } catch (Exception e) {
    // ignore, probably won't need it
    }
    Method[] allOFMethods;
    if (objectFactory != null) {
        allOFMethods = objectFactory.getClass().getMethods();
    } else {
        allOFMethods = new Method[0];
    }
    for (int x = 0; x < partNames.size(); x++) {
        String partName = partNames.get(x);
        if (partName == null) {
            getMethods.add(null);
            setMethods.add(null);
            fields.add(null);
            jaxbMethods.add(null);
            continue;
        }
        String elementType = elTypeNames.get(x);
        String getAccessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.GETTER);
        String setAccessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.SETTER);
        Method getMethod = null;
        Method setMethod = null;
        Class<?> valueClass = wrapperType;
        try {
            getMethod = valueClass.getMethod(getAccessor, AbstractWrapperHelper.NO_CLASSES);
        } catch (NoSuchMethodException ex) {
        // ignore for now
        }
        Field elField = getElField(partName, valueClass);
        if (getMethod == null && elementType != null && "boolean".equalsIgnoreCase(elementType) && (elField == null || (!Collection.class.isAssignableFrom(elField.getType()) && !elField.getType().isArray()))) {
            try {
                String newAcc = getAccessor.replaceFirst("get", "is");
                getMethod = wrapperType.getMethod(newAcc, AbstractWrapperHelper.NO_CLASSES);
            } catch (NoSuchMethodException ex) {
            // ignore for now
            }
        }
        if (getMethod == null && "return".equals(partName)) {
            // RI generated code uses this
            try {
                getMethod = valueClass.getMethod("get_return", AbstractWrapperHelper.NO_CLASSES);
            } catch (NoSuchMethodException ex) {
                try {
                    getMethod = valueClass.getMethod("is_return", new Class[0]);
                } catch (NoSuchMethodException ex2) {
                // ignore for now
                }
            }
        }
        if (getMethod == null && elField != null) {
            getAccessor = JAXBUtils.nameToIdentifier(elField.getName(), JAXBUtils.IdentifierType.GETTER);
            setAccessor = JAXBUtils.nameToIdentifier(elField.getName(), JAXBUtils.IdentifierType.SETTER);
            try {
                getMethod = valueClass.getMethod(getAccessor, AbstractWrapperHelper.NO_CLASSES);
            } catch (NoSuchMethodException ex) {
            // ignore for now
            }
        }
        String setAccessor2 = setAccessor;
        if ("return".equals(partName)) {
            // some versions of jaxb map "return" to "set_return" instead of "setReturn"
            setAccessor2 = "set_return";
        }
        for (Method method : allMethods) {
            if (method.getParameterTypes() != null && method.getParameterTypes().length == 1 && (setAccessor.equals(method.getName()) || setAccessor2.equals(method.getName()))) {
                setMethod = method;
                break;
            }
        }
        getMethods.add(getMethod);
        setMethods.add(setMethod);
        if (setMethod != null && JAXBElement.class.isAssignableFrom(setMethod.getParameterTypes()[0])) {
            Type t = setMethod.getGenericParameterTypes()[0];
            Class<?> pcls = null;
            if (t instanceof ParameterizedType) {
                t = ((ParameterizedType) t).getActualTypeArguments()[0];
            }
            if (t instanceof Class) {
                pcls = (Class<?>) t;
            }
            String methodName = "create" + wrapperType.getSimpleName() + setMethod.getName().substring(3);
            for (Method m : allOFMethods) {
                if (m.getName().equals(methodName) && m.getParameterTypes().length == 1 && (pcls == null || pcls.equals(m.getParameterTypes()[0]))) {
                    jaxbMethods.add(m);
                }
            }
        } else {
            jaxbMethods.add(null);
        }
        if (elField != null) {
            // JAXB Type get XmlElement Annotation
            XmlElement el = elField.getAnnotation(XmlElement.class);
            if (el != null && (partName.equals(el.name()) || "##default".equals(el.name()))) {
                ReflectionUtil.setAccessible(elField);
                fields.add(elField);
            } else {
                if (getMethod == null && setMethod == null) {
                    if (el != null) {
                        LOG.warning("Could not create accessor for property " + partName + " of type " + wrapperType.getName() + " as the @XmlElement " + "defines the name as " + el.name());
                    } else {
                        LOG.warning("Could not create accessor for property " + partName + " of type " + wrapperType.getName());
                    }
                }
                fields.add(null);
            }
        } else {
            fields.add(null);
        }
    }
    return createWrapperHelper(getBus(), wrapperType, setMethods.toArray(new Method[0]), getMethods.toArray(new Method[0]), jaxbMethods.toArray(new Method[0]), fields.toArray(new Field[0]), objectFactory);
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) JAXBElement(javax.xml.bind.JAXBElement) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) XmlElement(javax.xml.bind.annotation.XmlElement)

Example 65 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project tomee by apache.

the class JAXBDataBinding method getElField.

private static Field getElField(String partName, final Class<?> wrapperType) {
    String fieldName = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.VARIABLE);
    Field[] fields = ReflectionUtil.getDeclaredFields(wrapperType);
    for (Field field : fields) {
        XmlElement el = field.getAnnotation(XmlElement.class);
        if (el != null && partName.equals(el.name())) {
            return field;
        }
        XmlElementRef xmlElementRefAnnotation = field.getAnnotation(XmlElementRef.class);
        if (xmlElementRefAnnotation != null && partName.equals(xmlElementRefAnnotation.name())) {
            return field;
        }
        if (field.getName().equals(fieldName)) {
            return field;
        }
    }
    return null;
}
Also used : XmlElementRef(javax.xml.bind.annotation.XmlElementRef) Field(java.lang.reflect.Field) XmlElement(javax.xml.bind.annotation.XmlElement)

Aggregations

XmlElement (javax.xml.bind.annotation.XmlElement)85 Test (org.junit.Test)31 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)29 Element (com.zimbra.common.soap.Element)28 JSONElement (com.zimbra.common.soap.Element.JSONElement)28 XMLElement (com.zimbra.common.soap.Element.XMLElement)28 FilterTest (com.zimbra.soap.mail.type.FilterTest)26 Field (java.lang.reflect.Field)17 ArrayList (java.util.ArrayList)14 Method (java.lang.reflect.Method)13 XmlAttribute (javax.xml.bind.annotation.XmlAttribute)13 XmlElementWrapper (javax.xml.bind.annotation.XmlElementWrapper)13 XmlElements (javax.xml.bind.annotation.XmlElements)10 XmlElementRef (javax.xml.bind.annotation.XmlElementRef)8 KeyValuePair (com.zimbra.soap.type.KeyValuePair)7 Annotation (java.lang.annotation.Annotation)6 Type (java.lang.reflect.Type)6 ParameterizedType (java.lang.reflect.ParameterizedType)4 List (java.util.List)4 XmlList (javax.xml.bind.annotation.XmlList)4