Sometimes it is just simpler to move to a new implementation class entirely. You keep Just Enoughâ„¢ of the old class around that it can be read from disk... and then you add a `readResolve` method that instantiates and returns the new class
Here is a very old example of doing this for a complex migration: https://github.com/jenkinsci/cloudbees-deployer-plugin/blob/master/src/main/java/org/jenkins/plugins/cloudbees/CloudbeesPublisher.java#L102 On 14 June 2017 at 23:59, Christian McHugh <[email protected]> wrote: > Greetings community, > > Earlier versions of the saltstack plugin were pretty ugly and didn't use a > class for the different options in a dropdown selector, but instead just > parsed the json in the databoundconstructor. Thankfully, this has been > corrected, but now brings up the issue of how to support older configs. > > I have the following code added to readResolve() or > public static class ConverterImpl extends XStream2.PassthruConverter<Sal > tAPIBuilder> { > public ConverterImpl(XStream2 xstream) { super(xstream); } > @Override > protected void callback(SaltAPIBuilder obj, UnmarshallingContext > context) { > > // Support 1.7 and before > if (obj.clientInterfaces != null) { > obj.arguments = obj.arguments.replaceAll(",", " "); > if (obj.clientInterfaces.has("clientInterface")) { > if (obj.clientInterfaces.getString("clientInterface"). > equals("local")) { > obj.clientInterface = new LocalClient(obj.function, > obj.arguments + " " + obj.kwarguments, obj.target, obj.targettype); > ((LocalClient) obj.clientInterface).setJobPollTime(obj > .clientInterfaces.getInt("jobPollTime")); > ((LocalClient) obj.clientInterface).setBlockbuild(obj. > clientInterfaces.getBoolean("blockbuild")); > } else if (obj.clientInterfaces.getString( > "clientInterface").equals("local_batch")) { > obj.clientInterface = new LocalBatchClient(obj. > function, obj.arguments + " " + obj.kwarguments, obj.batchSize, obj.target > , obj.targettype); > } else if (obj.clientInterfaces.getString( > "clientInterface").equals("runner")) { > obj.clientInterface = new RunnerClient(obj.function, > obj.arguments + " " + obj.kwarguments, obj.mods, obj.pillarvalue); > } > } > final XStream xStream = new XStream(new DomDriver()); > final String xmlResult = xStream.toXML(obj).toString(); > System.out.println("!!!!!! Running ConverterImpl\n" + > xmlResult); > > OldDataMonitor.report(context, "1.7.2"); > } > } > } > This code attempts to parse the old format, and creates the appropriate > new classes. In the above, the system.out.println result shows the expected > output, but unfortunately there's a problem. > When viewing a job with an older config, nothing is displayed. When > viewing the manage old data page the following error is shown: > TypeNameVersion > hudson.model.FreeStyleProject job1.7.1 1.7.2 ConversionException: Cannot > construct com.waytta.clientinterface.BasicClient : > com.waytta.clientinterface.BasicClient : Cannot construct > com.waytta.clientinterface.BasicClient : > com.waytta.clientinterface.BasicClient > ---- Debugging information ---- message : Cannot construct > com.waytta.clientinterface.BasicClient : > com.waytta.clientinterface.BasicClient > cause-exception : > com.thoughtworks.xstream.converters.reflection.ObjectAccessException > cause-message : Cannot construct com.waytta.clientinterface.BasicClient : > com.waytta.clientinterface.BasicClient class : > com.waytta.clientinterface.BasicClient > required-type : com.waytta.clientinterface.BasicClient converter-type : > hudson.util.RobustReflectionConverter path : > /project/builders/com.waytta.SaltAPIBuilder/clientInterface > line number : 52 -------------------------------, InstantiationError: null > > Does anyone have a recomendation on how to support this migration and > creating a class from the ConverterImpl? > Thanks :) > > -- > You received this message because you are subscribed to the Google Groups > "Jenkins Developers" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit https://groups.google.com/d/ > msgid/jenkinsci-dev/3825faa2-a769-4a81-9c92-dc0065dea0c8% > 40googlegroups.com > <https://groups.google.com/d/msgid/jenkinsci-dev/3825faa2-a769-4a81-9c92-dc0065dea0c8%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Jenkins Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CA%2BnPnMxVns_8g51nbB-YV3Qs3eFwS7TuCjYbQsVNaX2XO8r36w%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
