Find the type adapter for the specified JAXB property. - Java XML

Java examples for XML:JAXB

Description

Find the type adapter for the specified JAXB property.

Demo Code


//package com.java2s;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

import java.beans.PropertyDescriptor;

public class Main {
    /**/*from  w ww . j a v a 2  s. c  o m*/
     * Find the type adapter for the specified JAXB property.
     *
     * @param jaxbProperty The JAXB property for which to find a type adapter.
     * @return The type adapter, or null if none was found.
     */
    public static XmlJavaTypeAdapter findTypeAdapter(
            PropertyDescriptor jaxbProperty) {
        XmlJavaTypeAdapter adapterInfo = null;

        if (jaxbProperty.getReadMethod() != null) {
            adapterInfo = jaxbProperty.getReadMethod().getAnnotation(
                    XmlJavaTypeAdapter.class);
        }

        if ((adapterInfo == null)
                && (jaxbProperty.getWriteMethod() != null)) {
            adapterInfo = jaxbProperty.getWriteMethod().getAnnotation(
                    XmlJavaTypeAdapter.class);
        }

        if (adapterInfo == null) {
            Package pckg = jaxbProperty.getReadMethod().getDeclaringClass()
                    .getPackage();
            Class<?> returnType = jaxbProperty.getReadMethod()
                    .getReturnType();

            XmlJavaTypeAdapter possibleAdapterInfo = pckg
                    .getAnnotation(XmlJavaTypeAdapter.class);
            if ((possibleAdapterInfo != null)
                    && (returnType.equals(possibleAdapterInfo.type()))) {
                adapterInfo = possibleAdapterInfo;
            } else if (pckg.isAnnotationPresent(XmlJavaTypeAdapters.class)) {
                XmlJavaTypeAdapters adapters = pckg
                        .getAnnotation(XmlJavaTypeAdapters.class);
                for (XmlJavaTypeAdapter possibility : adapters.value()) {
                    if (returnType.equals(possibility.type())) {
                        adapterInfo = possibility;
                    }
                }
            }
        }

        return adapterInfo;
    }
}

Related Tutorials