Search in sources :

Example 1 with JAnnotationValue

use of com.sun.codemodel.JAnnotationValue 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 2 with JAnnotationValue

use of com.sun.codemodel.JAnnotationValue in project jstuff by sebthom.

the class FieldInstantiatingPlugin method run.

@Override
public boolean run(final Outline outline, final Options options, final ErrorHandler errorHandler) throws SAXException {
    // collect all types defined in the XSD
    final List<JType> typeDefs = new ArrayList<>();
    for (final ClassOutline classDef : outline.getClasses()) {
        typeDefs.add(classDef.implClass);
    }
    // scan all XSD based classes for field references to other XSD based classes
    for (final ClassOutline classDef : outline.getClasses()) {
        for (final JFieldVar fieldDecl : classDef.implClass.fields().values()) {
            /*
             * @XmlElementRefs({
             *    @XmlElementRef(name = "bike", namespace = "my-config", type = JAXBElement.class, required = false),
             *    @XmlElementRef(name = "car", namespace = "my-config", type = JAXBElement.class, required = false)
             * })
             * private List<JAXBElement<?>> bikesAndCars;
             */
            final Field memberValueFields = Fields.find(JAnnotationUse.class, "memberValues");
            for (final JAnnotationUse a : fieldDecl.annotations()) {
                if (// 
                jakarta.xml.bind.annotation.XmlElementRefs.class.getName().equals(a.getAnnotationClass().binaryName()) || // 
                "javax.xml.bind.annotation.XmlElementRefs".equals(a.getAnnotationClass().binaryName())) {
                    for (final JAnnotationUse xmlElementRefAnno : ((JAnnotationArrayMember) a.getAnnotationMembers().get("value")).annotations()) {
                        final JAnnotationValue requiredAttribute = xmlElementRefAnno.getAnnotationMembers().get("required");
                        if (requiredAttribute != null) {
                            ((Map<?, ?>) Fields.read(xmlElementRefAnno, memberValueFields)).remove("required");
                        }
                    }
                }
            }
            if (!typeDefs.contains(fieldDecl.type())) {
                continue;
            }
            FieldOutline fieldDef = null;
            for (final FieldOutline f : classDef.getDeclaredFields()) {
                if (f.getPropertyInfo().getName(false).equals(fieldDecl.name())) {
                    fieldDef = f;
                }
            }
            if (fieldDef == null)
                throw new IllegalStateException("FieldOutline not found for " + fieldDecl.name());
            boolean doInstantiate = false;
            for (final CPluginCustomization pc : findCustomizations(fieldDef.getPropertyInfo().getCustomizations(), CUSTOMIZATION_ENABLED_TAG)) {
                pc.markAsAcknowledged();
                doInstantiate = true;
            }
            // initialize field
            if (doInstantiate) {
                LOG.info("%s#%s = new %s()", classDef.implClass.name(), fieldDecl.name(), fieldDecl.type().name());
                fieldDecl.init(JExpr._new(fieldDecl.type()));
            } else {
                LOG.info("Not instantiating %s#%s", classDef.implClass.name(), fieldDecl.name());
            }
        }
    }
    return true;
}
Also used : CPluginCustomization(com.sun.tools.xjc.model.CPluginCustomization) ArrayList(java.util.ArrayList) JAnnotationArrayMember(com.sun.codemodel.JAnnotationArrayMember) ClassOutline(com.sun.tools.xjc.outline.ClassOutline) Field(java.lang.reflect.Field) JFieldVar(com.sun.codemodel.JFieldVar) JAnnotationUse(com.sun.codemodel.JAnnotationUse) JAnnotationValue(com.sun.codemodel.JAnnotationValue) Map(java.util.Map) JType(com.sun.codemodel.JType) FieldOutline(com.sun.tools.xjc.outline.FieldOutline)

Aggregations

JAnnotationArrayMember (com.sun.codemodel.JAnnotationArrayMember)2 JAnnotationUse (com.sun.codemodel.JAnnotationUse)2 JAnnotationValue (com.sun.codemodel.JAnnotationValue)2 ArrayList (java.util.ArrayList)2 JAnnotationClassValue (com.sun.codemodel.JAnnotationClassValue)1 JAnnotationStringValue (com.sun.codemodel.JAnnotationStringValue)1 JClass (com.sun.codemodel.JClass)1 JFieldVar (com.sun.codemodel.JFieldVar)1 JType (com.sun.codemodel.JType)1 CPluginCustomization (com.sun.tools.xjc.model.CPluginCustomization)1 ClassOutline (com.sun.tools.xjc.outline.ClassOutline)1 FieldOutline (com.sun.tools.xjc.outline.FieldOutline)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JavaAnnotation (org.eclipse.persistence.jaxb.javamodel.JavaAnnotation)1