Search in sources :

Example 6 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project camel by apache.

the class CoreEipAnnotationProcessor method processRefExpression.

/**
     * Special for processing an @XmlElementRef expression field
     */
private void processRefExpression(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, TypeElement classElement, XmlElementRef elementRef, VariableElement fieldElement, String fieldName, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    if ("expression".equals(fieldName)) {
        String kind = "expression";
        String name = elementRef.name();
        if (isNullOrEmpty(name) || "##default".equals(name)) {
            name = fieldName;
        }
        name = prefix + name;
        TypeMirror fieldType = fieldElement.asType();
        String fieldTypeName = fieldType.toString();
        // find javadoc from original class as it will override the setExpression method where we can provide the javadoc for the given EIP
        String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, originalClassType, true);
        // is it used as predicate (check field first and then fallback to its class / original class)
        boolean asPredicate = fieldElement.getAnnotation(AsPredicate.class) != null;
        if (!asPredicate) {
            asPredicate = classElement.getAnnotation(AsPredicate.class) != null;
        }
        if (!asPredicate) {
            asPredicate = originalClassType.getAnnotation(AsPredicate.class) != null;
        }
        // gather oneOf expression/predicates which uses language
        Set<String> oneOfTypes = new TreeSet<String>();
        for (String language : ONE_OF_LANGUAGES) {
            TypeElement languages = findTypeElement(processingEnv, roundEnv, language);
            String superClassName = canonicalClassName(languages.toString());
            // find all classes that has that superClassName
            Set<TypeElement> children = new LinkedHashSet<TypeElement>();
            findTypeElementChildren(processingEnv, roundEnv, children, superClassName);
            for (TypeElement child : children) {
                XmlRootElement rootElement = child.getAnnotation(XmlRootElement.class);
                if (rootElement != null) {
                    String childName = rootElement.name();
                    oneOfTypes.add(childName);
                }
            }
        }
        boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
        String displayName = null;
        Metadata metadata = fieldElement.getAnnotation(Metadata.class);
        if (metadata != null) {
            displayName = metadata.displayName();
        }
        EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, true, "", docComment, deprecated, false, null, true, oneOfTypes, asPredicate);
        eipOptions.add(ep);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) AsPredicate(org.apache.camel.spi.AsPredicate) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) Metadata(org.apache.camel.spi.Metadata) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements) TypeMirror(javax.lang.model.type.TypeMirror) TreeSet(java.util.TreeSet)

Example 7 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project Gargoyle by callakrsos.

the class SAXPasrerUtil method saveXml.

// private static final Logger LOGGER =
// LoggerFactory.getLogger(SAXPasrerUtil.class);
/**
	 * 특정객체를 파일로 저장
	 *
	 * 단.. 클래스에 XmlRootElement 어노테이션이 붙어 있을것
	 *
	 * @작성자 : KYJ
	 * @작성일 : 2016. 3. 16.
	 * @param file
	 * @param job
	 * @throws JAXBException
	 * @throws GargoyleException
	 */
public static <T> void saveXml(File file, T job) throws JAXBException, GargoyleException {
    if (job.getClass().getAnnotation(XmlRootElement.class) == null)
        throw new GargoyleException("this object is not contains XmlRootElement annotation");
    JAXBContext context = JAXBContext.newInstance(job.getClass());
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(job, file);
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Marshaller(javax.xml.bind.Marshaller) JAXBContext(javax.xml.bind.JAXBContext) GargoyleException(com.kyj.fx.voeditor.visual.exceptions.GargoyleException)

Example 8 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project opennms by OpenNMS.

the class JaxbUtils method getClassForElement.

public static Class<?> getClassForElement(final String elementName) {
    if (elementName == null)
        return null;
    final Class<?> existing = m_elementClasses.get(elementName);
    if (existing != null)
        return existing;
    final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(XmlRootElement.class));
    for (final BeanDefinition bd : scanner.findCandidateComponents("org.opennms")) {
        final String className = bd.getBeanClassName();
        try {
            final Class<?> clazz = Class.forName(className);
            final XmlRootElement annotation = clazz.getAnnotation(XmlRootElement.class);
            if (annotation == null) {
                LOG.warn("Somehow found class {} but it has no @XmlRootElement annotation! Skipping.", className);
                continue;
            }
            if (elementName.equalsIgnoreCase(annotation.name())) {
                LOG.trace("Found class {} for element name {}", className, elementName);
                m_elementClasses.put(elementName, clazz);
                return clazz;
            }
        } catch (final ClassNotFoundException e) {
            LOG.warn("Unable to get class object from class name {}. Skipping.", className, e);
        }
    }
    return null;
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 9 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project winery by eclipse.

the class Util method getJAXBElement.

public static <T> JAXBElement<T> getJAXBElement(Class<T> clazz, T obj) {
    String namespace = null;
    XmlRootElement xmlRootElement = clazz.getAnnotation(XmlRootElement.class);
    if (xmlRootElement != null) {
        namespace = xmlRootElement.namespace();
        if ("##default".equals(namespace)) {
            XmlSchema xmlSchema = clazz.getPackage().getAnnotation(XmlSchema.class);
            if (xmlSchema != null) {
                namespace = xmlSchema.namespace();
            } else {
                // trigger default handling
                namespace = null;
            }
        }
    }
    if (namespace == null) {
        // fallback non-specified namespaces
        namespace = Namespaces.TOSCA_NAMESPACE;
    }
    String localName = Util.getLocalName(clazz);
    QName qname = new QName(namespace, localName);
    return new JAXBElement<T>(qname, clazz, obj);
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlSchema(javax.xml.bind.annotation.XmlSchema) QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement)

Example 10 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project cxf by apache.

the class JAXBExtensionHelper method addExtensions.

public static void addExtensions(ExtensionRegistry registry, Class<?> parentType, Class<?> cls, String namespace, ClassLoader loader) throws JAXBException {
    JAXBExtensionHelper helper = new JAXBExtensionHelper(cls, namespace);
    boolean found = false;
    Class<?> extCls = cls;
    try {
        Class<?> objectFactory = Class.forName(PackageUtils.getPackageName(cls) + ".ObjectFactory", true, loader);
        Method[] methods = ReflectionUtil.getDeclaredMethods(objectFactory);
        for (Method method : methods) {
            if (method.getParameterTypes().length == 1 && method.getParameterTypes()[0].equals(cls)) {
                XmlElementDecl elementDecl = method.getAnnotation(XmlElementDecl.class);
                if (null != elementDecl) {
                    String name = elementDecl.name();
                    String ns = namespace != null ? namespace : elementDecl.namespace();
                    if (namespace != null) {
                        helper.setJaxbNamespace(elementDecl.namespace());
                    }
                    QName elementType = new QName(ns, name);
                    if (!ExtensibilityElement.class.isAssignableFrom(extCls)) {
                        extCls = createExtensionClass(cls, elementType, loader);
                        helper.setExtensionClass(extCls);
                    }
                    registry.registerDeserializer(parentType, elementType, helper);
                    registry.registerSerializer(parentType, elementType, helper);
                    registry.mapExtensionTypes(parentType, elementType, extCls);
                    found = true;
                }
            }
        }
    } catch (ClassNotFoundException ex) {
    // ignore
    }
    if (!found) {
        // not in object factory or no object factory, try other annotations
        XmlRootElement elAnnot = cls.getAnnotation(XmlRootElement.class);
        if (elAnnot != null) {
            String name = elAnnot.name();
            String ns = elAnnot.namespace();
            if (StringUtils.isEmpty(ns) || "##default".equals(ns)) {
                XmlSchema schema = null;
                if (cls.getPackage() != null) {
                    schema = cls.getPackage().getAnnotation(XmlSchema.class);
                }
                if (schema != null) {
                    ns = schema.namespace();
                }
            }
            if (!StringUtils.isEmpty(ns) && !StringUtils.isEmpty(name)) {
                if (namespace != null) {
                    helper.setJaxbNamespace(ns);
                    ns = namespace;
                }
                QName elementType = new QName(ns, name);
                if (!ExtensibilityElement.class.isAssignableFrom(extCls)) {
                    extCls = createExtensionClass(cls, elementType, loader);
                    helper.setExtensionClass(extCls);
                }
                registry.registerDeserializer(parentType, elementType, helper);
                registry.registerSerializer(parentType, elementType, helper);
                registry.mapExtensionTypes(parentType, elementType, extCls);
                found = true;
            }
        }
    }
    if (!found) {
        LOG.log(Level.WARNING, "EXTENSION_NOT_REGISTERED", new Object[] { cls.getName(), parentType.getName() });
    }
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlSchema(javax.xml.bind.annotation.XmlSchema) QName(javax.xml.namespace.QName) XmlElementDecl(javax.xml.bind.annotation.XmlElementDecl) Method(java.lang.reflect.Method) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement)

Aggregations

XmlRootElement (javax.xml.bind.annotation.XmlRootElement)39 XmlSchema (javax.xml.bind.annotation.XmlSchema)11 XmlType (javax.xml.bind.annotation.XmlType)9 QName (javax.xml.namespace.QName)9 LinkedHashSet (java.util.LinkedHashSet)6 TreeSet (java.util.TreeSet)6 TypeElement (javax.lang.model.element.TypeElement)6 AnnotationProcessorHelper.findTypeElement (org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement)6 Method (java.lang.reflect.Method)5 TypeMirror (javax.lang.model.type.TypeMirror)5 Elements (javax.lang.model.util.Elements)5 XmlElements (javax.xml.bind.annotation.XmlElements)5 Metadata (org.apache.camel.spi.Metadata)5 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 XmlAccessorType (javax.xml.bind.annotation.XmlAccessorType)3 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)2 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)2