Search in sources :

Example 6 with JavaAnnotation

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

the class JavaMethodImpl method getAnnotation.

@Override
public JavaAnnotation getAnnotation(JavaClass arg0) {
    if (arg0 != null && !isMetadataComplete) {
        Class<?> annotationClass = ((JavaClassImpl) arg0).getJavaClass();
        Annotation anno = javaModelImpl.getAnnotationHelper().getAnnotation(getAnnotatedElement(), annotationClass);
        if (anno != null) {
            return new JavaAnnotationImpl(anno);
        }
    }
    return null;
}
Also used : JavaAnnotation(org.eclipse.persistence.jaxb.javamodel.JavaAnnotation) Annotation(java.lang.annotation.Annotation)

Example 7 with JavaAnnotation

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

the class XJCJavaAnnotationImpl method getJavaAnnotation.

/**
 * Return a Java <code>Annotation</code> representation of this <code>JavaAnnotation</code>.
 *
 * @return a Java <code>Annotation</code> representation of this <code>JavaAnnotation</code>.
 */
@SuppressWarnings("unchecked")
public Annotation getJavaAnnotation() {
    try {
        Map<String, Object> components = new HashMap<>();
        // First, get all the default values for this annotation class.
        Class<Annotation> annotationClass = (Class<Annotation>) getJavaAnnotationClass();
        Method[] methods = annotationClass.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            components.put(methods[i].getName(), methods[i].getDefaultValue());
        }
        // Get the property values for this annotation instance.
        Map<String, JAnnotationValue> memberValues = xjcAnnotation.getAnnotationMembers();
        if (memberValues == null) {
            // Return an annotation with just the defaults set.
            return AnnotationProxy.getProxy(components, annotationClass, dynamicClassLoader, XMLConversionManager.getDefaultManager());
        }
        boolean isXmlEnum = annotationClass.equals(XmlEnum.class);
        // Now overwrite the default values with anything we find in the XJC annotation instance.
        for (String key : memberValues.keySet()) {
            JAnnotationValue xjcValue = memberValues.get(key);
            if (xjcValue instanceof JAnnotationArrayMember) {
                Collection<JAnnotationValue> values = ((JAnnotationArrayMember) xjcValue).annotations2();
                List<Object> valuesArray = new ArrayList<>(values.size());
                for (JAnnotationValue val : values) {
                    if (val instanceof JAnnotationUse) {
                        JAnnotationUse xjcAnno = (JAnnotationUse) val;
                        XJCJavaAnnotationImpl anno = new XJCJavaAnnotationImpl(xjcAnno, dynamicClassLoader);
                        valuesArray.add(anno.getJavaAnnotation());
                    } else if (val instanceof JAnnotationStringValue) {
                        JAnnotationStringValue value = (JAnnotationStringValue) val;
                        valuesArray.add(value.toString());
                    } else if (val instanceof JAnnotationClassValue) {
                        JAnnotationClassValue cval = (JAnnotationClassValue) val;
                        valuesArray.add(getValueFromClsValue(cval, isXmlEnum));
                    } else {
                        throw new RuntimeException("got " + val.getClass().getName());
                    }
                }
                components.put(key, valuesArray.toArray(new Object[valuesArray.size()]));
            } else if (xjcValue instanceof JAnnotationStringValue) {
                JAnnotationStringValue value = (JAnnotationStringValue) xjcValue;
                components.put(key, value.toString());
            } else if (xjcValue instanceof JAnnotationClassValue) {
                JAnnotationClassValue cval = (JAnnotationClassValue) xjcValue;
                components.put(key, getValueFromClsValue(cval, isXmlEnum));
            } else {
                throw new RuntimeException("got " + xjcValue.getClass().getName());
            }
        }
        return AnnotationProxy.getProxy(components, annotationClass, dynamicClassLoader, XMLConversionManager.getDefaultManager());
    } catch (Exception e) {
        return null;
    }
}
Also used : HashMap(java.util.HashMap) JAnnotationStringValue(com.sun.codemodel.JAnnotationStringValue) ArrayList(java.util.ArrayList) JAnnotationArrayMember(com.sun.codemodel.JAnnotationArrayMember) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) JavaAnnotation(org.eclipse.persistence.jaxb.javamodel.JavaAnnotation) JAnnotationClassValue(com.sun.codemodel.JAnnotationClassValue) JAnnotationUse(com.sun.codemodel.JAnnotationUse) JClass(com.sun.codemodel.JClass) JAnnotationValue(com.sun.codemodel.JAnnotationValue)

Example 8 with JavaAnnotation

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

the class AnnotationsProcessor method createElementsForTypeMappingInfo.

public void createElementsForTypeMappingInfo() {
    if (javaClassToTypeMappingInfos != null && !javaClassToTypeMappingInfos.isEmpty()) {
        Set<JavaClass> classes = this.javaClassToTypeMappingInfos.keySet();
        for (JavaClass nextClass : classes) {
            List<TypeMappingInfo> nextInfos = this.javaClassToTypeMappingInfos.get(nextClass);
            for (TypeMappingInfo nextInfo : nextInfos) {
                if (nextInfo != null) {
                    boolean xmlAttachmentRef = false;
                    String xmlMimeType = null;
                    java.lang.annotation.Annotation[] annotations = getAnnotations(nextInfo);
                    Class<?> adapterClass = typeMappingInfoToAdapterClasses.get(nextInfo);
                    Class<?> declJavaType = null;
                    if (adapterClass != null) {
                        declJavaType = CompilerHelper.getTypeFromAdapterClass(adapterClass);
                    }
                    if (annotations != null) {
                        for (Annotation nextAnnotation : annotations) {
                            if (nextAnnotation != null) {
                                if (nextAnnotation instanceof XmlMimeType) {
                                    XmlMimeType javaAnnotation = (XmlMimeType) nextAnnotation;
                                    xmlMimeType = javaAnnotation.value();
                                } else if (nextAnnotation instanceof XmlAttachmentRef) {
                                    xmlAttachmentRef = true;
                                    if (!this.hasSwaRef) {
                                        this.hasSwaRef = true;
                                    }
                                }
                            }
                        }
                    }
                    QName qname;
                    String nextClassName = nextClass.getQualifiedName();
                    if (declJavaType != null) {
                        nextClassName = declJavaType.getCanonicalName();
                    }
                    if (typeMappingInfosToGeneratedClasses != null) {
                        Class<?> generatedClass = typeMappingInfosToGeneratedClasses.get(nextInfo);
                        if (generatedClass != null) {
                            nextClassName = generatedClass.getCanonicalName();
                        }
                    }
                    TypeInfo nextTypeInfo = typeInfos.get(nextClassName);
                    if (nextTypeInfo != null) {
                        qname = new QName(nextTypeInfo.getClassNamespace(), nextTypeInfo.getSchemaTypeName());
                    } else {
                        qname = getUserDefinedSchemaTypes().get(nextClassName);
                        if (qname == null) {
                            if (nextClassName.equals(ClassConstants.APBYTE.getName()) || nextClassName.equals(Image.class.getName()) || nextClassName.equals(Source.class.getName()) || nextClassName.equals("jakarta.activation.DataHandler")) {
                                if (xmlAttachmentRef) {
                                    qname = Constants.SWA_REF_QNAME;
                                } else {
                                    qname = Constants.BASE_64_BINARY_QNAME;
                                }
                            } else if (nextClassName.equals(ClassConstants.OBJECT.getName())) {
                                qname = Constants.ANY_TYPE_QNAME;
                            } else if (nextClassName.equals(ClassConstants.XML_GREGORIAN_CALENDAR.getName())) {
                                qname = Constants.ANY_SIMPLE_TYPE_QNAME;
                            } else {
                                Class<?> theClass = helper.getClassForJavaClass(nextClass);
                                qname = XMLConversionManager.getDefaultJavaTypes().get(theClass);
                            }
                        }
                    }
                    if (qname != null) {
                        typeMappingInfosToSchemaTypes.put(nextInfo, qname);
                    }
                    if (nextInfo.getXmlTagName() != null) {
                        ElementDeclaration element = new ElementDeclaration(nextInfo.getXmlTagName(), nextClass, nextClass.getQualifiedName(), false);
                        element.setTypeMappingInfo(nextInfo);
                        element.setXmlMimeType(xmlMimeType);
                        element.setXmlAttachmentRef(xmlAttachmentRef);
                        element.setNillable(nextInfo.isNillable());
                        if (declJavaType != null) {
                            element.setJavaType(helper.getJavaClass(declJavaType));
                        }
                        Class<?> generatedClass = typeMappingInfosToGeneratedClasses.get(nextInfo);
                        if (generatedClass != null) {
                            element.setJavaType(helper.getJavaClass(generatedClass));
                        }
                        if (nextInfo.getElementScope() == TypeMappingInfo.ElementScope.Global) {
                            ElementDeclaration currentElement = this.getGlobalElements().get(element.getElementName());
                            if (currentElement == null) {
                                addGlobalElement(element.getElementName(), element);
                            } else {
                                // if(currentElement.getTypeMappingInfo() == null) {
                                // the global element that exists came from an annotation
                                // } else {
                                this.localElements.add(element);
                            // }
                            }
                        } else {
                            this.localElements.add(element);
                        }
                        String rootNamespace = element.getElementName().getNamespaceURI();
                        if (rootNamespace == null) {
                            rootNamespace = Constants.EMPTY_STRING;
                        }
                        if (rootNamespace.equals(Constants.EMPTY_STRING)) {
                            isDefaultNamespaceAllowed = false;
                        }
                    }
                }
            }
        }
    }
}
Also used : XmlMimeType(jakarta.xml.bind.annotation.XmlMimeType) QName(javax.xml.namespace.QName) JavaAnnotation(org.eclipse.persistence.jaxb.javamodel.JavaAnnotation) Annotation(java.lang.annotation.Annotation) Source(javax.xml.transform.Source) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) XmlAttachmentRef(jakarta.xml.bind.annotation.XmlAttachmentRef) TypeMappingInfo(org.eclipse.persistence.jaxb.TypeMappingInfo)

Aggregations

JavaAnnotation (org.eclipse.persistence.jaxb.javamodel.JavaAnnotation)8 Annotation (java.lang.annotation.Annotation)5 JAnnotationUse (com.sun.codemodel.JAnnotationUse)4 ArrayList (java.util.ArrayList)4 Source (javax.xml.transform.Source)2 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)2 JAnnotationArrayMember (com.sun.codemodel.JAnnotationArrayMember)1 JAnnotationClassValue (com.sun.codemodel.JAnnotationClassValue)1 JAnnotationStringValue (com.sun.codemodel.JAnnotationStringValue)1 JAnnotationValue (com.sun.codemodel.JAnnotationValue)1 JClass (com.sun.codemodel.JClass)1 XmlAttachmentRef (jakarta.xml.bind.annotation.XmlAttachmentRef)1 XmlElement (jakarta.xml.bind.annotation.XmlElement)1 XmlElementDecl (jakarta.xml.bind.annotation.XmlElementDecl)1 XmlMimeType (jakarta.xml.bind.annotation.XmlMimeType)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 QName (javax.xml.namespace.QName)1