Search in sources :

Example 21 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project winery by eclipse.

the class ReflectionUtil method getFieldName.

/**
 * Checks a field for XmlElement or XmlAttribute annotations
 *
 * @return a pair of name and field
 */
@NonNull
private Pair<String, Field> getFieldName(Field field) {
    XmlAttribute xmlAttribute = field.getAnnotation(XmlAttribute.class);
    XmlElement xmlElement = field.getAnnotation(XmlElement.class);
    String name = field.getName();
    if (Objects.nonNull(xmlAttribute) && !xmlAttribute.name().equals("##default")) {
        name = xmlAttribute.name();
    } else if (Objects.nonNull(xmlElement) && !xmlElement.name().equals("##default")) {
        name = xmlElement.name();
    }
    return Tuples.pair(name, field);
}
Also used : XmlAttribute(javax.xml.bind.annotation.XmlAttribute) XmlElement(javax.xml.bind.annotation.XmlElement) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 22 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project cxf 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;
        }
        if (field.getName().equals(fieldName)) {
            return field;
        }
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) XmlElement(javax.xml.bind.annotation.XmlElement)

Example 23 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project cxf 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".equals(elementType.toLowerCase()) && (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(wrapperType, setMethods.toArray(new Method[setMethods.size()]), getMethods.toArray(new Method[getMethods.size()]), jaxbMethods.toArray(new Method[jaxbMethods.size()]), fields.toArray(new Field[fields.size()]), 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 24 with XmlElement

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

the class JAXBSchemaInitializer method buildExceptionType.

private void buildExceptionType(MessagePartInfo part, Class<?> cls) {
    SchemaInfo schemaInfo = null;
    for (SchemaInfo s : serviceInfo.getSchemas()) {
        if (s.getNamespaceURI().equals(part.getElementQName().getNamespaceURI())) {
            schemaInfo = s;
            break;
        }
    }
    XmlAccessorOrder xmlAccessorOrder = cls.getAnnotation(XmlAccessorOrder.class);
    XmlType xmlTypeAnno = cls.getAnnotation(XmlType.class);
    String[] propertyOrder = null;
    boolean respectXmlTypeNS = false;
    XmlSchema faultBeanSchema = null;
    if (xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.namespace()) && !xmlTypeAnno.namespace().equals(part.getElementQName().getNamespaceURI())) {
        respectXmlTypeNS = true;
        NamespaceMap nsMap = new NamespaceMap();
        nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, xmlTypeAnno.namespace());
        nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
        SchemaInfo faultBeanSchemaInfo = createSchemaIfNeeded(xmlTypeAnno.namespace(), nsMap);
        faultBeanSchema = faultBeanSchemaInfo.getSchema();
    }
    if (xmlTypeAnno != null && xmlTypeAnno.propOrder().length > 0) {
        propertyOrder = xmlTypeAnno.propOrder();
    // TODO: handle @XmlAccessOrder
    }
    XmlSchema schema = null;
    if (schemaInfo == null) {
        NamespaceMap nsMap = new NamespaceMap();
        nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, part.getElementQName().getNamespaceURI());
        nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
        schemaInfo = createSchemaIfNeeded(part.getElementQName().getNamespaceURI(), nsMap);
    }
    schema = schemaInfo.getSchema();
    // Before updating everything, make sure we haven't added this
    // type yet.  Multiple methods that throw the same exception
    // types will cause duplicates.
    String faultTypeName = xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.name()) ? xmlTypeAnno.name() : part.getElementQName().getLocalPart();
    XmlSchemaType existingType = schema.getTypeByName(faultTypeName);
    if (existingType != null) {
        return;
    }
    XmlSchemaElement el = new XmlSchemaElement(schema, true);
    el.setName(part.getElementQName().getLocalPart());
    part.setXmlSchema(el);
    schemaInfo.setElement(null);
    if (respectXmlTypeNS) {
        // create complexType in the new created schema for xmlType
        schema = faultBeanSchema;
    }
    XmlSchemaComplexType ct = new XmlSchemaComplexType(schema, true);
    ct.setName(faultTypeName);
    el.setSchemaTypeName(ct.getQName());
    XmlSchemaSequence seq = new XmlSchemaSequence();
    ct.setParticle(seq);
    String namespace = part.getElementQName().getNamespaceURI();
    XmlAccessType accessType = Utils.getXmlAccessType(cls);
    // 
    for (Field f : Utils.getFields(cls, accessType)) {
        // map field
        Type type = Utils.getFieldType(f);
        // generic return type.
        if ((type == null) && (f.getGenericType() instanceof ParameterizedType)) {
            type = f.getGenericType();
        }
        if (generateGenericType(type)) {
            buildGenericElements(schema, seq, f);
        } else {
            JAXBBeanInfo beanInfo = getBeanInfo(type);
            if (beanInfo != null) {
                XmlElement xmlElementAnno = f.getAnnotation(XmlElement.class);
                addElement(schema, seq, beanInfo, new QName(namespace, f.getName()), isArray(type), xmlElementAnno);
            }
        }
    }
    for (Method m : Utils.getGetters(cls, accessType)) {
        // map method
        Type type = Utils.getMethodReturnType(m);
        // generic return type.
        if ((type == null) && (m.getGenericReturnType() instanceof ParameterizedType)) {
            type = m.getGenericReturnType();
        }
        if (generateGenericType(type)) {
            buildGenericElements(schema, seq, m, type);
        } else {
            JAXBBeanInfo beanInfo = getBeanInfo(type);
            if (beanInfo != null) {
                int idx = m.getName().startsWith("get") ? 3 : 2;
                String name = m.getName().substring(idx);
                name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
                XmlElement xmlElementAnno = m.getAnnotation(XmlElement.class);
                addElement(schema, seq, beanInfo, new QName(namespace, name), isArray(type), xmlElementAnno);
            }
        }
    }
    // Create element in xsd:sequence for Exception.class
    if (Exception.class.isAssignableFrom(cls)) {
        addExceptionMessage(cls, schema, seq);
    }
    if (propertyOrder != null) {
        if (propertyOrder.length == seq.getItems().size()) {
            sortItems(seq, propertyOrder);
        } else if (propertyOrder.length > 1 || (propertyOrder.length == 1 && !propertyOrder[0].isEmpty())) {
            LOG.log(Level.WARNING, "propOrder in @XmlType doesn't define all schema elements :" + Arrays.toString(propertyOrder));
        }
    }
    if (xmlAccessorOrder != null && xmlAccessorOrder.value().equals(XmlAccessOrder.ALPHABETICAL) && propertyOrder == null) {
        sort(seq);
    }
    schemas.addCrossImports();
    part.setProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION", Boolean.TRUE);
}
Also used : XmlAccessorOrder(javax.xml.bind.annotation.XmlAccessorOrder) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) Method(java.lang.reflect.Method) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) Type(java.lang.reflect.Type) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) XmlSchema(org.apache.ws.commons.schema.XmlSchema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo) XmlElement(javax.xml.bind.annotation.XmlElement) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 25 with XmlElement

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

the class WrapperBeanFieldAnnotator method annotate.

public void annotate(final JavaAnnotatable field) {
    JavaField jField = null;
    if (field instanceof JavaField) {
        jField = (JavaField) field;
    } else {
        throw new RuntimeException("WrapperBeanFiledAnnotator expect JavaField as input");
    }
    String rawName = jField.getRawName();
    boolean hasEl = false;
    for (Annotation ann : jField.getJaxbAnnotaions()) {
        if (ann instanceof XmlMimeType) {
            JAnnotation mimeAnno = new JAnnotation(XmlMimeType.class);
            mimeAnno.addElement(new JAnnotationElement("value", ((XmlMimeType) ann).value()));
            jField.addAnnotation(mimeAnno);
        } else if (ann instanceof XmlJavaTypeAdapter) {
            JAnnotation jaxbAnn = new JAnnotation(XmlJavaTypeAdapter.class);
            jaxbAnn.addElement(new JAnnotationElement("value", ((XmlJavaTypeAdapter) ann).value()));
            jaxbAnn.addElement(new JAnnotationElement("type", ((XmlJavaTypeAdapter) ann).type()));
            jField.addAnnotation(jaxbAnn);
        } else if (ann instanceof XmlAttachmentRef) {
            JAnnotation jaxbAnn = new JAnnotation(XmlAttachmentRef.class);
            jField.addAnnotation(jaxbAnn);
        } else if (ann instanceof XmlList) {
            JAnnotation jaxbAnn = new JAnnotation(XmlList.class);
            jField.addAnnotation(jaxbAnn);
        } else if (ann instanceof XmlElement) {
            hasEl = true;
            XmlElement el = (XmlElement) ann;
            JAnnotation xmlElementAnnotation = new JAnnotation(XmlElement.class);
            xmlElementAnnotation.addElement(new JAnnotationElement("name", el.name()));
            if (!StringUtils.isEmpty(el.namespace())) {
                xmlElementAnnotation.addElement(new JAnnotationElement("namespace", el.namespace()));
            }
            if (el.nillable()) {
                xmlElementAnnotation.addElement(new JAnnotationElement("nillable", el.nillable(), true));
            }
            if (el.required()) {
                xmlElementAnnotation.addElement(new JAnnotationElement("required", el.required(), true));
            }
            if (!StringUtils.isEmpty(el.defaultValue())) {
                xmlElementAnnotation.addElement(new JAnnotationElement("defaultValue", el.defaultValue()));
            }
            jField.addAnnotation(xmlElementAnnotation);
        }
    }
    if (!hasEl) {
        JAnnotation xmlElementAnnotation = new JAnnotation(XmlElement.class);
        xmlElementAnnotation.addElement(new JAnnotationElement("name", rawName));
        if (!StringUtils.isEmpty(jField.getTargetNamespace())) {
            xmlElementAnnotation.addElement(new JAnnotationElement("namespace", jField.getTargetNamespace()));
        }
        jField.addAnnotation(xmlElementAnnotation);
    }
}
Also used : JavaField(org.apache.cxf.tools.common.model.JavaField) XmlMimeType(javax.xml.bind.annotation.XmlMimeType) JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) XmlAttachmentRef(javax.xml.bind.annotation.XmlAttachmentRef) JAnnotationElement(org.apache.cxf.tools.common.model.JAnnotationElement) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) XmlElement(javax.xml.bind.annotation.XmlElement) JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) Annotation(java.lang.annotation.Annotation) XmlList(javax.xml.bind.annotation.XmlList)

Aggregations

XmlElement (javax.xml.bind.annotation.XmlElement)51 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)26 Element (com.zimbra.common.soap.Element)25 JSONElement (com.zimbra.common.soap.Element.JSONElement)25 XMLElement (com.zimbra.common.soap.Element.XMLElement)25 Test (org.junit.Test)25 FilterTest (com.zimbra.soap.mail.type.FilterTest)24 XmlAttribute (javax.xml.bind.annotation.XmlAttribute)8 Field (java.lang.reflect.Field)7 XmlElements (javax.xml.bind.annotation.XmlElements)7 KeyValuePair (com.zimbra.soap.type.KeyValuePair)5 XmlElementRef (javax.xml.bind.annotation.XmlElementRef)5 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)4 StringAttribIntValue (com.zimbra.soap.jaxb.StringAttribIntValue)3 Annotation (java.lang.annotation.Annotation)3 TypeMirror (javax.lang.model.type.TypeMirror)3 XmlElementWrapper (javax.xml.bind.annotation.XmlElementWrapper)3 QName (javax.xml.namespace.QName)3 GetDistributionListResponse (com.zimbra.soap.account.message.GetDistributionListResponse)2