Cannot find exception classes in CodeModel in XJC plugin

131 Views Asked by At

I'm generating custom Java code from a WSDL provided by PeopleSoft. I've written several XJC plugins to try to clean up the generated code to be easier to use – adding interfaces and custom methods, etc.

However, I'd like to add a common interface to generated Exception classes, but inside my plugin, the classes don't seem to exist.

A generated Exception class looks like this:

public class M286565V1 extends Exception

They all have the same naming pattern (M followed by a string of digits, ending in V1) and they all extend java.lang.Exception. However, they all also have a common method, getFaultInfo(), that I want to add to an interface.

In my plugin, I loop through all the classes provided by Outline.getClasses() and check each one if they extend java.lang.Exception.

for ( ClassOutline classOutline : outline.getClasses() ) {
    final JDefinedClass implClass = classOutline.implClass;

    JClass superClass = implClass._extends();
    if ( superClass != null && superClass.fullName().equals( "java.lang.Exception" ) ) {
        //This class is an Exception class
    }
}

This fails to locate any Exception classes.

I've also tried matching by name:

for ( ClassOutline classOutline : outline.getClasses() ) {
    final JDefinedClass implClass = classOutline.implClass;

    //Try to capture exception classes by name
    //Exception classes start with an M, followed by a number
    final String className = implClass.name();
    LOG.debug( "Checking class {}", className );
    if ( className.startsWith( "M" ) && Character.isDigit( className.charAt( 1 ) ) ) {
        //This class is an Exception class
    }
}

This fails as well, and the logging message ("Checking class…") does not print any of the Exception classes that are ultimately generated.

I've also tried moving this code out of the run() method and into the postProcessModel() method of my plugin, hoping that the code being executed later in the process would help. This failed as well.

The code executes as expected, so I'm confident my plugin configuration is correct. Also, I've captured and modified other classes in this same way. There just doesn't appear to be any Exceptions in the model at runtime. However, the Exception classes are correctly generated every time.

Is there any way to capture these classes from an XJC plugin at runtime?

Thanks in advance for any help you can provide.

1

There are 1 best solutions below

1
On

Consider using JAXB2 Basics Inheritance plugin for the task. (Disclaimer: I'm the author.)

You should actually be getting classes in the run method of your plugin. The postProcessModell is not the right place, it is too early, it is called after the model is loaded to postprocess it.

I'd just recommend to debug it. You can execute XJC from a test case without big problems, see this test for instance:

https://github.com/highsource/jsonix-schema-compiler/blob/master/compiler/src/test/java/org/hisrc/jsonix/xjc/plugin/tests/JsonixPluginTest.java

Hope this helps.