[英]Add import using code model
我正在尝试使用代码模型在我的代码中导入一个类。 这是我的代码。
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()
:
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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.