Search code examples
javajcodemodel

JCodeModel ref not imported


I'm trying to create a class (MoMoTest.class) which extends a generic abstract class (MappingObject.class). Everything looks good, except that JCodeModel doesn't import the narrowed class (MoTest.class), although I created a JClass of it with codeModel.ref:

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");
    }
}

Result (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();
    }

}

So why is Motest.class not imported in MomoTest.class ?


Solution

  • After modifying the parametername of the constructor of the generated class (MoMoTest.class) to first-letter-lower-case it somehow works:

    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);
    

    Result:

    
    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();
        }
    
    }