I am trying to import a class in my code using code model. This is my code.
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);
Now I am getting the following class as result.
package com.example;
public class Something {
public static void testMethod() {
Mapper.get()
}
}
But actually i need is,
package com.example;
import com.another.Mapper;
public class Something {
public static void testMethod() {
Mapper.get()
}
}
The import is not coming unless it use. How can i possible to make this import.
For
Mapper
to be added as an import, you will need to callinvoke()
/staticInvoke()
instead ofdirectStatement()
: