Search in sources :

Example 1 with XmlLocation

use of org.eclipse.persistence.oxm.annotations.XmlLocation in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method getPropertyPropertiesForClass.

public ArrayList<Property> getPropertyPropertiesForClass(JavaClass cls, TypeInfo info, boolean onlyPublic, boolean onlyExplicit) {
    ArrayList<Property> properties = new ArrayList<>();
    if (cls == null) {
        return properties;
    }
    // First collect all the getters and setters
    ArrayList<JavaMethod> propertyMethods = new ArrayList<>();
    for (JavaMethod next : new ArrayList<>(cls.getDeclaredMethods())) {
        if (!next.isSynthetic()) {
            if (((next.getName().startsWith(GET_STR) && next.getName().length() > 3) || (next.getName().startsWith(IS_STR) && next.getName().length() > 2)) && next.getParameterTypes().length == 0 && next.getReturnType() != helper.getJavaClass(java.lang.Void.class)) {
                int modifiers = next.getModifiers();
                if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers) && ((onlyPublic && Modifier.isPublic(next.getModifiers())) || !onlyPublic || hasJAXBAnnotations(next))) {
                    propertyMethods.add(next);
                }
            } else if (next.getName().startsWith(SET_STR) && next.getName().length() > 3 && next.getParameterTypes().length == 1) {
                int modifiers = next.getModifiers();
                if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers) && ((onlyPublic && Modifier.isPublic(next.getModifiers())) || !onlyPublic || hasJAXBAnnotations(next))) {
                    propertyMethods.add(next);
                }
            }
        }
    }
    // Next iterate over the getters and find their setter methods, add
    // whichever one is
    // annotated to the properties list. If neither is, use the getter
    // keep track of property names to avoid processing the same property
    // twice (for getter and setter)
    List<String> propertyNames = new ArrayList<>();
    for (JavaMethod propertyMethod1 : propertyMethods) {
        boolean isPropertyTransient = false;
        JavaMethod nextMethod = propertyMethod1;
        String propertyName = EMPTY_STRING;
        JavaMethod getMethod;
        JavaMethod setMethod;
        JavaMethod propertyMethod = null;
        if (!nextMethod.getName().startsWith(SET_STR)) {
            if (nextMethod.getName().startsWith(GET_STR)) {
                propertyName = nextMethod.getName().substring(3);
            } else if (nextMethod.getName().startsWith(IS_STR)) {
                propertyName = nextMethod.getName().substring(2);
            }
            getMethod = nextMethod;
            String setMethodName = SET_STR + propertyName;
            // use the JavaBean API to correctly decapitalize the first
            // character, if necessary
            propertyName = Introspector.decapitalize(propertyName);
            JavaClass[] paramTypes = { getMethod.getReturnType() };
            setMethod = cls.getDeclaredMethod(setMethodName, paramTypes);
            if (setMethod == null) {
                // if there's no locally declared set method, check for an inherited
                // set method
                setMethod = cls.getMethod(setMethodName, paramTypes);
            }
            if (setMethod == null && !(hasJAXBAnnotations(getMethod))) {
                // if there's no corresponding setter, and not explicitly
                // annotated, don't process
                isPropertyTransient = true;
            }
            if (setMethod != null && hasJAXBAnnotations(setMethod)) {
                // use the set method if it exists and is annotated
                boolean isTransient = helper.isAnnotationPresent(setMethod, XmlTransient.class);
                boolean isLocation = helper.isAnnotationPresent(setMethod, XmlLocation.class) || helper.isAnnotationPresent(setMethod, CompilerHelper.XML_LOCATION_ANNOTATION_CLASS) || helper.isAnnotationPresent(setMethod, CompilerHelper.INTERNAL_XML_LOCATION_ANNOTATION_CLASS);
                propertyMethod = setMethod;
                if (isTransient) {
                    isPropertyTransient = true;
                    // XmlLocation can also be transient
                    if (isLocation) {
                        info.setLocationAware(true);
                    }
                }
            } else if ((onlyExplicit && hasJAXBAnnotations(getMethod)) || !onlyExplicit) {
                boolean isTransient = helper.isAnnotationPresent(getMethod, XmlTransient.class);
                boolean isLocation = helper.isAnnotationPresent(getMethod, XmlLocation.class) || helper.isAnnotationPresent(setMethod, CompilerHelper.XML_LOCATION_ANNOTATION_CLASS) || helper.isAnnotationPresent(setMethod, CompilerHelper.INTERNAL_XML_LOCATION_ANNOTATION_CLASS);
                propertyMethod = getMethod;
                if (isTransient) {
                    isPropertyTransient = true;
                    // XmlLocation can also be transient
                    if (isLocation) {
                        info.setLocationAware(true);
                    }
                }
            } else if (onlyExplicit) {
                continue;
            }
        } else {
            propertyName = nextMethod.getName().substring(3);
            setMethod = nextMethod;
            String getMethodName = GET_STR + propertyName;
            getMethod = cls.getDeclaredMethod(getMethodName, new JavaClass[] {});
            if (getMethod == null) {
                // try is instead of get
                getMethodName = IS_STR + propertyName;
                getMethod = cls.getDeclaredMethod(getMethodName, new JavaClass[] {});
            }
            // may look for get method on parent class
            if (getMethod == null) {
                // look for inherited getMethod
                getMethod = cls.getMethod(GET_STR + propertyName, new JavaClass[] {});
                if (getMethod == null) {
                    getMethod = cls.getMethod(IS_STR + propertyName, new JavaClass[] {});
                }
            }
            if (getMethod == null && !(hasJAXBAnnotations(setMethod))) {
                isPropertyTransient = true;
            }
            if (getMethod != null && hasJAXBAnnotations(getMethod)) {
                // use the get method if it exists and is annotated
                boolean isTransient = helper.isAnnotationPresent(getMethod, XmlTransient.class);
                boolean isLocation = helper.isAnnotationPresent(getMethod, XmlLocation.class) || helper.isAnnotationPresent(getMethod, CompilerHelper.XML_LOCATION_ANNOTATION_CLASS) || helper.isAnnotationPresent(getMethod, CompilerHelper.INTERNAL_XML_LOCATION_ANNOTATION_CLASS);
                propertyMethod = getMethod;
                if (isTransient) {
                    isPropertyTransient = true;
                    // XmlLocation can also be transient
                    if (isLocation) {
                        info.setLocationAware(true);
                    }
                }
            } else if ((onlyExplicit && hasJAXBAnnotations(setMethod)) || !onlyExplicit) {
                boolean isTransient = helper.isAnnotationPresent(setMethod, XmlTransient.class);
                boolean isLocation = helper.isAnnotationPresent(setMethod, XmlLocation.class) || helper.isAnnotationPresent(getMethod, CompilerHelper.XML_LOCATION_ANNOTATION_CLASS) || helper.isAnnotationPresent(getMethod, CompilerHelper.INTERNAL_XML_LOCATION_ANNOTATION_CLASS);
                propertyMethod = setMethod;
                if (isTransient) {
                    isPropertyTransient = true;
                    // XmlLocation can also be transient
                    if (isLocation) {
                        info.setLocationAware(true);
                    }
                }
            } else if (onlyExplicit) {
                continue;
            }
            // use the JavaBean API to correctly decapitalize the first
            // character, if necessary
            propertyName = Introspector.decapitalize(propertyName);
        }
        JavaClass ptype = null;
        if (getMethod != null) {
            ptype = getMethod.getReturnType();
        } else {
            ptype = setMethod.getParameterTypes()[0];
        }
        if (!propertyNames.contains(propertyName)) {
            try {
                Property property = buildNewProperty(info, cls, propertyMethod, propertyName, ptype);
                propertyNames.add(propertyName);
                property.setTransient(isPropertyTransient);
                if (getMethod != null) {
                    property.setOriginalGetMethodName(getMethod.getName());
                    if (property.getGetMethodName() == null) {
                        property.setGetMethodName(getMethod.getName());
                    }
                }
                if (setMethod != null) {
                    property.setOriginalSetMethodName(setMethod.getName());
                    if (property.getSetMethodName() == null) {
                        property.setSetMethodName(setMethod.getName());
                    }
                }
                property.setMethodProperty(true);
                // boolean isTransient = helper.isAnnotationPresent(property.getElement(), XmlTransient.class);
                // boolean isLocation = helper.isAnnotationPresent(property.getElement(), XmlLocation.class) ||
                // helper.isAnnotationPresent(setMethod, CompilerHelper.XML_LOCATION_ANNOTATION_CLASS) ||
                // helper.isAnnotationPresent(setMethod, CompilerHelper.INTERNAL_XML_LOCATION_ANNOTATION_CLASS);
                // if (!isTransient || (isTransient && isLocation)) {
                properties.add(property);
            // }
            } catch (JAXBException ex) {
                if (ex.getErrorCode() != JAXBException.INVALID_INTERFACE || !isPropertyTransient) {
                    throw ex;
                }
            }
        }
    }
    properties = removeSuperclassProperties(cls, properties);
    // default to alphabetical ordering
    // RI compliancy
    Collections.sort(properties, new PropertyComparitor());
    return properties;
}
Also used : XmlLocation(org.eclipse.persistence.oxm.annotations.XmlLocation) JAXBException(org.eclipse.persistence.exceptions.JAXBException) ArrayList(java.util.ArrayList) XmlTransient(jakarta.xml.bind.annotation.XmlTransient) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod) XmlProperty(org.eclipse.persistence.oxm.annotations.XmlProperty)

Example 2 with XmlLocation

use of org.eclipse.persistence.oxm.annotations.XmlLocation in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method processPropertyAnnotations.

private void processPropertyAnnotations(TypeInfo info, JavaClass cls, JavaHasAnnotations propertyElement, Property property) {
    // Check for mixed context
    if (helper.isAnnotationPresent(propertyElement, XmlMixed.class)) {
        info.setMixed(true);
        property.setMixedContent(true);
        findAndProcessObjectFactory(cls);
    }
    if (helper.isAnnotationPresent(propertyElement, XmlInverseReference.class)) {
        XmlInverseReference inverseReference = (XmlInverseReference) helper.getAnnotation(propertyElement, XmlInverseReference.class);
        property.setInverseReferencePropertyName(inverseReference.mappedBy());
        TypeInfo targetInfo = this.getTypeInfos().get(property.getActualType().getName());
        if (targetInfo != null && targetInfo.getXmlAccessType() == XmlAccessType.PROPERTY) {
            String propName = property.getPropertyName();
            propName = Character.toUpperCase(propName.charAt(0)) + propName.substring(1);
            property.setInverseReferencePropertyGetMethodName(GET_STR + propName);
            property.setInverseReferencePropertySetMethodName(SET_STR + propName);
        }
        property.setInverseReference(true, helper.isAnnotationPresent(propertyElement, XmlElement.class));
    }
    processXmlJavaTypeAdapter(property, info, cls);
    if (helper.isAnnotationPresent(propertyElement, XmlAttachmentRef.class) && areEquals(property.getActualType(), JAVAX_ACTIVATION_DATAHANDLER)) {
        property.setIsSwaAttachmentRef(true);
        property.setSchemaType(Constants.SWA_REF_QNAME);
    }
    processXmlElement(property, info);
    // JavaClass ptype = property.getActualType();
    if (!(property.isSwaAttachmentRef()) && isMtomAttachment(property)) {
        property.setIsMtomAttachment(true);
        property.setSchemaType(Constants.BASE_64_BINARY_QNAME);
    }
    if (helper.isAnnotationPresent(propertyElement, XmlMimeType.class)) {
        property.setMimeType(((XmlMimeType) helper.getAnnotation(propertyElement, XmlMimeType.class)).value());
    }
    // non-binary data type won't have any affect
    if (helper.isAnnotationPresent(propertyElement, XmlInlineBinaryData.class) || info.isBinaryDataToBeInlined()) {
        property.setisInlineBinaryData(true);
    }
    // later use:
    if (helper.isAnnotationPresent(propertyElement, XmlSchemaType.class)) {
        XmlSchemaType schemaType = (XmlSchemaType) helper.getAnnotation(propertyElement, XmlSchemaType.class);
        QName schemaTypeQname = new QName(schemaType.namespace(), schemaType.name());
        property.setSchemaType(schemaTypeQname);
    }
    if (helper.isAnnotationPresent(propertyElement, XmlAttribute.class)) {
        property.setIsAttribute(true);
        property.setIsRequired(((XmlAttribute) helper.getAnnotation(propertyElement, XmlAttribute.class)).required());
    }
    if (helper.isAnnotationPresent(propertyElement, XmlAnyAttribute.class)) {
        if (info.isSetAnyAttributePropertyName() && !info.getAnyAttributePropertyName().equals(property.getPropertyName())) {
            throw org.eclipse.persistence.exceptions.JAXBException.multipleAnyAttributeMapping(cls.getName());
        }
        if (!helper.isMapType(property.getType())) {
            throw org.eclipse.persistence.exceptions.JAXBException.anyAttributeOnNonMap(property.getPropertyName());
        }
        property.setIsAnyAttribute(true);
        info.setAnyAttributePropertyName(property.getPropertyName());
    }
    // Make sure XmlElementWrapper annotation is on a collection or array
    if (helper.isAnnotationPresent(propertyElement, XmlElementWrapper.class)) {
        XmlElementWrapper wrapper = (XmlElementWrapper) helper.getAnnotation(propertyElement, XmlElementWrapper.class);
        org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper xmlEltWrapper = new org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper();
        String wrapperName = wrapper.name();
        if (wrapperName.equals(XMLProcessor.DEFAULT)) {
            wrapperName = info.getXmlNameTransformer().transformElementName(property.getPropertyName());
        }
        xmlEltWrapper.setName(wrapperName);
        xmlEltWrapper.setNamespace(wrapper.namespace());
        xmlEltWrapper.setNillable(wrapper.nillable());
        xmlEltWrapper.setRequired(wrapper.required());
        property.setXmlElementWrapper(xmlEltWrapper);
    }
    if (helper.isAnnotationPresent(propertyElement, XmlList.class)) {
        // Make sure XmlList annotation is on a collection or array
        if (!helper.isCollectionType(property.getType()) && !property.getType().isArray()) {
            throw JAXBException.invalidList(property.getPropertyName());
        }
        property.setIsXmlList(true);
    }
    if (helper.isAnnotationPresent(propertyElement, XmlValue.class)) {
        property.setIsXmlValue(true);
        info.setXmlValueProperty(property);
    }
    if (helper.isAnnotationPresent(propertyElement, XmlReadOnly.class)) {
        property.setReadOnly(true);
    }
    if (helper.isAnnotationPresent(propertyElement, XmlWriteOnly.class)) {
        property.setWriteOnly(true);
    }
    if (helper.isAnnotationPresent(propertyElement, XmlCDATA.class)) {
        property.setCdata(true);
    }
    if (helper.isAnnotationPresent(propertyElement, XmlAccessMethods.class)) {
        XmlAccessMethods accessMethods = (XmlAccessMethods) helper.getAnnotation(propertyElement, XmlAccessMethods.class);
        if (!(accessMethods.getMethodName().equals(EMPTY_STRING))) {
            property.setGetMethodName(accessMethods.getMethodName());
        }
        if (!(accessMethods.setMethodName().equals(EMPTY_STRING))) {
            property.setSetMethodName(accessMethods.setMethodName());
        }
        if (!(property.isMethodProperty())) {
            property.setMethodProperty(true);
        }
    }
    // handle user properties
    if (helper.isAnnotationPresent(propertyElement, XmlProperties.class)) {
        XmlProperties xmlProperties = (XmlProperties) helper.getAnnotation(propertyElement, XmlProperties.class);
        Map<Object, Object> propertiesMap = createUserPropertiesMap(xmlProperties.value());
        property.setUserProperties(propertiesMap);
    } else if (helper.isAnnotationPresent(propertyElement, XmlProperty.class)) {
        XmlProperty xmlProperty = (XmlProperty) helper.getAnnotation(propertyElement, XmlProperty.class);
        Map<Object, Object> propertiesMap = createUserPropertiesMap(new XmlProperty[] { xmlProperty });
        property.setUserProperties(propertiesMap);
    }
    // handle XmlKey
    if (helper.isAnnotationPresent(propertyElement, XmlKey.class)) {
        info.addXmlKeyProperty(property);
    }
    // handle XmlJoinNode(s)
    processXmlJoinNodes(property);
    processXmlNullPolicy(property, cls, info);
    // Handle XmlLocation
    JavaHasAnnotations elem = propertyElement;
    if (helper.isAnnotationPresent(elem, XmlLocation.class) || helper.isAnnotationPresent(elem, CompilerHelper.XML_LOCATION_ANNOTATION_CLASS) || helper.isAnnotationPresent(elem, CompilerHelper.OLD_XML_LOCATION_ANNOTATION_CLASS) || helper.isAnnotationPresent(elem, CompilerHelper.INTERNAL_XML_LOCATION_ANNOTATION_CLASS)) {
        if (!helper.getJavaClass(Constants.LOCATOR_CLASS).isAssignableFrom(property.getType())) {
            throw JAXBException.invalidXmlLocation(property.getPropertyName(), property.getType().getName());
        }
        property.setXmlLocation(true);
    }
}
Also used : XmlAccessMethods(org.eclipse.persistence.oxm.annotations.XmlAccessMethods) XmlProperties(org.eclipse.persistence.oxm.annotations.XmlProperties) XmlProperty(org.eclipse.persistence.oxm.annotations.XmlProperty) QName(javax.xml.namespace.QName) XmlLocation(org.eclipse.persistence.oxm.annotations.XmlLocation) XmlSchemaType(jakarta.xml.bind.annotation.XmlSchemaType) XmlInverseReference(org.eclipse.persistence.oxm.annotations.XmlInverseReference) XmlAttachmentRef(jakarta.xml.bind.annotation.XmlAttachmentRef) XmlElement(jakarta.xml.bind.annotation.XmlElement) XmlInlineBinaryData(jakarta.xml.bind.annotation.XmlInlineBinaryData) Map(java.util.Map) HashMap(java.util.HashMap) XmlElementWrapper(jakarta.xml.bind.annotation.XmlElementWrapper) JavaHasAnnotations(org.eclipse.persistence.jaxb.javamodel.JavaHasAnnotations)

Aggregations

XmlLocation (org.eclipse.persistence.oxm.annotations.XmlLocation)2 XmlProperty (org.eclipse.persistence.oxm.annotations.XmlProperty)2 XmlAttachmentRef (jakarta.xml.bind.annotation.XmlAttachmentRef)1 XmlElement (jakarta.xml.bind.annotation.XmlElement)1 XmlElementWrapper (jakarta.xml.bind.annotation.XmlElementWrapper)1 XmlInlineBinaryData (jakarta.xml.bind.annotation.XmlInlineBinaryData)1 XmlSchemaType (jakarta.xml.bind.annotation.XmlSchemaType)1 XmlTransient (jakarta.xml.bind.annotation.XmlTransient)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 QName (javax.xml.namespace.QName)1 JAXBException (org.eclipse.persistence.exceptions.JAXBException)1 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)1 JavaHasAnnotations (org.eclipse.persistence.jaxb.javamodel.JavaHasAnnotations)1 JavaMethod (org.eclipse.persistence.jaxb.javamodel.JavaMethod)1 XmlAccessMethods (org.eclipse.persistence.oxm.annotations.XmlAccessMethods)1 XmlInverseReference (org.eclipse.persistence.oxm.annotations.XmlInverseReference)1 XmlProperties (org.eclipse.persistence.oxm.annotations.XmlProperties)1