R. Oosterholt
R. Oosterholt

Reputation: 8080

How to migrate Java code to a more recent jdk (e.g. 1.8)

What is the best way to migrate Java code of an older jdk (1.5) to a more recent Java version (1.8) to provide from its new features and improvements. We have a large Java jdk 5 code base and want to migrate to jdk 8.

There are a lot of compiler warnings and hints (e.g. diamond operator, multicatch, unnecessary (un)boxing, etc) which will improve the performance, code readability, etc.

We are using Netbeans IDE. Are there any plugins which we can use or are there migration scripts?

Upvotes: 6

Views: 7398

Answers (3)

R. Oosterholt
R. Oosterholt

Reputation: 8080

Found what I was looking for: Netbeans offers Inspect and Transform.

This can be used to transform your complete code base (or parts of it) with a configuration of changes.

This is how it works:

  1. select your project
  2. click Refactor menu
  3. click Inspect and Transform menu item
  4. select configuration and configure it using the Manage button
  5. choose your desired transformations (e.g. Can Use Diamond, Join catch sections using multicatch, unnecessary boxing, etc.)
  6. click Inspect
  7. Review proposed refactoring suggestions and click Do Refactoring

Your complete code base is refactored and uses your selected new features and new idioms.

IntelliJ IDEA has a similar feature. See Analyze > Inspect Code ...

In Eclipse this is called clean up in code style (configuration) or source (menu).

Upvotes: 1

Makoto
Makoto

Reputation: 106440

The likelihood of your code being incompatible with Java 8 is slim, since Java has taken great strides to ensure backwards compatibility with all previous versions.

The issues that you'll likely run into lie much deeper, likely in implementations of collections or methods who have changed over the years.

If you don't have a test suite that covers the critical paths of your code, start there. You'll need that test suite to ensure that the migration hasn't horribly broken anything.

Next, peruse the compatibility guides for Java 1.7 and Java 1.8 and be sure that nothing that you're using in particular is impacted by those changes.

Lastly, the code cleanup piece can be tackled, but it shouldn't be addressed right now. The only thing you need to concern yourself with is to get the platform running on the new version of Java. As you work in the code base, discipline yourself and the team to use the newer Java idioms, such as the diamond notation, and try-with-resources where applicable.

Upvotes: 5

Kartik Pandya
Kartik Pandya

Reputation: 2898

Unfortunately, there are no magical ways to achieve what you are asking, but here are a few pointers that can make it easier for you to migrate the code to JDK 1.7 (note that JDK 1.8 has been out for some time now, and 1.7 is already out of support officially by Oracle):

  • Use checkstyle or a similar plugin in Eclipse to find the problems

  • Build your project with JDK compiler level 1.7 in Eclipse; warnings given by Eclipse are much more user friendly than the warnings printed on console by command line compiler

  • In theory, JDK 1.7 is backwards compatible with 1.5. The only exceptions are assert and enum keywords. If you used these words as user defined type/method names, you'll get a compilation error. So for most part, you can get right down to warnings. If push comes to shove, you can choose to ignore many of these warnings (of course, only if you must)

Upvotes: 1

Related Questions