tags:

views:

133

answers:

5

This is for an Android application but I'm broadening the question to Java as I don't know how this is usually implemented.

Assuming you have a project that targets a specific SDK version. A new release of the SDK is backward incompatible and requires changing three lines in one class.

How is this managed in Java without duplicating any code(or by duplicating the least amount)? I don't want to create two projects for only 3 lines that are different.

What I'm trying to achieve in the end is a single executable that'll work for both versions. In C/C++, you'd have a #define based on the version. How do I achieve the same thing in Java?

Edit: after reading the comments about the #define, I realized there were two issues I was merging into one:

  • So first issue is, how do I not duplicate code ? What construct is there that is the equivalent of a #define in C.
  • The second one is: is it possible to bundle everything in the same executable? (this is less of a concern as the first one).
A: 

Possibly a similar situation, I had a method which wasn't available in the previous version of the JDK but if it was there I wanted to call it, I didn't want to force people to use the more recent version though. I used reflection to look for the method, if it was there I called it, if it wasn't I didn't.

Pretty hacky but might give you what you want.

Robin
A: 

Addressing Java in general, I see two primary approaches.

1). Refactor the specific code to its own library. Have different versions of that library. Effectively your app is creating an abstaction above the different SDKs. Heavyweight for 3 lines of code, but perhaps quite reasonable for larger scale problems.

2). Injection using annotation. Write your own annotation processor to manage the appropriate injection. More work, but maybe more fun.

djna
+2  A: 

I would isolate the code that needs to be different in a separate class (or multiple classes if necessary), and include / exclude them when building the project for the different versions.

So i would have like src/java/org/myproj/Foo.java which is the common stuff, and then oldversion/java/org/myproj/Bar.java and newversion/java/org/myproj/Bar.java which is the different implementations of the class that uses changed api.

Then I either compile "src/java and oldversion/java" or "src/java and newversion/java".

Rasmus Kaj
Ok, and how to I do this so that I have a single executable instead of 2?
JRL
In android it's an APK, but for all practical purpose we can view it as a jar. So once I have my two class files, how do I create a single JAR? Do I have to use reflection in the code to load one or the other? Do I specify something on the command line?
JRL
I assumed distributing two versions of the app would be ok, and the point was avoiding having to fork the source. If both versions needs to be included in the same runnable, you either need to explicitly get the right versions when instantiating the class (here, the classes should have different names, but implement a common interface), or do some trick with the class loader.
Rasmus Kaj
(removed my previous answer as I had misunderstood your question).
Rasmus Kaj
(By the way, using #define in C/C++ would also mean you have to distribute different versions).
Rasmus Kaj
You're right, see my edit in my question.
JRL
+1  A: 

It depends heavily on the incompatibility. If it is simply behavior, you can check the java.version system property and branch the code accordingly (for three lines, something as simple as an if statement).

If, however, it is a lack of a class or something similar that will throw an error when the class is loaded or when the code gets closer to execution (not necessarily something you can void reasonably by checking before calling), then the solution gets a lot harder. The notion of having a separate version is the cleanest from a code point of view, but it does mean you have to distribute two versions.

Another solution is reflection. Don't reference the class directly, call it via reflection (test for the methods or classes to determine what environment you are currently running in and execute the methods). This is probably the "official" approach in that reflection exists to deal with classes that you don't have or don't know you will have at compile time. It is just being applied to libraries within the JDK. It gets very ugly very fast, however. For three lines of code, it's ok, but doing anything extensive is going to get bad.

The last thing I can think of is to write common denominator code - that is code that gets the job done in both, finding another way to do it that doesn't trigger the problematic class or method.

Yishai
It's indeed a case that'll throw an error if attempting to load the class.
JRL
Reflection was what was chosen as a solution in the end.
JRL
A: 

Separate changing code in different classes with the same interface. Place classes in the same jar. Use factory design pattern to instantiate one or another class depending on SDK version.

avro