Dealing with jar package structure changes

517 views Asked by At

I'm in a bit of a predicament and I'm looking for some help on how to solve it. I have some source code that imports various classes from a jar file created from idls. Now I've been given a new version of this idl jar that has a different package structure which breaks all of my old import statements. I can't modify the import statements so I'm trying to see if there is a way around this. Any ideas?

Basically I'm being asked to make the current source code work with both the new and old versions of the jar, preferably without modifying the code. I don't think it's possible but I'm hoping I'm wrong.

3

There are 3 answers

1
Vivien Barousse On BEST ANSWER

Here is one thing you could do: recreate all the old classes and make them match with the new ones. For example, if you had a.Foo, and it is now b.foo, you can have:

package a;
public class Foo {
    b.Foo foo;
    public void method() {
        foo.method();
    }
}

package b;
public class Foo {
    public void method() {
        // Your new code...
    }
}

This is a bit of a pain, but I'm afraid it is the price to pay to have backwards compatibility.

Another solution, if you class structure allows it, would be o have a.Foo inherit b.foo, so you don't have to create all the delegate methods. But depending on your project that might not work.

Hope this helps.

0
Ernesto Campohermoso On

No there is no way for do that. But if you want to change the package name in bulk you can use the command sed of linux.

By example if your old package is com.patito and you want to change it to org.duck you can do something like:

 sed 's/com.patito/org.duck/g' *.java
0
Eric B. On

It all depends on how badly the structure change is. If new classes (different functionality) have replaced the old classes but maintain the same name, you're pretty much toast. If the new classes simply have new names/pkgs then you can create a wrapper for all the original imports.

For example, if the original class was:

package my.package;
public class OldClassName{
  public void getSomeData(){}
}

But it has now changed to:

package my.new.package;
public class NewClassName{
  public void getSomeData(){}
}

You can create a wrapper to the original class:

package my.package;
import my.new.package;
public class OldClassName extends NewClassName{
}

Caveats:

  1. This is a royal pain if you have a lot of classes
  2. You will be limited to classes that are not final. If any original imports are final classes, you're toast, and the best you can do is composition (which means even more work) and a lot of delegation.

If you have a lot of classes, you could always write yourself a small utility to generate the wrappers for you...

Once done, you should be able to drop your new wrapper jar alongside the new lib, and things should work.