문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top