Search in sources :

Example 6 with JavaField

use of org.apache.cxf.tools.common.model.JavaField in project cxf by apache.

the class WrapperBeanFieldAnnotatorTest method testAnnotate.

@Test
public void testAnnotate() {
    JavaClass clz = new JavaClass();
    clz.setFullClassName("org.apache.cxf.tools.fortest.withannotation.doc.jaxws.SayHi");
    JavaField reqField = new JavaField("array", "String[]", "http://doc.withannotation.fortest.tools.cxf.apache.org/");
    reqField.setOwner(clz);
    List<JAnnotation> annotation = reqField.getAnnotations();
    assertEquals(0, annotation.size());
    reqField.annotate(new WrapperBeanFieldAnnotator());
    annotation = reqField.getAnnotations();
    String expectedNamespace = "http://doc.withannotation.fortest.tools.cxf.apache.org/";
    assertEquals("@XmlElement(name = \"array\", namespace = \"" + expectedNamespace + "\")", annotation.get(0).toString());
    clz.setFullClassName("org.apache.cxf.tools.fortest.withannotation.doc.jaxws.SayHiResponse");
    JavaField resField = new JavaField("return", "String[]", "http://doc.withannotation.fortest.tools.cxf.apache.org/");
    resField.setOwner(clz);
    resField.annotate(new WrapperBeanFieldAnnotator());
    annotation = resField.getAnnotations();
    assertEquals("@XmlElement(name = \"return\", namespace = \"" + expectedNamespace + "\")", annotation.get(0).toString());
}
Also used : JavaField(org.apache.cxf.tools.common.model.JavaField) JavaClass(org.apache.cxf.tools.common.model.JavaClass) JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) Test(org.junit.Test)

Example 7 with JavaField

use of org.apache.cxf.tools.common.model.JavaField in project cxf by apache.

the class FaultBeanTest method testTransform.

@Test
public void testTransform() throws Exception {
    Class<?> faultClass = Class.forName("org.apache.cxf.tools.fortest.cxf523.DBServiceFault");
    FaultBean bean = new FaultBean();
    WrapperBeanClass beanClass = bean.transform(faultClass, "org.apache.cxf.tools.fortest.cxf523.jaxws");
    assertNotNull(beanClass);
    assertEquals("DBServiceFaultBean", beanClass.getName());
    assertEquals("org.apache.cxf.tools.fortest.cxf523.jaxws", beanClass.getPackageName());
    assertEquals(1, beanClass.getFields().size());
    JavaField field = beanClass.getFields().get(0);
    assertEquals("message", field.getName());
    assertEquals("java.lang.String", field.getType());
    QName qname = beanClass.getElementName();
    assertEquals("DBServiceFault", qname.getLocalPart());
    assertEquals("http://cxf523.fortest.tools.cxf.apache.org/", qname.getNamespaceURI());
}
Also used : JavaField(org.apache.cxf.tools.common.model.JavaField) QName(javax.xml.namespace.QName) WrapperBeanClass(org.apache.cxf.tools.java2wsdl.generator.wsdl11.model.WrapperBeanClass) Test(org.junit.Test)

Example 8 with JavaField

use of org.apache.cxf.tools.common.model.JavaField in project cxf by apache.

the class ResponseWrapper method buildFields.

protected List<JavaField> buildFields(final Method method, final MessageInfo message) {
    List<JavaField> fields = new ArrayList<>();
    final Class<?> returnType = method.getReturnType();
    JavaField field = new JavaField();
    if (CollectionUtils.isEmpty(message.getMessageParts())) {
        return fields;
    }
    MessagePartInfo part = message.getMessageParts().get(0);
    field.setName(part.getName().getLocalPart());
    field.setTargetNamespace(part.getName().getNamespaceURI());
    if (method.getAnnotation(WebResult.class) == null && method.getAnnotation(javax.xml.ws.ResponseWrapper.class) == null || method.getAnnotation(WebResult.class) != null && method.getAnnotation(WebResult.class).targetNamespace().isEmpty()) {
        field.setTargetNamespace("");
    }
    boolean hasReturnType = false;
    if (!returnType.isAssignableFrom(void.class)) {
        hasReturnType = true;
        String type = getTypeString(method.getGenericReturnType());
        List<Annotation> jaxbAnns = WrapperUtil.getJaxbAnnotations(method);
        field.setType(type);
        field.setJaxbAnnotations(jaxbAnns.toArray(new Annotation[0]));
    }
    fields.add(field);
    final Type[] paramClasses = method.getGenericParameterTypes();
    for (MessagePartInfo mpi : message.getMessageParts()) {
        int idx = hasReturnType ? mpi.getIndex() - 1 : mpi.getIndex();
        if (idx >= 0) {
            String name = mpi.getName().getLocalPart();
            Type t = paramClasses[idx];
            String type = getTypeString(t);
            JavaField jf = new JavaField(name, type, "");
            List<Annotation> jaxbAnns = WrapperUtil.getJaxbAnnotations(method, idx - 1);
            jf.setJaxbAnnotations(jaxbAnns.toArray(new Annotation[0]));
            if (t instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) t;
                Class<?> c = (Class<?>) pt.getRawType();
                if (Holder.class.isAssignableFrom(c)) {
                    Annotation[][] paramAnnotations = method.getParameterAnnotations();
                    WebParam wParam = getWebParamAnnotation(paramAnnotations[idx]);
                    if (wParam != null && !StringUtils.isEmpty(wParam.targetNamespace())) {
                        jf.setTargetNamespace(wParam.targetNamespace());
                    }
                }
            }
            fields.add(jf);
        }
    }
    return fields;
}
Also used : ArrayList(java.util.ArrayList) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Annotation(java.lang.annotation.Annotation) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WebParam(javax.jws.WebParam) JavaField(org.apache.cxf.tools.common.model.JavaField) WrapperBeanClass(org.apache.cxf.tools.java2wsdl.generator.wsdl11.model.WrapperBeanClass) WebResult(javax.jws.WebResult)

Example 9 with JavaField

use of org.apache.cxf.tools.common.model.JavaField in project cxf by apache.

the class WrapperBeanAnnotator method annotate.

public void annotate(final JavaAnnotatable clz) {
    final WrapperBeanClass beanClass;
    if (clz instanceof WrapperBeanClass) {
        beanClass = (WrapperBeanClass) clz;
    } else {
        throw new RuntimeException("WrapperBeanAnnotator expect JavaClass as input");
    }
    JAnnotation xmlRootElement = new JAnnotation(XmlRootElement.class);
    xmlRootElement.addElement(new JAnnotationElement("name", beanClass.getElementName().getLocalPart()));
    xmlRootElement.addElement(new JAnnotationElement("namespace", beanClass.getElementName().getNamespaceURI()));
    JAnnotation xmlAccessorType = new JAnnotation(XmlAccessorType.class);
    xmlAccessorType.addElement(new JAnnotationElement(null, XmlAccessType.FIELD));
    XmlType tp = null;
    if (sourceClass != null) {
        tp = sourceClass.getAnnotation(XmlType.class);
    }
    JAnnotation xmlType = new JAnnotation(XmlType.class);
    if (tp == null) {
        xmlType.addElement(new JAnnotationElement("name", beanClass.getElementName().getLocalPart()));
        xmlType.addElement(new JAnnotationElement("namespace", beanClass.getElementName().getNamespaceURI()));
    } else {
        if (!"##default".equals(tp.name())) {
            xmlType.addElement(new JAnnotationElement("name", tp.name()));
        }
        if (!"##default".equals(tp.namespace())) {
            xmlType.addElement(new JAnnotationElement("namespace", tp.namespace()));
        }
        if (!StringUtils.isEmpty(tp.factoryMethod())) {
            xmlType.addElement(new JAnnotationElement("factoryMethod", tp.factoryMethod()));
        }
        if (tp.propOrder().length != 1 || !StringUtils.isEmpty(tp.propOrder()[0])) {
            xmlType.addElement(new JAnnotationElement("propOrder", tp.propOrder()));
        }
    }
    List<String> props = new ArrayList<>();
    for (JavaField f : beanClass.getFields()) {
        props.add(f.getParaName());
    }
    if (props.size() > 1) {
        xmlType.addElement(new JAnnotationElement("propOrder", props));
    }
    // Revisit: why annotation is string?
    beanClass.addAnnotation(xmlRootElement);
    beanClass.addAnnotation(xmlAccessorType);
    beanClass.addAnnotation(xmlType);
}
Also used : JavaField(org.apache.cxf.tools.common.model.JavaField) JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) JAnnotationElement(org.apache.cxf.tools.common.model.JAnnotationElement) ArrayList(java.util.ArrayList) WrapperBeanClass(org.apache.cxf.tools.java2wsdl.generator.wsdl11.model.WrapperBeanClass) XmlType(javax.xml.bind.annotation.XmlType)

Example 10 with JavaField

use of org.apache.cxf.tools.common.model.JavaField in project cxf by apache.

the class FaultBean method buildBeanFields.

private void buildBeanFields(final Class<?> exceptionClass, final JavaClass jClass) {
    Map<String, JavaField> fields = new TreeMap<>();
    for (Method method : exceptionClass.getMethods()) {
        if (isIncludedGetter(method)) {
            JavaField field = new JavaField(getFieldName(method), method.getReturnType().getName(), "");
            field.setOwner(jClass);
            fields.put(field.getName(), field);
        }
    }
    for (Map.Entry<String, JavaField> ent : fields.entrySet()) {
        jClass.addField(ent.getValue());
        jClass.appendGetter(ent.getValue());
        jClass.appendSetter(ent.getValue());
    }
}
Also used : JavaField(org.apache.cxf.tools.common.model.JavaField) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

JavaField (org.apache.cxf.tools.common.model.JavaField)14 Test (org.junit.Test)6 OperationInfo (org.apache.cxf.service.model.OperationInfo)4 WrapperBeanClass (org.apache.cxf.tools.java2wsdl.generator.wsdl11.model.WrapperBeanClass)4 Annotation (java.lang.annotation.Annotation)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)3 JAnnotation (org.apache.cxf.tools.common.model.JAnnotation)3 Type (java.lang.reflect.Type)2 WebParam (javax.jws.WebParam)2 QName (javax.xml.namespace.QName)2 MessageInfo (org.apache.cxf.service.model.MessageInfo)2 JAnnotationElement (org.apache.cxf.tools.common.model.JAnnotationElement)2 JavaClass (org.apache.cxf.tools.common.model.JavaClass)2 JavaExceptionClass (org.apache.cxf.tools.common.model.JavaExceptionClass)2 JavaMethod (org.apache.cxf.tools.common.model.JavaMethod)2 JavaModel (org.apache.cxf.tools.common.model.JavaModel)2 GreeterArray (org.apache.cxf.tools.fortest.withannotation.doc.GreeterArray)2 ParameterizedType (java.lang.reflect.ParameterizedType)1