Java XML QName getXmlElementRefOrElementQName(Class jaxbClass, Field field)

Here you can find the source of getXmlElementRefOrElementQName(Class jaxbClass, Field field)

Description

Get the name of the field by looking at the XmlElement annotation.

License

Apache License

Parameter

Parameter Description
jaxbClass a parameter
fieldName a parameter

Exception

Parameter Description
NoSuchFieldException an exception

Declaration

private static QName getXmlElementRefOrElementQName(Class<?> jaxbClass, Field field)
        throws NoSuchFieldException 

Method Source Code

//package com.java2s;
/*//from  w w  w .  ja v  a  2  s .c o m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;

import javax.xml.namespace.QName;

public class Main {
    /**
     * Get the name of the field by looking at the XmlElement annotation.
     *
     * @param jaxbClass
     * @param fieldName
     * @return
     * @throws NoSuchFieldException
     */
    private static QName getXmlElementRefOrElementQName(Class<?> jaxbClass, Field field)
            throws NoSuchFieldException {
        XmlElementRef xmlElementRef = (XmlElementRef) getAnnotation(field, XmlElementRef.class);
        if (xmlElementRef != null) {
            return new QName(xmlElementRef.namespace(), xmlElementRef.name());
        }
        XmlElement xmlElement = (XmlElement) getAnnotation(field, XmlElement.class);

        // If XmlElement does not exist, default to using the field name
        if (xmlElement == null || xmlElement.name().equals("##default")) {
            return new QName("", field.getName());
        }
        return new QName(xmlElement.namespace(), xmlElement.name());
    }

    /**
     * Get an annotation.  This is wrappered to avoid a Java2Security violation.
     * @param cls Class that contains annotation 
     * @param annotation Class of requrested Annotation
     * @return annotation or null
     */
    private static <T extends Annotation> T getAnnotation(final AnnotatedElement element,
            final Class<T> annotation) {
        return AccessController.doPrivileged(new PrivilegedAction<T>() {
            public T run() {
                return element.getAnnotation(annotation);
            }
        });
    }
}

Related

  1. getThreddsCatalogAttributeQName(String localName)
  2. getTypeFromQName(final QName name)
  3. getTypeQName(Class type)
  4. getUniquePOAName(QName serviceName, String portName, String poaName)
  5. getValueAsQName(XMLStreamReader reader, String value)
  6. getXmlQName(final NamespaceContext context, final String value)
  7. getXmlRootElementQName(Class clazz)
  8. getXMLValue(String xml, String xQuery, QName resultType)
  9. getXSTypeAsString(QName qname)