Search in sources :

Example 1 with JavaField

use of org.eclipse.persistence.jaxb.javamodel.JavaField in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method getFieldPropertiesForClass.

public ArrayList<Property> getFieldPropertiesForClass(JavaClass cls, TypeInfo info, boolean onlyPublic, boolean onlyExplicit) {
    ArrayList<Property> properties = new ArrayList<>();
    if (cls == null) {
        return properties;
    }
    for (JavaField javaField : cls.getDeclaredFields()) {
        Property property = null;
        int modifiers = javaField.getModifiers();
        if (!Modifier.isTransient(modifiers) && ((Modifier.isPublic(javaField.getModifiers()) && onlyPublic) || !onlyPublic || hasJAXBAnnotations(javaField))) {
            if (!Modifier.isStatic(modifiers)) {
                if ((onlyExplicit && hasJAXBAnnotations(javaField)) || !onlyExplicit) {
                    try {
                        property = buildNewProperty(info, cls, javaField, javaField.getName(), javaField.getResolvedType());
                        properties.add(property);
                    } catch (JAXBException ex) {
                        if (ex.getErrorCode() != JAXBException.INVALID_INTERFACE || !helper.isAnnotationPresent(javaField, XmlTransient.class)) {
                            throw ex;
                        }
                    }
                }
            } else {
                try {
                    property = buildNewProperty(info, cls, javaField, javaField.getName(), javaField.getResolvedType());
                    if (helper.isAnnotationPresent(javaField, XmlAttribute.class)) {
                        Object value = ((JavaFieldImpl) javaField).get(null);
                        if (value != null) {
                            String stringValue = XMLConversionManager.getDefaultXMLManager().convertObject(value, String.class, property.getSchemaType());
                            property.setFixedValue(stringValue);
                        }
                    }
                    property.setWriteOnly(true);
                    if (!hasJAXBAnnotations(javaField)) {
                        property.setTransient(true);
                    }
                    properties.add(property);
                } catch (ClassCastException e) {
                // do Nothing
                } catch (IllegalAccessException e) {
                // do Nothing
                } catch (JAXBException ex) {
                    if (ex.getErrorCode() != JAXBException.INVALID_INTERFACE || !helper.isAnnotationPresent(javaField, XmlTransient.class)) {
                        throw ex;
                    }
                }
            }
        }
        if (helper.isAnnotationPresent(javaField, XmlTransient.class)) {
            if (property != null) {
                property.setTransient(true);
            }
        }
    }
    return properties;
}
Also used : JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) JAXBException(org.eclipse.persistence.exceptions.JAXBException) ArrayList(java.util.ArrayList) JavaFieldImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaFieldImpl) XmlProperty(org.eclipse.persistence.oxm.annotations.XmlProperty) XmlTransient(jakarta.xml.bind.annotation.XmlTransient)

Example 2 with JavaField

use of org.eclipse.persistence.jaxb.javamodel.JavaField in project eclipselink by eclipse-ee4j.

the class XMLProcessor method createProperty.

private Property createProperty(TypeInfo info, JavaAttribute javaAttribute) {
    XmlAccessType xmlAccessorType = javaAttribute.getXmlAccessorType();
    // Property prop = new Property();
    // prop.setPropertyName(javaAttribute.getJavaAttribute());
    String propName = javaAttribute.getJavaAttribute();
    JavaHasAnnotations element = null;
    JavaClass pType = null;
    JavaClass jClass = this.jModelInput.getJavaModel().getClass(info.getJavaClassName());
    if (xmlAccessorType == XmlAccessType.PROPERTY) {
        // set up accessor method names
        String name = Character.toUpperCase(propName.charAt(0)) + propName.substring(1);
        String getMethodName = GET_STR + name;
        String setMethodName = SET_STR + name;
        JavaMethod jMethod = jClass.getDeclaredMethod(getMethodName, new JavaClass[] {});
        if (jMethod == null) {
            getMethodName = IS_STR + name;
            jMethod = jClass.getDeclaredMethod(getMethodName, new JavaClass[] {});
        }
        if (jMethod != null) {
            pType = jMethod.getReturnType();
            element = jMethod;
        } else {
            // look for a set method if type is set on javaAttribute
            for (Object next : jClass.getDeclaredMethods()) {
                JavaMethod nextMethod = (JavaMethod) next;
                if (nextMethod.getName().equals(setMethodName) && nextMethod.getParameterTypes().length == 1) {
                    pType = nextMethod.getParameterTypes()[0];
                    element = nextMethod;
                }
            }
            if (element == null) {
                return null;
            }
        }
    } else {
        JavaField jField = jClass.getDeclaredField(propName);
        if (jField == null) {
            return null;
        }
        pType = jField.getResolvedType();
        element = jField;
    }
    return this.aProcessor.buildNewProperty(info, jClass, element, propName, pType);
}
Also used : JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod) XmlAccessType(org.eclipse.persistence.jaxb.xmlmodel.XmlAccessType) JavaHasAnnotations(org.eclipse.persistence.jaxb.javamodel.JavaHasAnnotations)

Example 3 with JavaField

use of org.eclipse.persistence.jaxb.javamodel.JavaField in project eclipselink by eclipse-ee4j.

the class JavaClassImpl method getFields.

public Collection getFields() {
    ArrayList<JavaField> fieldCollection = new ArrayList<>();
    Field[] fields = PrivilegedAccessHelper.getFields(jClass);
    for (Field field : fields) {
        fieldCollection.add(getJavaField(field));
    }
    return fieldCollection;
}
Also used : JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) Field(java.lang.reflect.Field) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) ArrayList(java.util.ArrayList)

Example 4 with JavaField

use of org.eclipse.persistence.jaxb.javamodel.JavaField in project eclipselink by eclipse-ee4j.

the class MappingsGenerator method generateXMLObjectReferenceMapping.

/**
 * Create an XMLObjectReferenceMapping and add it to the descriptor.
 */
public ObjectReferenceMapping generateXMLObjectReferenceMapping(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo, JavaClass referenceClass) {
    ObjectReferenceMapping<AbstractSession, AttributeAccessor, ContainerPolicy, ClassDescriptor, DatabaseField, UnmarshalRecord, XMLField, XMLRecord> mapping = new XMLObjectReferenceMapping();
    initializeXMLMapping((XMLMapping) mapping, property);
    mapping.setReferenceClassName(referenceClass.getQualifiedName());
    // here we need to setup source/target key field associations
    if (property.isSetXmlJoinNodes()) {
        for (XmlJoinNode xmlJoinNode : property.getXmlJoinNodes().getXmlJoinNode()) {
            validateJoinNode(descriptor.getJavaClassName(), property, xmlJoinNode.getReferencedXmlPath(), referenceClass);
            mapping.addSourceToTargetKeyFieldAssociation(xmlJoinNode.getXmlPath(), xmlJoinNode.getReferencedXmlPath());
        }
    } else {
        String tgtXPath = null;
        TypeInfo referenceType = typeInfo.get(referenceClass.getQualifiedName());
        if (null != referenceType && referenceType.isIDSet()) {
            Property prop = referenceType.getIDProperty();
            tgtXPath = getXPathForField(prop, namespaceInfo, !prop.isAttribute(), false).getXPath();
        }
        // if the XPath is set (via xml-path) use it, otherwise figure it out
        Field srcXPath;
        if (property.getXmlPath() != null) {
            srcXPath = new XMLField(property.getXmlPath());
        } else {
            srcXPath = getXPathForField(property, namespaceInfo, true, false);
        }
        mapping.addSourceToTargetKeyFieldAssociation(srcXPath.getXPath(), tgtXPath);
    }
    if (property.getInverseReferencePropertyName() != null) {
        mapping.getInverseReferenceMapping().setAttributeName(property.getInverseReferencePropertyName());
        JavaClass backPointerPropertyType = null;
        if (property.getInverseReferencePropertyGetMethodName() != null && property.getInverseReferencePropertySetMethodName() != null && !property.getInverseReferencePropertyGetMethodName().equals("") && !property.getInverseReferencePropertySetMethodName().equals("")) {
            mapping.getInverseReferenceMapping().setGetMethodName(property.getInverseReferencePropertySetMethodName());
            mapping.getInverseReferenceMapping().setSetMethodName(property.getInverseReferencePropertySetMethodName());
            JavaMethod getMethod = referenceClass.getDeclaredMethod(mapping.getInverseReferenceMapping().getGetMethodName(), new JavaClass[] {});
            if (getMethod != null) {
                backPointerPropertyType = getMethod.getReturnType();
            }
        } else {
            JavaField backpointerField = referenceClass.getDeclaredField(property.getInverseReferencePropertyName());
            if (backpointerField != null) {
                backPointerPropertyType = backpointerField.getResolvedType();
            }
        }
        if (helper.isCollectionType(backPointerPropertyType)) {
            mapping.getInverseReferenceMapping().setContainerPolicy(ContainerPolicy.buildDefaultPolicy());
        }
    }
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) XmlJoinNode(org.eclipse.persistence.jaxb.xmlmodel.XmlJoinNodes.XmlJoinNode) XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Field(org.eclipse.persistence.internal.oxm.mappings.Field) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) UnmarshalRecord(org.eclipse.persistence.internal.oxm.record.UnmarshalRecord) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod) VirtualAttributeAccessor(org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor) CustomAccessorAttributeAccessor(org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor) JAXBSetMethodAttributeAccessor(org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) MapValueAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor) InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 5 with JavaField

use of org.eclipse.persistence.jaxb.javamodel.JavaField in project eclipselink by eclipse-ee4j.

the class MappingsGenerator method generateXMLCollectionReferenceMapping.

/**
 * Create an XMLCollectionReferenceMapping and add it to the descriptor.
 */
public CollectionReferenceMapping generateXMLCollectionReferenceMapping(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo, JavaClass referenceClass) {
    CollectionReferenceMapping<AbstractSession, AttributeAccessor, ContainerPolicy, ClassDescriptor, DatabaseField, UnmarshalRecord, XMLField, XMLRecord> mapping = new XMLCollectionReferenceMapping();
    initializeXMLMapping((XMLMapping) mapping, property);
    initializeXMLContainerMapping(mapping, property.getType().isArray());
    mapping.setUsesSingleNode(property.isXmlList() || (property.isAttribute() && (property.getXmlPath() == null || !property.getXmlPath().contains("/"))));
    String referenceClassName = referenceClass.getQualifiedName();
    JavaClass collectionType = property.getType();
    if (collectionType.isArray()) {
        JAXBArrayAttributeAccessor accessor = new JAXBArrayAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy(), helper.getClassLoader());
        JavaClass componentType = collectionType.getComponentType();
        if (componentType.isArray()) {
            Class<?> adaptedClass = classToGeneratedClasses.get(componentType.getName());
            referenceClassName = adaptedClass.getName();
            accessor.setAdaptedClassName(referenceClassName);
            JavaClass baseComponentType = getBaseComponentType(componentType);
            if (baseComponentType.isPrimitive()) {
                Class<Object> primitiveClass = XMLConversionManager.getDefaultManager().convertClassNameToClass(baseComponentType.getRawName());
                accessor.setComponentClass(primitiveClass);
            } else {
                accessor.setComponentClassName(baseComponentType.getQualifiedName());
            }
        } else {
            accessor.setComponentClassName(componentType.getQualifiedName());
        }
        mapping.setAttributeAccessor(accessor);
    }
    collectionType = containerClassImpl(collectionType);
    mapping.useCollectionClassName(collectionType.getRawName());
    mapping.setReferenceClassName(referenceClassName);
    // here we need to setup source/target key field associations
    if (property.isSetXmlJoinNodes()) {
        for (XmlJoinNode xmlJoinNode : property.getXmlJoinNodes().getXmlJoinNode()) {
            validateJoinNode(descriptor.getJavaClassName(), property, xmlJoinNode.getReferencedXmlPath(), referenceClass);
            mapping.addSourceToTargetKeyFieldAssociation(xmlJoinNode.getXmlPath(), xmlJoinNode.getReferencedXmlPath());
        }
    } else {
        // here we need to setup source/target key field associations
        TypeInfo referenceType = typeInfo.get(referenceClass.getQualifiedName());
        String tgtXPath = null;
        if (null != referenceType && referenceType.isIDSet()) {
            Property prop = referenceType.getIDProperty();
            tgtXPath = getXPathForField(prop, namespaceInfo, !prop.isAttribute(), false).getXPath();
        }
        // if the XPath is set (via xml-path) use it
        Field srcXPath;
        if (property.getXmlPath() != null) {
            srcXPath = new XMLField(property.getXmlPath());
        } else {
            srcXPath = getXPathForField(property, namespaceInfo, true, false);
        }
        mapping.addSourceToTargetKeyFieldAssociation(srcXPath.getXPath(), tgtXPath);
    }
    if (property.getInverseReferencePropertyName() != null) {
        mapping.getInverseReferenceMapping().setAttributeName(property.getInverseReferencePropertyName());
        JavaClass backPointerPropertyType = null;
        if (property.getInverseReferencePropertyGetMethodName() != null && property.getInverseReferencePropertySetMethodName() != null && !property.getInverseReferencePropertyGetMethodName().equals("") && !property.getInverseReferencePropertySetMethodName().equals("")) {
            mapping.getInverseReferenceMapping().setGetMethodName(property.getInverseReferencePropertySetMethodName());
            mapping.getInverseReferenceMapping().setSetMethodName(property.getInverseReferencePropertySetMethodName());
            JavaMethod getMethod = referenceClass.getDeclaredMethod(mapping.getInverseReferenceMapping().getGetMethodName(), new JavaClass[] {});
            if (getMethod != null) {
                backPointerPropertyType = getMethod.getReturnType();
            }
        } else {
            JavaField backpointerField = referenceClass.getDeclaredField(property.getInverseReferencePropertyName());
            if (backpointerField != null) {
                backPointerPropertyType = backpointerField.getResolvedType();
            }
        }
        if (helper.isCollectionType(backPointerPropertyType)) {
            mapping.getInverseReferenceMapping().setContainerPolicy(ContainerPolicy.buildDefaultPolicy());
        }
    }
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) XMLCollectionReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) XmlJoinNode(org.eclipse.persistence.jaxb.xmlmodel.XmlJoinNodes.XmlJoinNode) XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Field(org.eclipse.persistence.internal.oxm.mappings.Field) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) UnmarshalRecord(org.eclipse.persistence.internal.oxm.record.UnmarshalRecord) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod) VirtualAttributeAccessor(org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor) CustomAccessorAttributeAccessor(org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor) JAXBSetMethodAttributeAccessor(org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) MapValueAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor) InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

JavaField (org.eclipse.persistence.jaxb.javamodel.JavaField)9 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)5 ArrayList (java.util.ArrayList)4 JavaMethod (org.eclipse.persistence.jaxb.javamodel.JavaMethod)4 Field (java.lang.reflect.Field)3 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 InstanceVariableAttributeAccessor (org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)2 MethodAttributeAccessor (org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)2 VirtualAttributeAccessor (org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor)2 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)2 CustomAccessorAttributeAccessor (org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor)2 JAXBSetMethodAttributeAccessor (org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor)2 JAXBArrayAttributeAccessor (org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor)2 MapValueAttributeAccessor (org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor)2 Field (org.eclipse.persistence.internal.oxm.mappings.Field)2 UnmarshalRecord (org.eclipse.persistence.internal.oxm.record.UnmarshalRecord)2 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)2 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)2 XmlJoinNode (org.eclipse.persistence.jaxb.xmlmodel.XmlJoinNodes.XmlJoinNode)2 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)2