Example usage for javax.xml.bind.annotation XmlAccessType PROPERTY

List of usage examples for javax.xml.bind.annotation XmlAccessType PROPERTY

Introduction

In this page you can find the example usage for javax.xml.bind.annotation XmlAccessType PROPERTY.

Prototype

XmlAccessType PROPERTY

To view the source code for javax.xml.bind.annotation XmlAccessType PROPERTY.

Click Source Link

Document

Every getter/setter pair in a JAXB-bound class will be automatically bound to XML, unless annotated by XmlTransient .

Usage

From source file:org.castor.jaxb.reflection.PackageAnnotationProcessingServiceTest.java

/**
 * Test method for {@link org.castor.jaxb.annoproc.BaseAnnotationProcessingService#processAnnotations(org.castor.jaxb.reflection.info.Info, 
 * java.lang.annotation.Annotation[])}.//w ww .  j a  va2  s .  c om
 */
@Test
public final void testProcessAnnotations() {
    Class<USAddress> clazz = USAddress.class;
    Package pack = clazz.getPackage();
    Annotation[] annotations = pack.getAnnotations();
    JaxbPackageNature packageInfo = new JaxbPackageNature(new PackageInfo(pack.getName()));
    paps.processAnnotations(packageInfo, annotations);
    Assert.assertEquals(XmlAccessType.PROPERTY, packageInfo.getAccessType());
}

From source file:org.javelin.sws.ext.bind.internal.metadata.PropertyCallback.java

/**
 * @param field/*from   w  w  w  . j  a  v  a 2  s  .  c o m*/
 * @param class1
 */
private <P> void doWithPropertySafe(PropertyMetadata<T, P> metadata)
        throws IllegalArgumentException, IllegalAccessException {

    AnnotatedElement[] accessors = metadata.getAccessors();

    if (this.findJaxbAnnotation(accessors, XmlTransient.class) != null)
        return;

    XmlSchemaType xmlSchemaType = this.findJaxbAnnotation(accessors, XmlSchemaType.class);
    // a pattern for property's class - force creating
    TypedPattern<P> pattern = null;
    if (xmlSchemaType != null) {
        // the schema type determines the pattern - if it is not present in the registry, it will be present after determining it on the basis of Java class
        // of the property
        QName typeName = new QName(xmlSchemaType.namespace(), xmlSchemaType.name());
        pattern = this.patternRegistry.findPatternByType(typeName, metadata.getPropertyClass());
        if (log.isTraceEnabled() && pattern != null)
            log.trace("-- @XmlSchemaType points to {}", pattern.toString());
    }

    if (pattern == null)
        pattern = this.patternRegistry.determineAndCacheXmlPattern(metadata.getPropertyClass());

    // is it value or list?
    XmlValue xmlValue = this.findJaxbAnnotation(accessors, XmlValue.class);
    XmlList xmlList = this.findJaxbAnnotation(accessors, XmlList.class);
    if (xmlValue != null || xmlList != null) {
        // the field's class must be a simpleType, i.e., a type convertible to String, which is either:
        //  - a type registered in org.javelin.sws.ext.bind.internal.BuiltInMappings.initialize()
        //  - a type which has only one property annotated with @XmlValue
        // a type with one @XmlValue property + non-zero @XmlAttribute properties is complex type with simple content, not a simple type
        if (!(pattern.isSimpleType() && pattern instanceof SimpleContentPattern))
            throw new RuntimeException("TODO: should be simpleType");

        if (log.isTraceEnabled())
            log.trace("-- @XmlValue property \"{}\" of type {} mapped to {}", metadata.getPropertyName(),
                    pattern.getJavaType().getName(), pattern.toString());

        metadata.setPattern(pattern);
        if (this.valueMetadata != null)
            throw new RuntimeException("TODO: Only one @XmlValue allowed!");

        this.valueMetadata = metadata;
        return;
    }

    // is it an attribute?
    XmlAttribute xmlAttribute = this.findJaxbAnnotation(accessors, XmlAttribute.class);
    if (xmlAttribute != null) {
        String namespace = XMLConstants.NULL_NS_URI;
        if (this.attributeFormDefault == XmlNsForm.QUALIFIED) {
            // the attribute MUST have namespace
            namespace = "##default".equals(xmlAttribute.namespace()) ? this.typeName.getNamespaceURI()
                    : xmlAttribute.namespace();
        } else {
            // the attribute MAY have namespace
            // TODO: handle org.javelin.sws.ext.bind.annotations.XmlAttribute
            if (!"##default".equals(xmlAttribute.namespace()))
                namespace = xmlAttribute.namespace();
        }
        String name = "##default".equals(xmlAttribute.name()) ? metadata.getPropertyName()
                : xmlAttribute.name();

        if (!(pattern.isSimpleType() && pattern instanceof SimpleContentPattern))
            throw new RuntimeException("TODO: should be simpleType");

        QName attributeQName = new QName(namespace, name);

        if (log.isTraceEnabled())
            log.trace("-- @XmlAttribute property \"{}\" of type {} mapped to {} attribute {}",
                    metadata.getPropertyName(), pattern.getJavaType().getName(), attributeQName,
                    pattern.toString());

        metadata.setPattern(
                AttributePattern.newAttributePattern(attributeQName, (SimpleContentPattern<P>) pattern));
        this.childAttributeMetadata.add(metadata);
        return;
    }

    // is it an element?
    XmlElement xmlElement = this.findJaxbAnnotation(accessors, XmlElement.class);

    // field is also an element when told so using XmlAccessorType
    boolean isElement = false;

    if (accessors[0] instanceof Field) {
        if (this.accessType == XmlAccessType.FIELD)
            isElement = true;
        else if (this.accessType == XmlAccessType.PUBLIC_MEMBER
                && Modifier.isPublic(((Field) accessors[0]).getModifiers()))
            isElement = true;
    } else if (accessors[0] instanceof Method) {
        if (this.accessType == XmlAccessType.PROPERTY)
            isElement = true;
        else if (this.accessType == XmlAccessType.PUBLIC_MEMBER
                && Modifier.isPublic(((Method) accessors[0]).getModifiers())) {
            // TODO: what if getter is private and setter is public?
            isElement = true;
        }
    }

    if (xmlElement != null || isElement) {
        String namespace = XMLConstants.NULL_NS_URI;
        if (this.elementFormDefault == XmlNsForm.QUALIFIED) {
            // the element MUST have namespace
            namespace = xmlElement == null || "##default".equals(xmlElement.namespace())
                    ? this.typeName.getNamespaceURI()
                    : xmlElement.namespace();
        } else {
            // the element MAY have namespace
            if (xmlElement != null && !"##default".equals(xmlElement.namespace()))
                namespace = xmlElement.namespace();
        }
        String name = xmlElement == null || "##default".equals(xmlElement.name()) ? metadata.getPropertyName()
                : xmlElement.name();
        QName elementQName = new QName(namespace, name);

        if (log.isTraceEnabled())
            log.trace("-- @XmlElement property \"{}\" of type {} mapped to {} element with {}",
                    metadata.getPropertyName(), pattern.getJavaType().getName(), elementQName,
                    pattern.toString());

        ElementPattern<?> elementPattern = ElementPattern.newElementPattern(elementQName, pattern);
        XmlElementWrapper xmlElementWrapper = this.findJaxbAnnotation(accessors, XmlElementWrapper.class);
        if (xmlElementWrapper != null) {
            if (!"##default".equals(xmlElementWrapper.namespace()))
                namespace = xmlElementWrapper.namespace();
            name = !"##default".equals(xmlElementWrapper.name()) ? xmlElementWrapper.name()
                    : metadata.getPropertyName();

            // XmlElementWrapper creates (in XSD Category) a new complex, anonymous, nested (inside element declaration) type
            // DESIGNFLAW: XmlElementWrapper works, but not as clean as it should
            PropertyMetadata<T, ?> md = PropertyMetadata.newPropertyMetadata(this.clazz,
                    metadata.getCollectionClass(), "", PropertyKind.PASSTHROUGH);
            md.setPattern(elementPattern);
            ComplexTypePattern<T> newAnonymousType = ComplexTypePattern.newContentModelPattern(null, this.clazz,
                    md);
            // TODO: Handle @XmlElementWrapper for collection properties
            // TODO: JAXB2 doesn't allow this, but maybe it's a good idea to be able to wrap non-collection properties also?
            // TODO: maybe it's a good idea to create @XmlElementWrappers class (aggregating multime @XmlElementWrapper annotations?)
            elementPattern = ElementPattern.newElementPattern(new QName(namespace, name), newAnonymousType);
            metadata.setWrappedCollection(true);
        }

        metadata.setPattern(elementPattern);
        this.childElementMetadata.add(metadata);
        return;
    }
}

From source file:com.evolveum.midpoint.schema.xjc.schema.SchemaProcessor.java

private void updateClassAnnotation(ClassOutline classOutline) {
    try {/*from  ww  w. j av  a2 s  .  c  o  m*/
        JDefinedClass definedClass = classOutline.implClass;
        List<JAnnotationUse> existingAnnotations = (List<JAnnotationUse>) getAnnotations(definedClass);
        for (JAnnotationUse annotation : existingAnnotations) {
            if (isAnnotationTypeOf(annotation, XmlAccessorType.class)) {
                Field field = getField(JAnnotationUse.class, "memberValues");
                field.setAccessible(true);
                Map<String, Object> map = (Map<String, Object>) field.get(annotation);
                field.setAccessible(false);
                map.clear();
                annotation.param("value", XmlAccessType.PROPERTY);
            }
            if (isAnnotationTypeOf(annotation, XmlType.class)) {
                Field field = getField(JAnnotationUse.class, "memberValues");
                field.setAccessible(true);
                Map<String, Object> map = (Map<String, Object>) field.get(annotation);
                Object propOrder = map.get("propOrder");
                if (propOrder != null) {
                    JAnnotationArrayMember paramArray = (JAnnotationArrayMember) propOrder;
                    Field valField = getField(JAnnotationArrayMember.class, "values");
                    valField.setAccessible(true);
                    List<JAnnotationValue> values = (List<JAnnotationValue>) valField.get(paramArray);
                    for (int i = 0; i < values.size(); i++) {
                        JAnnotationValue jAnnValue = values.get(i);
                        String value = extractString(jAnnValue);
                        if (value.startsWith("_")) {
                            paramArray.param(value.substring(1));
                            values.set(i, values.get(values.size() - 1));
                            values.remove(values.size() - 1);
                        }
                        String valAfter = extractString(values.get(i));
                        //                     System.out.println("PPPPPPPPPPPPPPPPPPP: "+value+" -> "+valAfter);
                    }
                    valField.setAccessible(false);
                }
                field.setAccessible(false);
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}