Search in sources :

Example 1 with JAnnotationClassValue

use of com.sun.codemodel.JAnnotationClassValue 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)

Aggregations

JAnnotationArrayMember (com.sun.codemodel.JAnnotationArrayMember)1 JAnnotationClassValue (com.sun.codemodel.JAnnotationClassValue)1 JAnnotationStringValue (com.sun.codemodel.JAnnotationStringValue)1 JAnnotationUse (com.sun.codemodel.JAnnotationUse)1 JAnnotationValue (com.sun.codemodel.JAnnotationValue)1 JClass (com.sun.codemodel.JClass)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JavaAnnotation (org.eclipse.persistence.jaxb.javamodel.JavaAnnotation)1