Hi.
> I didn't find any way to specify that a generated class implemented some
> existing interface, so I wrote a simple plugin to do it. Any comments on
> existing ways to do this are welcome, as are comments on the code. As
> small as it is, I'm sure there are several things I haven't considered,
> or are just mistaken about.
>
> The plugin can be found at http://xml.w-wins.com/xjc-plugins/interfaces/
I have -Xinheritance plug-in for JAXB, implemented similar to yours. ;)
Really nice to see people writing plugins. ;)
Would you like to host it on jaxb2-commons.dev.java.net?
Few comments on code.
...
classOutline.implClass._implements(getClass().getClassLoader().loadClass(interfaceName));
...
1. Don't do getClass().getClassLoader().loadClass(interfaceName). This will
require you to make the interface class available to XJC during the code
generation time. This will have consequences in the build order and so on.
You can do
classOutline.implClass._implements(codeModel.ref(interfaceName))
instead.
You can get codeModel from outline.
2. I'd recommend to check if the class already implements the interface.
Here are some code snippets from hyperjaxb3 ClassUtils:
public static void _implements(JDefinedClass theClass, JClass theInterface) {
if (!isImplementing(theClass, theInterface))
theClass._implements(theInterface);
}
public static boolean isImplementing(JDefinedClass theClass, JClass
theInterface) {
for (Iterator iterator = theClass._implements(); iterator.hasNext();) {
final JClass implementedInterface = (JClass) iterator.next();
if (theInterface.equals(implementedInterface)) {
return true;
}
}
return false;
}
So I'd do ClassUtils._implements(classOutline.impleClass, myInterface)
throw new InterfacePluginException("Unable to find named interface
'"+interfaceName+"'",cnfe);
3. (well, you won't have any cnfe if you use codeModel.ref(...) but anyway)
Errors should be reported via the errorHandler.
Bye.
/lexi