Search in sources :

Example 6 with JavaMethod

use of org.eclipse.persistence.jaxb.javamodel.JavaMethod 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 7 with JavaMethod

use of org.eclipse.persistence.jaxb.javamodel.JavaMethod 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)

Example 8 with JavaMethod

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

the class AnnotationsProcessor method postProcessXmlType.

/**
 * Process XmlType for a given TypeInfo. Here we assume that the TypeInfo
 * has an XmlType set - typically via preProcessXmlType or XmlProcessor
 * override.
 */
private void postProcessXmlType(JavaClass javaClass, TypeInfo info, PackageInfo packageNamespace) {
    // assumes that the TypeInfo has an XmlType set from
    org.eclipse.persistence.jaxb.xmlmodel.XmlType xmlType = info.getXmlType();
    // set/validate factoryClass and factoryMethod
    String factoryClassName = xmlType.getFactoryClass();
    String factoryMethodName = xmlType.getFactoryMethod();
    if (factoryClassName.equals("jakarta.xml.bind.annotation.XmlType.DEFAULT")) {
        if (factoryMethodName != null && !factoryMethodName.equals(EMPTY_STRING)) {
            // factory method applies to the current class - verify method
            // exists
            JavaMethod method = javaClass.getDeclaredMethod(factoryMethodName, new JavaClass[] {});
            if (method == null) {
                throw org.eclipse.persistence.exceptions.JAXBException.factoryMethodNotDeclared(factoryMethodName, javaClass.getName());
            }
            info.setFactoryMethodName(factoryMethodName);
        }
    } else {
        if (factoryMethodName == null || factoryMethodName.equals(EMPTY_STRING)) {
            throw org.eclipse.persistence.exceptions.JAXBException.factoryClassWithoutFactoryMethod(javaClass.getName());
        }
        info.setObjectFactoryClassName(factoryClassName);
        info.setFactoryMethodName(factoryMethodName);
    }
    // figure out type name
    String typeName = xmlType.getName();
    if (typeName.equals(XMLProcessor.DEFAULT)) {
        try {
            typeName = info.getXmlNameTransformer().transformTypeName(javaClass.getName());
        } catch (Exception ex) {
            throw org.eclipse.persistence.exceptions.JAXBException.exceptionDuringNameTransformation(javaClass.getName(), info.getXmlNameTransformer().getClass().getName(), ex);
        }
    }
    info.setSchemaTypeName(typeName);
    // set propOrder
    if (xmlType.isSetPropOrder()) {
        List<String> props = xmlType.getPropOrder();
        if (props.size() == 0) {
            info.setPropOrder(new String[0]);
        } else if (props.get(0).equals(EMPTY_STRING)) {
            info.setPropOrder(new String[] { EMPTY_STRING });
        } else {
            info.setPropOrder(xmlType.getPropOrder().toArray(new String[xmlType.getPropOrder().size()]));
        }
    }
    // figure out namespace
    if (xmlType.getNamespace().equals(XMLProcessor.DEFAULT)) {
        info.setClassNamespace(packageNamespace.getNamespace());
    } else {
        info.setClassNamespace(xmlType.getNamespace());
    }
}
Also used : JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod) InvocationTargetException(java.lang.reflect.InvocationTargetException) JAXBException(org.eclipse.persistence.exceptions.JAXBException) ConversionException(org.eclipse.persistence.exceptions.ConversionException)

Example 9 with JavaMethod

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

the class CompilerHelper method getTypeFromAdapterClass.

/**
 * If adapter class is null return null If there is a marshal method that
 * returns something other than Object on the adapter class return the
 * return type of that method Otherwise return Object.class
 */
public static JavaClass getTypeFromAdapterClass(JavaClass adapterClass, Helper helper) {
    if (adapterClass != null) {
        // JavaClass declJavaType = Object.class;
        JavaClass declJavaType = helper.getJavaClass(Object.class);
        // look for marshal method
        Object[] tacMethods = adapterClass.getMethods().toArray();
        for (int i = 0; i < tacMethods.length; i++) {
            JavaMethod method = (JavaMethod) tacMethods[i];
            if (method.getName().equals("marshal")) {
                JavaClass returnType = method.getReturnType();
                if (!(returnType.getQualifiedName().equals(declJavaType.getQualifiedName()))) {
                    declJavaType = returnType;
                    return declJavaType;
                }
            }
        }
        return declJavaType;
    }
    return null;
}
Also used : JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod)

Example 10 with JavaMethod

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

the class XMLProcessor method processPropertyForAccessorType.

private Property processPropertyForAccessorType(TypeInfo typeInfo, JavaAttribute javaAttribute, Property originalProperty) {
    if (originalProperty == null) {
        Property prop = createProperty(typeInfo, javaAttribute);
        if (prop != null) {
            typeInfo.addProperty(prop.getPropertyName(), prop);
        }
        return prop;
    }
    if ((javaAttribute.getXmlAccessorType() == XmlAccessType.FIELD && !(originalProperty.isMethodProperty())) || javaAttribute.getXmlAccessorType() == XmlAccessType.PROPERTY && originalProperty.isMethodProperty()) {
        return originalProperty;
    }
    originalProperty.setMethodProperty(!(originalProperty.isMethodProperty()));
    if (originalProperty.isMethodProperty()) {
        // figure out get and set method names. See if they exist.
        JavaClass jClass = this.jModelInput.getJavaModel().getClass(typeInfo.getJavaClassName());
        String propName = originalProperty.getPropertyName();
        propName = Character.toUpperCase(propName.charAt(0)) + propName.substring(1);
        String getMethodName = GET_STR + propName;
        String setMethodName = SET_STR + propName;
        JavaMethod getMethod = jClass.getDeclaredMethod(getMethodName, new JavaClass[] {});
        if (getMethod == null) {
            getMethodName = IS_STR + propName;
            getMethod = jClass.getDeclaredMethod(getMethodName, new JavaClass[] {});
        }
        JavaMethod setMethod = jClass.getDeclaredMethod(setMethodName, new JavaClass[] { originalProperty.getType() });
        if (getMethod != null) {
            originalProperty.setGetMethodName(getMethodName);
        }
        if (setMethod != null) {
            originalProperty.setSetMethodName(setMethodName);
        }
    } else {
        originalProperty.setGetMethodName(null);
        originalProperty.setSetMethodName(null);
        originalProperty.setMethodProperty(false);
    }
    return originalProperty;
}
Also used : JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod) XmlProperty(org.eclipse.persistence.jaxb.xmlmodel.XmlProperties.XmlProperty)

Aggregations

JavaMethod (org.eclipse.persistence.jaxb.javamodel.JavaMethod)12 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)9 ArrayList (java.util.ArrayList)3 JavaField (org.eclipse.persistence.jaxb.javamodel.JavaField)3 XmlElementDecl (jakarta.xml.bind.annotation.XmlElementDecl)2 Method (java.lang.reflect.Method)2 Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 JAXBException (org.eclipse.persistence.exceptions.JAXBException)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