Saturday 15 May 2010

java - Best practice for developing under multiple JDKs -


I have a library which I am currently developing with target compatibility for JDK 6. I would like to take advantage of the Java 8 feature, such as the Lambadas code and API Java 8 will greatly benefit.

I would like to offer library versions for Java 6 and Java 8, for example, where a certain part is only available in Java version 8. / P>

Now there are some core classes that can be their separate source version (Legacy Java version 6 and newer Java version 8).

What is the best practice to do this? In Objective-C or C ++, I consider using a preprocessor instruction.

I see some options:

  1. Different versions of your library Package one, one for JDK 6 and one JDK 8. Users are responsible for incorporating the right one for their application. This would be an interesting construct to do if you are using Maven, then I think that you will only use 3 projects; A "Commons" project with all common sections, a "JDK 6" project on which the Commons project has been placed as a dependency and is a Commons project as a dependency on the JDK6 Jar and then the "JDK 8" project, and JDK 8 makes jars. An attractive bit of this approach is that it clearly works, and you do not have any tough maintenance.
  2. The JDK 6 and JDK 8 versions both packages in the same jar, and "runtime" -type class which is responsible for making a proper version of the objects of your library on runtime, running on Depends on JDK. For example:

      public abstract class runtime {Private runtime runtime instance; Public stable runtime getInstance () {if (example == blank) {try (if (system.getProperty ("java.version"). BeginWith ("8") example = Class.forName ("com.yourcompany.libname. Runtime.Runtime8 ") newInstance (). Else instance = class.ForName (" com.yourcompany.libname.runtime.Runtime6 ") NewInstance ();} hold (exception E) {take new runtime exception (" do not make runtime };}} Return example;} public abstract framework getFramework ();}   

    To use this approach, you need to load the initial "runtime" object Use only reflection , And then you can use native Java for everything else, for example:

      framework frame = runtime.justinstance (); GetFramework (); ModelObject1M = Framework .Newmodd object1 (); // ...   

    Just make sure your runtime 6 class does not mention the JDK 8 class, even so That is also transit through imports. The main drawback in this approach is a bit tidbit with time. Aking is.

    I think what you are doing is interesting, and I'm very curious to hear how you choose to post it to us to complete it!

No comments:

Post a Comment