我试图使用代码模型导入代码中的一个类。这是我的密码。
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()
}
}除非使用,否则导入不会来。我怎样才能使这个进口。
发布于 2015-06-14 14:40:30
要将Mapper添加为导入,需要调用invoke() / staticInvoke()而不是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");https://stackoverflow.com/questions/30800034
复制相似问题