Hint on retaining backward compatibility

From Eclipsepedia

Jump to: navigation, search

Retaining backward compatibility

Before

You decide to extend a class and create new choosable classes, e.g. more browsers for a SCM-plugin. The old data structure looked like this when you had only one class SCMBrowser:

        <browser>
          <url>http://yahoo.com/</url>
        </browser>

After

Now you decide to add a new NewSCMBrowser, all your SCMBrowsers are extending SCMBrowserBase and your XML suddenly looks like this:

        <browser class="hudson.plugins.foo.NewSCMBrowser">
          <url>http://yahoo.com/</url>
        </browser>

or

        <browser class="hudson.plugins.foo.SCMBrowser">
          <url>http://yahoo.com/</url>
        </browser>

With new jobs, no problem. Old jobs however will probably break. In your SCMBrowserBase class add a method readResolve (see XStream FAQ):

       // compatibility with earlier plugins
        public Object readResolve() {        
            if (!this.getClass().equals(SCMBrowserBase.class)) {
                return this;
            }
            // make sure to return the default SCMBrowser only if we no class is given in config.
            try {
                return new SCMBrowser(url.toExternalForm());
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }