Java 使用代码模型添加导入

Java 使用代码模型添加导入,java,sun-codemodel,jcodemodel,Java,Sun Codemodel,Jcodemodel,我正在尝试使用代码模型导入代码中的类。 这是我的密码 JCodeModel model = new JCodeModel(); JClass mapper = model.directClass("com.another.Mapper"); JDefinedClass dc = model._class("com.example.Something"); JMethod method = dc.method(JMod.PUBLIC | JMod.STATIC, Void.TYPE,

我正在尝试使用代码模型导入代码中的类。 这是我的密码

JCodeModel model = new JCodeModel();
JClass mapper = model.directClass("com.another.Mapper");
JDefinedClass dc = model._class("com.example.Something");
JMethod method = dc.method(JMod.PUBLIC | JMod.STATIC, Void.TYPE,
        "testMethod");
JBlock executerBlock = method.body();
    executerBlock.directStatement("Mapper.get()");
File file = new File("./src");
file.mkdirs();
model.build(file);
现在我得到了下面的结果

package com.example;
public class Something {
    public static void testMethod() {
    Mapper.get()
    }
}
但实际上我需要的是

package com.example;
import com.another.Mapper;
public class Something {
    public static void testMethod() {
    Mapper.get()
    }
}

除非使用,否则导入不会出现。如何才能进行此导入。

对于要作为导入添加的
Mapper
,您需要调用
invoke()
/
staticInvoke()
,而不是
directStatement()


如果未使用导入,则它不在.class中。下面是创建正确源代码的工具。看起来像一个功能。为什么要用其他方法?@Jayan我正在使用JBlock.directStatement()方法。我正在使用这个类(有问题的更新)。
JClass mapper = model.directClass("com.another.Mapper");
JDefinedClass dc = model._class("com.example.Something");
JMethod method = dc.method(JMod.PUBLIC | JMod.STATIC, Void.TYPE, "testMethod");
JBlock executerBlock = method.body();
executerBlock.staticInvoke(mapper, "get");