Search in sources :

Example 1 with XmlAttachmentRef

use of jakarta.xml.bind.annotation.XmlAttachmentRef in project metro-jax-ws by eclipse-ee4j.

the class WrapperBeanGenerator method createBeanImage.

// Creates class's bytes
private static byte[] createBeanImage(String className, String rootName, String rootNS, String typeName, String typeNS, Collection<Field> fields) throws Exception {
    ClassWriter cw = new ClassWriter(0);
    // org.objectweb.asm.util.TraceClassVisitor cw = new org.objectweb.asm.util.TraceClassVisitor(actual, new java.io.PrintWriter(System.out));
    cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, replaceDotWithSlash(className), null, "java/lang/Object", null);
    AnnotationVisitor root = cw.visitAnnotation("Ljakarta/xml/bind/annotation/XmlRootElement;", true);
    root.visit("name", rootName);
    root.visit("namespace", rootNS);
    root.visitEnd();
    AnnotationVisitor type = cw.visitAnnotation("Ljakarta/xml/bind/annotation/XmlType;", true);
    type.visit("name", typeName);
    type.visit("namespace", typeNS);
    if (fields.size() > 1) {
        AnnotationVisitor propVisitor = type.visitArray("propOrder");
        for (Field field : fields) {
            propVisitor.visit("propOrder", field.fieldName);
        }
        propVisitor.visitEnd();
    }
    type.visitEnd();
    for (Field field : fields) {
        FieldVisitor fv = cw.visitField(Opcodes.ACC_PUBLIC, field.fieldName, field.asmType.getDescriptor(), field.getSignature(), null);
        for (Annotation ann : field.jaxbAnnotations) {
            if (ann instanceof XmlMimeType) {
                AnnotationVisitor mime = fv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlMimeType;", true);
                mime.visit("value", ((XmlMimeType) ann).value());
                mime.visitEnd();
            } else if (ann instanceof XmlJavaTypeAdapter) {
                AnnotationVisitor ada = fv.visitAnnotation("Ljakarta/xml/bind/annotation/adapters/XmlJavaTypeAdapter;", true);
                ada.visit("value", getASMType(((XmlJavaTypeAdapter) ann).value()));
                // XmlJavaTypeAdapter.type() is for package only. No need to copy.
                // ada.visit("type", ((XmlJavaTypeAdapter)ann).type());
                ada.visitEnd();
            } else if (ann instanceof XmlAttachmentRef) {
                AnnotationVisitor att = fv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlAttachmentRef;", true);
                att.visitEnd();
            } else if (ann instanceof XmlList) {
                AnnotationVisitor list = fv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlList;", true);
                list.visitEnd();
            } else if (ann instanceof XmlElement) {
                AnnotationVisitor elem = fv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlElement;", true);
                XmlElement xmlElem = (XmlElement) ann;
                elem.visit("name", xmlElem.name());
                elem.visit("namespace", xmlElem.namespace());
                if (xmlElem.nillable()) {
                    elem.visit("nillable", true);
                }
                if (xmlElem.required()) {
                    elem.visit("required", true);
                }
                elem.visitEnd();
            } else {
                throw new WebServiceException("Unknown JAXB annotation " + ann);
            }
        }
        fv.visitEnd();
    }
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    cw.visitEnd();
    if (LOGGER.isLoggable(Level.FINE)) {
        // Class's @XmlRootElement
        StringBuilder sb = new StringBuilder();
        sb.append("\n");
        sb.append("@XmlRootElement(name=").append(rootName).append(", namespace=").append(rootNS).append(")");
        // Class's @XmlType
        sb.append("\n");
        sb.append("@XmlType(name=").append(typeName).append(", namespace=").append(typeNS);
        if (fields.size() > 1) {
            sb.append(", propOrder={");
            for (Field field : fields) {
                sb.append(" ");
                sb.append(field.fieldName);
            }
            sb.append(" }");
        }
        sb.append(")");
        // class declaration
        sb.append("\n");
        sb.append("public class ").append(className).append(" {");
        // fields declaration
        for (Field field : fields) {
            sb.append("\n");
            // Field's other JAXB annotations
            for (Annotation ann : field.jaxbAnnotations) {
                sb.append("\n    ");
                if (ann instanceof XmlMimeType) {
                    sb.append("@XmlMimeType(value=").append(((XmlMimeType) ann).value()).append(")");
                } else if (ann instanceof XmlJavaTypeAdapter) {
                    sb.append("@XmlJavaTypeAdapter(value=").append(getASMType(((XmlJavaTypeAdapter) ann).value())).append(")");
                } else if (ann instanceof XmlAttachmentRef) {
                    sb.append("@XmlAttachmentRef");
                } else if (ann instanceof XmlList) {
                    sb.append("@XmlList");
                } else if (ann instanceof XmlElement) {
                    XmlElement xmlElem = (XmlElement) ann;
                    sb.append("\n    ");
                    sb.append("@XmlElement(name=").append(xmlElem.name()).append(", namespace=").append(xmlElem.namespace());
                    if (xmlElem.nillable()) {
                        sb.append(", nillable=true");
                    }
                    if (xmlElem.required()) {
                        sb.append(", required=true");
                    }
                    sb.append(")");
                } else {
                    throw new WebServiceException("Unknown JAXB annotation " + ann);
                }
            }
            // Field declaration
            sb.append("\n    ");
            sb.append("public ");
            if (field.getSignature() == null) {
                sb.append(field.asmType.getDescriptor());
            } else {
                sb.append(field.getSignature());
            }
            sb.append(" ");
            sb.append(field.fieldName);
        }
        sb.append("\n\n}");
        LOGGER.fine(sb.toString());
    }
    return cw.toByteArray();
}
Also used : XmlMimeType(jakarta.xml.bind.annotation.XmlMimeType) WebServiceException(jakarta.xml.ws.WebServiceException) XmlJavaTypeAdapter(jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter) FieldVisitor(com.sun.xml.ws.org.objectweb.asm.FieldVisitor) ClassWriter(com.sun.xml.ws.org.objectweb.asm.ClassWriter) Annotation(java.lang.annotation.Annotation) XmlList(jakarta.xml.bind.annotation.XmlList) MethodVisitor(com.sun.xml.ws.org.objectweb.asm.MethodVisitor) XmlAttachmentRef(jakarta.xml.bind.annotation.XmlAttachmentRef) AnnotationVisitor(com.sun.xml.ws.org.objectweb.asm.AnnotationVisitor) XmlElement(jakarta.xml.bind.annotation.XmlElement)

Example 2 with XmlAttachmentRef

use of jakarta.xml.bind.annotation.XmlAttachmentRef in project metro-jax-ws by eclipse-ee4j.

the class WebServiceWrapperGenerator method annotateParameterWithJaxbAnnotations.

private void annotateParameterWithJaxbAnnotations(MemberInfo memInfo, JFieldVar field) {
    List<Annotation> jaxbAnnotations = memInfo.getJaxbAnnotations();
    for (Annotation ann : jaxbAnnotations) {
        if (ann instanceof XmlMimeType) {
            JAnnotationUse jaxbAnn = field.annotate(XmlMimeType.class);
            jaxbAnn.param("value", ((XmlMimeType) ann).value());
        } else if (ann instanceof XmlJavaTypeAdapter) {
            JAnnotationUse jaxbAnn = field.annotate(XmlJavaTypeAdapter.class);
            XmlJavaTypeAdapter ja = (XmlJavaTypeAdapter) ann;
            try {
                ja.value();
                throw new AssertionError();
            } catch (MirroredTypeException e) {
                jaxbAnn.param("value", getType(e.getTypeMirror()));
            }
        // XmlJavaTypeAdapter.type() is for package only. No need to copy.
        } else if (ann instanceof XmlAttachmentRef) {
            field.annotate(XmlAttachmentRef.class);
        } else if (ann instanceof XmlList) {
            field.annotate(XmlList.class);
        } else if (ann instanceof XmlElement) {
            XmlElement elemAnn = (XmlElement) ann;
            JAnnotationUse jAnn = field.annotate(XmlElement.class);
            jAnn.param("name", elemAnn.name());
            jAnn.param("namespace", elemAnn.namespace());
            if (elemAnn.nillable()) {
                jAnn.param("nillable", true);
            }
            if (elemAnn.required()) {
                jAnn.param("required", true);
            }
        } else {
            throw new WebServiceException("SEI Parameter cannot have this JAXB annotation: " + ann);
        }
    }
}
Also used : MirroredTypeException(javax.lang.model.type.MirroredTypeException) XmlMimeType(jakarta.xml.bind.annotation.XmlMimeType) WebServiceException(jakarta.xml.ws.WebServiceException) XmlAttachmentRef(jakarta.xml.bind.annotation.XmlAttachmentRef) XmlJavaTypeAdapter(jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter) JAnnotationUse(com.sun.codemodel.JAnnotationUse) XmlElement(jakarta.xml.bind.annotation.XmlElement) Annotation(java.lang.annotation.Annotation) XmlList(jakarta.xml.bind.annotation.XmlList)

Example 3 with XmlAttachmentRef

use of jakarta.xml.bind.annotation.XmlAttachmentRef 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)

Example 4 with XmlAttachmentRef

use of jakarta.xml.bind.annotation.XmlAttachmentRef in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method getAnnotations.

/**
 * Returns an array of Annotations for a given TypeMappingInfo. This array
 * will either be populated from the TypeMappingInfo's array of annotations,
 * or based on an xml-element if present. The xml-element will take
 * precedence over the annotation array; if there is an xml-element the
 * Array of Annotations will be ignored.
 */
private java.lang.annotation.Annotation[] getAnnotations(TypeMappingInfo tmInfo) {
    if (tmInfo.getXmlElement() != null) {
        ClassLoader loader = helper.getClassLoader();
        // create a single ConversionManager for that will be shared by the
        // proxy objects
        ConversionManager cMgr = new ConversionManager();
        cMgr.setLoader(loader);
        // unmarshal the node into an XmlElement
        org.eclipse.persistence.jaxb.xmlmodel.XmlElement xElt = CompilerHelper.getXmlElement(tmInfo.getXmlElement(), loader);
        List<Annotation> annotations = new ArrayList<>();
        // where applicable, a given dynamic proxy will contain a Map of
        // method name/return value entries
        Map<String, Object> components = null;
        // handle @XmlElement: set 'type' method
        if (!(xElt.getType().equals("jakarta.xml.bind.annotation.XmlElement.DEFAULT"))) {
            components = new HashMap<>();
            components.put(TYPE_METHOD_NAME, xElt.getType());
            annotations.add(AnnotationProxy.getProxy(components, XmlElement.class, loader, cMgr));
        }
        // handle @XmlList
        if (xElt.isXmlList()) {
            annotations.add(AnnotationProxy.getProxy(components, XmlList.class, loader, cMgr));
        }
        // handle @XmlAttachmentRef
        if (xElt.isXmlAttachmentRef()) {
            annotations.add(AnnotationProxy.getProxy(components, XmlAttachmentRef.class, loader, cMgr));
        }
        // handle @XmlMimeType: set 'value' method
        if (xElt.getXmlMimeType() != null) {
            components = new HashMap<>();
            components.put(VALUE_METHOD_NAME, xElt.getXmlMimeType());
            annotations.add(AnnotationProxy.getProxy(components, XmlMimeType.class, loader, cMgr));
        }
        // handle @XmlJavaTypeAdapter: set 'type' and 'value' methods
        if (xElt.getXmlJavaTypeAdapter() != null) {
            components = new HashMap<>();
            components.put(TYPE_METHOD_NAME, xElt.getXmlJavaTypeAdapter().getType());
            components.put(VALUE_METHOD_NAME, xElt.getXmlJavaTypeAdapter().getValue());
            annotations.add(AnnotationProxy.getProxy(components, XmlJavaTypeAdapter.class, loader, cMgr));
        }
        // return the newly created array of dynamic proxy objects
        return annotations.toArray(new Annotation[annotations.size()]);
    }
    // array of Annotation objects
    return tmInfo.getAnnotations();
}
Also used : XmlMimeType(jakarta.xml.bind.annotation.XmlMimeType) XmlJavaTypeAdapter(jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter) ArrayList(java.util.ArrayList) JavaAnnotation(org.eclipse.persistence.jaxb.javamodel.JavaAnnotation) Annotation(java.lang.annotation.Annotation) XmlList(jakarta.xml.bind.annotation.XmlList) XmlAttachmentRef(jakarta.xml.bind.annotation.XmlAttachmentRef) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager) XmlElement(jakarta.xml.bind.annotation.XmlElement)

Aggregations

XmlAttachmentRef (jakarta.xml.bind.annotation.XmlAttachmentRef)4 XmlMimeType (jakarta.xml.bind.annotation.XmlMimeType)4 Annotation (java.lang.annotation.Annotation)4 XmlElement (jakarta.xml.bind.annotation.XmlElement)3 XmlList (jakarta.xml.bind.annotation.XmlList)3 XmlJavaTypeAdapter (jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter)3 WebServiceException (jakarta.xml.ws.WebServiceException)2 JavaAnnotation (org.eclipse.persistence.jaxb.javamodel.JavaAnnotation)2 JAnnotationUse (com.sun.codemodel.JAnnotationUse)1 AnnotationVisitor (com.sun.xml.ws.org.objectweb.asm.AnnotationVisitor)1 ClassWriter (com.sun.xml.ws.org.objectweb.asm.ClassWriter)1 FieldVisitor (com.sun.xml.ws.org.objectweb.asm.FieldVisitor)1 MethodVisitor (com.sun.xml.ws.org.objectweb.asm.MethodVisitor)1 ArrayList (java.util.ArrayList)1 MirroredTypeException (javax.lang.model.type.MirroredTypeException)1 QName (javax.xml.namespace.QName)1 Source (javax.xml.transform.Source)1 ConversionManager (org.eclipse.persistence.internal.helper.ConversionManager)1 JaxbClassLoader (org.eclipse.persistence.internal.jaxb.JaxbClassLoader)1 XMLConversionManager (org.eclipse.persistence.internal.oxm.XMLConversionManager)1