Search in sources :

Example 1 with JavaClass

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

the class PropertyTypeTestCases method testPropertyTypes.

public void testPropertyTypes() throws Exception {
    JaxbClassLoader classLoader = new JaxbClassLoader(Thread.currentThread().getContextClassLoader());
    Generator generator = new Generator(new JavaModelInputImpl(new Class<?>[] { TestObject.class }, new JavaModelImpl(this.classLoader)));
    CoreProject proj = generator.generateProject();
    TypeInfo info = generator.getAnnotationsProcessor().getTypeInfos().get("org.eclipse.persistence.testing.jaxb.annotations.TestObject");
    Property booleanProp = info.getProperties().get("booleanTest");
    JavaClass type = booleanProp.getType();
    assertEquals("java.lang.Boolean", type.getRawName());
    Property booleanListProp = info.getProperties().get("booleanListTest");
    type = booleanListProp.getType();
    assertEquals("java.util.List", type.getRawName());
    Property byteArrayProp = info.getProperties().get("byteArrayTest");
    type = byteArrayProp.getType();
    assertEquals("java.lang.String", type.getRawName());
    Property byteArrayListProp = info.getProperties().get("byteArrayListTest");
    type = byteArrayListProp.getType();
    assertEquals("java.util.List", type.getRawName());
    assertEquals("java.lang.String", byteArrayListProp.getGenericType().getRawName());
}
Also used : JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) CoreProject(org.eclipse.persistence.core.sessions.CoreProject) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) TypeInfo(org.eclipse.persistence.jaxb.compiler.TypeInfo) Property(org.eclipse.persistence.jaxb.compiler.Property) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 2 with JavaClass

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

the class MappingsGenerator method generateInverseReferenceMapping.

private InverseReferenceMapping generateInverseReferenceMapping(Property property, Descriptor descriptor, NamespaceInfo namespace) {
    InverseReferenceMapping<AbstractSession, AttributeAccessor, ContainerPolicy, ClassDescriptor, DatabaseField, DatabaseMapping, org.eclipse.persistence.internal.oxm.record.XMLRecord> invMapping = new XMLInverseReferenceMapping();
    boolean isCollection = helper.isCollectionType(property.getType());
    if (isCollection) {
        invMapping.setReferenceClassName(property.getGenericType().getQualifiedName());
    } else {
        invMapping.setReferenceClassName(property.getType().getQualifiedName());
    }
    invMapping.setAttributeName(property.getPropertyName());
    String setMethodName = property.getInverseReferencePropertySetMethodName();
    String getMethodName = property.getInverseReferencePropertyGetMethodName();
    if (setMethodName != null && !setMethodName.equals(Constants.EMPTY_STRING)) {
        invMapping.setSetMethodName(setMethodName);
    }
    if (getMethodName != null && !getMethodName.equals(Constants.EMPTY_STRING)) {
        invMapping.setGetMethodName(getMethodName);
    }
    invMapping.setMappedBy(property.getInverseReferencePropertyName());
    if (isCollection) {
        JavaClass collectionType = property.getType();
        collectionType = containerClassImpl(collectionType);
        invMapping.useCollectionClass(org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(collectionType.getQualifiedName(), helper.getClassLoader()));
    }
    if (property.isWriteableInverseReference()) {
        if (isCollection) {
            JavaClass descriptorClass = helper.getJavaClass(descriptor.getJavaClassName());
            invMapping.setInlineMapping((XMLCompositeCollectionMapping) generateCompositeCollectionMapping(property, descriptor, descriptorClass, namespace, invMapping.getReferenceClassName()));
        } else {
            invMapping.setInlineMapping((XMLCompositeObjectMapping) generateCompositeObjectMapping(property, descriptor, namespace, invMapping.getReferenceClassName()));
        }
    }
    return invMapping;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) 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 3 with JavaClass

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

the class MappingsGenerator method generateCollectionMapping.

public Mapping generateCollectionMapping(Property property, Descriptor descriptor, JavaClass descriptorJavaClass, NamespaceInfo namespaceInfo) {
    // check to see if this should be a composite or direct mapping
    JavaClass javaClass = property.getActualType();
    if (property.isMixedContent()) {
        return generateAnyCollectionMapping(property, descriptor, namespaceInfo, true);
    }
    if (property.isXmlIdRef() || property.isSetXmlJoinNodes()) {
        return generateXMLCollectionReferenceMapping(property, descriptor, namespaceInfo, javaClass);
    }
    if (javaClass != null && typeInfo.get(javaClass.getQualifiedName()) != null) {
        TypeInfo referenceInfo = typeInfo.get(javaClass.getQualifiedName());
        if (referenceInfo.isEnumerationType()) {
            return generateEnumCollectionMapping(property, descriptor, namespaceInfo, (EnumTypeInfo) referenceInfo);
        }
        return generateCompositeCollectionMapping(property, descriptor, descriptorJavaClass, namespaceInfo, javaClass.getQualifiedName());
    }
    if (!property.isAttribute() && javaClass != null && javaClass.getQualifiedName().equals(OBJECT_CLASS_NAME)) {
        CompositeCollectionMapping ccMapping = generateCompositeCollectionMapping(property, descriptor, descriptorJavaClass, namespaceInfo, null);
        ccMapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
        return ccMapping;
    }
    if (isBinaryData(javaClass)) {
        return generateBinaryDataCollectionMapping(property, descriptor, namespaceInfo);
    }
    return generateDirectCollectionMapping(property, descriptor, namespaceInfo);
}
Also used : JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)

Example 4 with JavaClass

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

the class MappingsGenerator method generateAnyAttributeMapping.

public AnyAttributeMapping generateAnyAttributeMapping(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo) {
    XMLAnyAttributeMapping mapping = new XMLAnyAttributeMapping();
    initializeXMLMapping(mapping, property);
    initializeXMLContainerMapping(mapping, property.getType().isArray());
    // if the XPath is set (via xml-path) use it
    if (property.getXmlPath() != null) {
        mapping.setField(new XMLField(property.getXmlPath()));
    }
    mapping.setSchemaInstanceIncluded(false);
    mapping.setNamespaceDeclarationIncluded(false);
    JavaClass mapType = property.getType();
    if (areEquals(mapType, Map.class)) {
        mapType = jotHashMap;
    }
    mapping.useMapClassName(mapType.getRawName());
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) XMLAnyAttributeMapping(org.eclipse.persistence.oxm.mappings.XMLAnyAttributeMapping) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass)

Example 5 with JavaClass

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

the class MappingsGenerator method generateCompositeCollectionMapping.

public CompositeCollectionMapping generateCompositeCollectionMapping(Property property, Descriptor descriptor, JavaClass javaClass, NamespaceInfo namespaceInfo, String referenceClassName) {
    boolean nestedArray = false;
    CompositeCollectionMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, UnmarshalKeepAsElementPolicy, XMLUnmarshaller, XMLRecord> mapping = new XMLCompositeCollectionMapping();
    initializeXMLMapping((XMLMapping) mapping, property);
    initializeXMLContainerMapping(mapping, property.getType().isArray());
    JavaClass manyValueJavaClass = helper.getJavaClass(ManyValue.class);
    if (manyValueJavaClass.isAssignableFrom(javaClass)) {
        mapping.setReuseContainer(false);
    }
    // handle null policy set via xml metadata
    if (property.isSetNullPolicy()) {
        mapping.setNullPolicy(getNullPolicyFromProperty(property, getNamespaceResolverForDescriptor(namespaceInfo)));
    } else if (property.isNillable()) {
        mapping.getNullPolicy().setNullRepresentedByXsiNil(true);
        mapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
    }
    if (property.isSetXmlElementWrapper()) {
        mapping.setWrapperNullPolicy(getWrapperNullPolicyFromProperty(property));
    }
    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);
    } else if (helper.isMapType(property.getType())) {
        Class<?> generatedClass = generateMapEntryClassAndDescriptor(property, descriptor.getNonNullNamespaceResolver());
        referenceClassName = generatedClass.getName();
        String mapClassName = property.getType().getRawName();
        mapping.setAttributeAccessor(new MapValueAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy(), generatedClass, mapClassName, helper.getClassLoader()));
    }
    // Nested array check (used in JSON marshalling)
    if (collectionType.getComponentType() == null) {
        if ((collectionType.isArray() || helper.isCollectionType(collectionType)) && (referenceClassName != null && referenceClassName.contains(AnnotationsProcessor.ARRAY_PACKAGE_NAME))) {
            nestedArray = true;
        }
    } else if ((collectionType.isArray() || helper.isCollectionType(collectionType)) && (collectionType.getComponentType().isArray() || helper.isCollectionType(collectionType.getComponentType()))) {
        nestedArray = true;
    }
    collectionType = containerClassImpl(collectionType);
    mapping.useCollectionClassName(collectionType.getRawName());
    // if the XPath is set (via xml-path) use it; otherwise figure it out
    Field xmlField = getXPathForField(property, namespaceInfo, false, false);
    if (helper.isMapType(property.getType())) {
        JavaClass mapValueClass = helper.getJavaClass(MapValue.class);
        if (mapValueClass.isAssignableFrom(javaClass)) {
            mapping.setXPath("entry");
        } else {
            mapping.setXPath(xmlField.getXPath() + "/entry");
        }
    } else {
        mapping.setXPath(xmlField.getXPath());
    }
    if (referenceClassName == null) {
        setTypedTextField((Field) mapping.getField());
    } else {
        mapping.setReferenceClassName(referenceClassName);
    }
    if (property.isTransientType()) {
        mapping.setReferenceClassName(Constants.UNKNOWN_OR_TRANSIENT_CLASS);
    }
    if (property.isRequired()) {
        ((Field) mapping.getField()).setRequired(true);
    }
    ((Field) mapping.getField()).setNestedArray(nestedArray);
    return mapping;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) MapValueAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) 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) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) CoreConverter(org.eclipse.persistence.core.mappings.converters.CoreConverter) JAXBEnumTypeConverter(org.eclipse.persistence.jaxb.JAXBEnumTypeConverter) JAXBElementConverter(org.eclipse.persistence.internal.jaxb.JAXBElementConverter) XMLListConverter(org.eclipse.persistence.oxm.mappings.converters.XMLListConverter) XMLConverter(org.eclipse.persistence.oxm.mappings.converters.XMLConverter) Converter(org.eclipse.persistence.mappings.converters.Converter) DefaultElementConverter(org.eclipse.persistence.internal.jaxb.DefaultElementConverter) DomHandlerConverter(org.eclipse.persistence.internal.jaxb.DomHandlerConverter) JAXBElementRootConverter(org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) UnmarshalKeepAsElementPolicy(org.eclipse.persistence.oxm.mappings.UnmarshalKeepAsElementPolicy) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) 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) Session(org.eclipse.persistence.sessions.Session) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)109 ArrayList (java.util.ArrayList)22 QName (javax.xml.namespace.QName)19 XMLField (org.eclipse.persistence.oxm.XMLField)18 JAXBException (org.eclipse.persistence.exceptions.JAXBException)15 JavaField (org.eclipse.persistence.jaxb.javamodel.JavaField)15 JavaMethod (org.eclipse.persistence.jaxb.javamodel.JavaMethod)14 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)13 Field (org.eclipse.persistence.internal.oxm.mappings.Field)12 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)10 JAXBArrayAttributeAccessor (org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor)10 Collection (java.util.Collection)8 CoreMapping (org.eclipse.persistence.core.mappings.CoreMapping)7 JAXBSetMethodAttributeAccessor (org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor)7 BinaryDataMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping)7 XmlProperty (org.eclipse.persistence.oxm.annotations.XmlProperty)7 HashMap (java.util.HashMap)6 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)6 CoreDescriptor (org.eclipse.persistence.core.descriptors.CoreDescriptor)5 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)5