Search in sources :

Example 1 with JavaClass

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

the class BeanGenerator method generateAndCompile.

public void generateAndCompile(Collection<JavaClass> wrapperClasses, File dir) {
    VelocityGenerator generator = new VelocityGenerator(false);
    generator.setBaseDir(dir.toString());
    List<File> generatedFiles = new ArrayList<>();
    try {
        for (JavaClass wrapperClass : wrapperClasses) {
            generator.setCommonAttributes();
            generator.setAttributes("bean", wrapperClass);
            File file = generator.parseOutputName(wrapperClass.getPackageName(), wrapperClass.getName());
            generatedFiles.add(file);
            generator.doWrite(TEMPLATE, new FileWriterUtil(file.getParent(), getOutputStreamCreator()).getWriter(file, (String) getToolContext().get(ToolConstants.CFG_ENCODING)));
            generator.clearAttributes();
        }
        // compile the classes
        Compiler compiler = (Compiler) getToolContext().get(ToolConstants.COMPILER);
        if (compiler == null) {
            compiler = new Compiler();
        }
        compiler.setOutputDir(compileToDir);
        List<String> files = new ArrayList<>(generatedFiles.size());
        for (File file : generatedFiles) {
            files.add(file.getAbsolutePath());
        }
        compiler.compileFiles(files.toArray(new String[0]));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Compiler(org.apache.cxf.common.util.Compiler) VelocityGenerator(org.apache.cxf.tools.common.VelocityGenerator) JavaClass(org.apache.cxf.tools.common.model.JavaClass) FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) ArrayList(java.util.ArrayList) File(java.io.File)

Example 2 with JavaClass

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

the class BeanGenerator method generate.

public File generate(final File sourcedir) {
    File dir = getOutputBase();
    if (dir == null) {
        dir = sourcedir;
    }
    if (dir == null) {
        dir = new File("./");
    }
    Collection<JavaClass> wrapperClasses = generateBeanClasses(getServiceModel());
    if (!wrapperClasses.isEmpty()) {
        generateAndCompile(wrapperClasses, dir);
    }
    return dir;
}
Also used : JavaClass(org.apache.cxf.tools.common.model.JavaClass) File(java.io.File)

Example 3 with JavaClass

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

the class RequestWrapperTest method testNoAnnotationNoClass.

@Test
public void testNoAnnotationNoClass() throws Exception {
    String pkgName = "org.apache.cxf.tools.fortest.classnoanno.docwrapped";
    Class<?> testingClass = Class.forName(pkgName + ".Stock");
    OperationInfo opInfo = getOperation(testingClass, "getPrice");
    Wrapper wrapper = new RequestWrapper();
    wrapper.setOperationInfo(opInfo);
    assertTrue(wrapper.isWrapperAbsent());
    assertTrue(wrapper.isToDifferentPackage());
    assertFalse(wrapper.isWrapperBeanClassNotExist());
    assertEquals(pkgName + ".jaxws", wrapper.getJavaClass().getPackageName());
    assertEquals("GetPrice", wrapper.getJavaClass().getName());
    JavaClass jClass = wrapper.buildWrapperBeanClass();
    assertNotNull(jClass);
    List<JavaField> jFields = jClass.getFields();
    assertEquals(1, jFields.size());
    assertEquals("arg0", jFields.get(0).getName());
    assertEquals("java.lang.String", jFields.get(0).getClassName());
    List<JavaMethod> jMethods = jClass.getMethods();
    assertEquals(2, jMethods.size());
    JavaMethod jMethod = jMethods.get(0);
    assertEquals("getArg0", jMethod.getName());
    assertTrue(jMethod.getParameterListWithoutAnnotation().isEmpty());
    jMethod = jMethods.get(1);
    assertEquals("setArg0", jMethod.getName());
    assertEquals(1, jMethod.getParameterListWithoutAnnotation().size());
    assertEquals("java.lang.String newArg0", jMethod.getParameterListWithoutAnnotation().get(0));
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) JavaField(org.apache.cxf.tools.common.model.JavaField) JavaClass(org.apache.cxf.tools.common.model.JavaClass) JavaMethod(org.apache.cxf.tools.common.model.JavaMethod) Test(org.junit.Test)

Example 4 with JavaClass

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

the class WrapperTest method testGetWrapperBeanClassFromQName.

@Test
public void testGetWrapperBeanClassFromQName() {
    QName qname = new QName("http://cxf.apache.org", "sayHi");
    Wrapper wrapper = new Wrapper();
    wrapper.setName(qname);
    JavaClass jClass = wrapper.getWrapperBeanClass(qname);
    assertEquals("org.apache.cxf", jClass.getPackageName());
    assertEquals("SayHi", jClass.getName());
    assertEquals("http://cxf.apache.org", jClass.getNamespace());
}
Also used : JavaClass(org.apache.cxf.tools.common.model.JavaClass) QName(javax.xml.namespace.QName) Test(org.junit.Test)

Example 5 with JavaClass

use of org.apache.cxf.tools.common.model.JavaClass 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)

Aggregations

JavaClass (org.apache.cxf.tools.common.model.JavaClass)8 Test (org.junit.Test)4 OperationInfo (org.apache.cxf.service.model.OperationInfo)3 File (java.io.File)2 Method (java.lang.reflect.Method)2 HashSet (java.util.HashSet)2 JavaField (org.apache.cxf.tools.common.model.JavaField)2 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 Compiler (org.apache.cxf.common.util.Compiler)1 VelocityGenerator (org.apache.cxf.tools.common.VelocityGenerator)1 JAnnotation (org.apache.cxf.tools.common.model.JAnnotation)1 JavaMethod (org.apache.cxf.tools.common.model.JavaMethod)1 FaultBean (org.apache.cxf.tools.java2wsdl.processor.internal.jaxws.FaultBean)1 RequestWrapper (org.apache.cxf.tools.java2wsdl.processor.internal.jaxws.RequestWrapper)1 ResponseWrapper (org.apache.cxf.tools.java2wsdl.processor.internal.jaxws.ResponseWrapper)1 FileWriterUtil (org.apache.cxf.tools.util.FileWriterUtil)1