如何将Javassist CtClass导出到文件

如何将Javassist CtClass导出到文件,java,Java,我尝试使用javassist CtClass将动态创建的类导出到文件中。我的代码如下所示: public Class generate() throws NotFoundException, CannotCompileException { ClassPool pool = ClassPool.getDefault(); CtClass targetClass = pool.makeClass("ExampleController"); // ma

我尝试使用javassist CtClass将动态创建的类导出到文件中。我的代码如下所示:

 public Class generate() throws NotFoundException,
        CannotCompileException {

    ClassPool pool = ClassPool.getDefault();
    CtClass targetClass =  pool.makeClass("ExampleController");

    // make class fields
    Iterator i = fields.iterator();
    while (i.hasNext()) {

        ClassFIeldProperty fieldProperty = (ClassFIeldProperty) i.next();
        CtClass fieldStoringType = pool.makeClass(fieldProperty.getStoringType().getName());
        Initializer fieldValue = CtField.Initializer.byNew(pool.makeClass(fieldStoringType.getName()));
        String fieldName = fieldProperty.getName();
        AnnotationsAttribute annotation = fieldProperty.getAnnotationAttribute();

        CtField field = new CtField(fieldStoringType, fieldName, targetClass);
        field.getFieldInfo().addAttribute(annotation);
        targetClass.addField(field,fieldValue);

    }

    // now we have class with some fields

    Class classWithFields = targetClass.toClass();
如何使用类定义将classWithFields导出并保存到文件(ExampleController.java)

   // code likes:
    File sourceFile = new File("/temp/ExampleController.java");
    try (FileWriter writer = new FileWriter(sourceFile)) {
        writer.write(classWithFields.toString());
    }

   // doesnt work - file not appear
请尝试使用以下代码:

            DataOutputStream out = new DataOutputStream(new FileOutputStream("WelcomeController2.java"));
            targetClass.getClassFile().write(out);
并检查目录:

 C:\Users\UserAcccountName\AppData\Roaming\NetBeans\8.0\config\GF_4.0\domain1\config