Skip to main content


Refactoring Java Legacy Deprecated API


There are certain java code refactoring in the long-running applications, that are due to deprecated legacy APIs, that needs to be replaced with latest APIs/libraries.

For example, when we upgrade AEM server from 6.2 to 6.3, Apache Sling Commons JSON libraries are deprecated and we may need to replace its references with newer Gson or org.json library API calls.

These scenarios, involve a full-fledged code-refactoring, touching all layers of classes, to remove the references of deprecated API and replace it with the new one.


Such problems can be handled in a more elegant way, with a new pattern of code-refactoring, as detailed below.


  1. List down all the deprecated API references across the projects.
  2. Create a new set of API classes, with the same class names and method names, but with a new package structure- mostly as a separate common package in the project.
  3. Change all the import statements of the deprecated classes, to the newly created package in the commons package.
  4. In the new library created, write your logic referencing to the new API. In few cases, it would be a direct call to the new library, but in few cases, we may need to deal with some conversions between the classes.


  1. To replace references of “” classes.
  1. Create a new util package. 
  2. Create methods with same name as in deprecated library. 
  3. Change Import statements in the existing code base, removing apache sling commons class and importing the newly added utility class. This would be the only change in the application classes.





  1. Ease of coding
  2. No duplication of code across the project.
  3. More elegant
  4. Ease of review
  5. Ease of unit testing
  6. Ease of debugging/maintenance
  7. This new API can be built as a separate common jar file and imported to other projects also.


Due to time constraint, it may not be possible to collect all the reference at a single go.  This can be addressed if we have the common new library open for adding new methods, as and when we come across the old references.

Alternatively, we can go with a phased approach for refactoring to address this challenge. In this, we will be anyways creating a new library in the first phase, and in the later, will be adding new classes and methods as required and identified.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Saraswathy Kalyani

More from this Author

Follow Us