Search in sources :

Example 1 with JAXBException

use of org.eclipse.persistence.exceptions.JAXBException in project eclipselink by eclipse-ee4j.

the class TypeMappingInfoNullTypeTestCases method testNullTypeNullTagName.

public void testNullTypeNullTagName() throws Exception {
    TypeMappingInfo[] tmis = new TypeMappingInfo[1];
    TypeMappingInfo tmi = new TypeMappingInfo();
    tmi.setType(null);
    tmis[0] = tmi;
    try {
        JAXBContext ctx = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(tmis, null, Thread.currentThread().getContextClassLoader());
    } catch (JAXBException ex) {
        assertEquals(JAXBException.NULL_TYPE_ON_TYPEMAPPINGINFO, ex.getErrorCode());
        return;
    }
    fail("A JAXBException should have happened but didn't");
}
Also used : JAXBException(org.eclipse.persistence.exceptions.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) TypeMappingInfo(org.eclipse.persistence.jaxb.TypeMappingInfo)

Example 2 with JAXBException

use of org.eclipse.persistence.exceptions.JAXBException in project eclipselink by eclipse-ee4j.

the class TypeMappingInfoNullTypeTestCases method testNullTypeNonNullTagName.

public void testNullTypeNonNullTagName() throws Exception {
    TypeMappingInfo[] tmis = new TypeMappingInfo[1];
    TypeMappingInfo tmi = new TypeMappingInfo();
    tmi.setType(null);
    QName tagname = new QName("someUri", "someLocalName");
    tmi.setXmlTagName(tagname);
    tmis[0] = tmi;
    try {
        JAXBContext ctx = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(tmis, null, Thread.currentThread().getContextClassLoader());
    } catch (JAXBException ex) {
        assertEquals(JAXBException.NULL_TYPE_ON_TYPEMAPPINGINFO, ex.getErrorCode());
        return;
    }
    fail("A JAXBException should have happened but didn't");
}
Also used : QName(javax.xml.namespace.QName) JAXBException(org.eclipse.persistence.exceptions.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) TypeMappingInfo(org.eclipse.persistence.jaxb.TypeMappingInfo)

Example 3 with JAXBException

use of org.eclipse.persistence.exceptions.JAXBException 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 4 with JAXBException

use of org.eclipse.persistence.exceptions.JAXBException in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method processXmlJavaTypeAdapter.

/**
 * Process @XmlJavaTypeAdapter on a given property.
 */
private void processXmlJavaTypeAdapter(Property property, TypeInfo info, JavaClass javaClass) {
    JavaClass adapterClass = null;
    JavaClass ptype = property.getActualType();
    if (helper.isAnnotationPresent(property.getElement(), XmlJavaTypeAdapter.class)) {
        XmlJavaTypeAdapter adapter = (XmlJavaTypeAdapter) helper.getAnnotation(property.getElement(), XmlJavaTypeAdapter.class);
        org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter xja = new org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter();
        xja.setValue(adapter.value().getName());
        xja.setType(adapter.type().getName());
        property.setXmlJavaTypeAdapter(xja);
    } else {
        TypeInfo ptypeInfo = typeInfos.get(ptype.getQualifiedName());
        org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter xmlJavaTypeAdapter;
        if (ptypeInfo == null && shouldGenerateTypeInfo(ptype)) {
            if (helper.isAnnotationPresent(ptype, XmlJavaTypeAdapter.class)) {
                XmlJavaTypeAdapter adapter = (XmlJavaTypeAdapter) helper.getAnnotation(ptype, XmlJavaTypeAdapter.class);
                org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter xja = new org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter();
                xja.setValue(adapter.value().getName());
                /*String boundType = adapter.type().getName();
                    if (boundType == null || boundType.equals("jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter.DEFAULT")) {
                        boundType = ptype.getRawName();
                    } value from boundType is not used - fix if you know what it should do. */
                xja.setType(adapter.type().getName());
                property.setXmlJavaTypeAdapter(xja);
            }
        }
        if (ptypeInfo != null) {
            if (null != (xmlJavaTypeAdapter = ptypeInfo.getXmlJavaTypeAdapter())) {
                try {
                    property.setXmlJavaTypeAdapter(xmlJavaTypeAdapter);
                } catch (JAXBException e) {
                    throw JAXBException.invalidTypeAdapterClass(xmlJavaTypeAdapter.getValue(), javaClass.getName());
                }
            }
        }
        if (info.hasPackageLevelAdaptersByClass()) {
            if (info.getPackageLevelAdaptersByClass().get(ptype.getQualifiedName()) != null && !property.isSetXmlJavaTypeAdapter()) {
                adapterClass = info.getPackageLevelAdapterClass(ptype);
                org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter xja = new org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter();
                xja.setValue(adapterClass.getQualifiedName());
                xja.setType(ptype.getQualifiedName());
                property.setXmlJavaTypeAdapter(xja);
            }
        }
    }
}
Also used : JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) XmlJavaTypeAdapter(jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter) JAXBException(org.eclipse.persistence.exceptions.JAXBException)

Example 5 with JAXBException

use of org.eclipse.persistence.exceptions.JAXBException 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)

Aggregations

JAXBException (org.eclipse.persistence.exceptions.JAXBException)24 HashMap (java.util.HashMap)11 Source (javax.xml.transform.Source)9 StreamSource (javax.xml.transform.stream.StreamSource)9 InputStream (java.io.InputStream)7 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)7 JAXBContext (jakarta.xml.bind.JAXBContext)6 ArrayList (java.util.ArrayList)3 QName (javax.xml.namespace.QName)3 XmlProperty (org.eclipse.persistence.oxm.annotations.XmlProperty)3 XmlTransient (jakarta.xml.bind.annotation.XmlTransient)2 Type (java.lang.reflect.Type)2 TypeMappingInfo (org.eclipse.persistence.jaxb.TypeMappingInfo)2 XmlBindings (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)2 XmlAnyElement (jakarta.xml.bind.annotation.XmlAnyElement)1 XmlElement (jakarta.xml.bind.annotation.XmlElement)1 XmlJavaTypeAdapter (jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Collection (java.util.Collection)1