Reputation: 3
I am currently working on dynamic java source code creation and I ran into a problem. Everything is working fine so far but not with interfaces.
UMLClass = codemodel._class(JMod.PUBLIC, umlInterface.getName(), ClassType.INTERFACE);
//UMLClass is a JDefinedClass, umlInterface.getName() is just a String
If I try running this code, it creates source code looking like this:
public class Bull {
private final static long bli;
private final static double bla;
abstract void abstractOperation();
}
It doesn't name it interface and I have not figured out, why, because the JType was set to INTERFACE which should create an interface?
Upvotes: 0
Views: 485
Reputation: 955
It's a bug in CodeModel:
JCodeModel._class(fullyqualifiedName, ClassType.INTERFACE) create a concrete class if fullyqualifiedName is in root package. When calling jCodeModel._class(fullyqualifiedName, ClassType.INTERFACE),if fullyqualifiedName does not have a ".", the classtype is set to CLASS.
https://github.com/javaee/jaxb-codemodel/issues/24
So you need to specify the fully qualified name for your interface.
The following does the job:
JCodeModel codeModel = new JCodeModel();
JDefinedClass bull = codeModel._class(JMod.PUBLIC, "com.Bull", ClassType.INTERFACE);
System.out.println("is interface " + bull.isInterface());
codeModel.build(new File("/home/user"));
System.out.println("done!");
BufferedReader br = new BufferedReader(new FileReader("/home/user/com/Bull.java"));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
Upvotes: 2