JCodeModel 引用未导入

JCodeModel ref not imported

我正在尝试创建一个扩展通用摘要 class (MappingObject.class) 的 class (MoMoTest.class)。一切看起来都不错,除了 JCodeModel 没有导入缩小的 class (MoTest.class),尽管我用 codeModel.ref:

创建了它的 JClass

MappingObject.class:

package test;

public abstract class MappingObject<T> {

    protected T dataObject;

    public MappingObject( T dataObject ) {
        this.dataObject = dataObject;
    }

    public abstract T getDataObject();

    public abstract String getStandardFormat();
}

MoTest.class:

package test;

public class MoTest {
}

MappingObjectCreator.class:

package test;

import com.sun.codemodel.*;
import java.io.File;
import java.io.IOException;

public class MappingObjectCreator {

    public JDefinedClass getMappingObject(JCodeModel codeModel, JPackage jPackage, Class<?> clazz)     throws JClassAlreadyExistsException {
        JClass ref = codeModel.ref(clazz); // Not imported in MoMoTest.class
        JDefinedClass definedClass = jPackage._class("Mo" + ref.name());
        JClass superClass = codeModel.ref(MappingObject.class).narrow(ref);
        definedClass._extends(superClass);
        JFieldRef dataObject = JExpr.ref("dataObject");

        JMethod constructor = definedClass.constructor(JMod.PUBLIC);
        JVar param = constructor.param(ref, ref.name());
        constructor.body().invoke("super").arg(param);

        JMethod getDataObject = definedClass.method(JMod.PUBLIC, ref, "getDataObject");
        getDataObject.annotate(codeModel.ref(Override.class));
        getDataObject.body()._return(dataObject);

        JMethod getStandardFormat = definedClass.method(JMod.PUBLIC, String.class, "getStandardFormat");
        getStandardFormat.annotate(codeModel.ref(Override.class));
        getStandardFormat.body()._return(dataObject.invoke("toString"));

        return definedClass;
    }

    public void getMappingObject(Class clazz, String path) throws JClassAlreadyExistsException, IOException {
        JCodeModel codeModel = new JCodeModel();
        JPackage jPackage = codeModel._package(clazz.getPackage().getName());

        getMappingObject(codeModel, jPackage, clazz);
        codeModel.build(new File(path));
    }

    public static void main(String[] args) throws IOException, JClassAlreadyExistsException {
        new MappingObjectCreator().getMappingObject(MoTest.class, "src/main/java");
    }
}

结果(MoMoTest.class):

package test;


public class MoMoTest
    extends MappingObject<test.MoTest>
{


    public MoMoTest(test.MoTest MoTest) {
        super(MoTest);
    }

    @Override
    public test.MoTest getDataObject() {
        return dataObject;
    }

    @Override
    public String getStandardFormat() {
        return dataObject.toString();
    }

}

那么为什么 Motest.class 没有在 MomoTest.class 中导入?

将生成的 class (MoMoTest.class) 的构造函数的参数名修改为首字母小写后,它以某种方式起作用:

MappingObjectCreator.class:

        JMethod constructor = definedClass.constructor(JMod.PUBLIC);
        char[] refName = ref.name().toCharArray();
        refName[0] = Character.toLowerCase(refName[0]);
        JVar param = constructor.param(ref, new String(refName));
        constructor.body().invoke("super").arg(param);

结果:


package test;


public class MoMoTest
    extends MappingObject<MoTest>
{


    public MoMoTest(MoTest moTest) {
        super(moTest);
    }

    @Override
    public MoTest getDataObject() {
        return dataObject;
    }

    @Override
    public String getStandardFormat() {
        return dataObject.toString();
    }

}