user1578872
user1578872

Reputation: 9018

JAVA refactoring using reflection

I am using a 3rd party API in few Java applications. They have updated few things in the latest version. We will have to update to the latest version and it needs corresponding changes from our code. The changes are,

1) The interface and the abstract class name which we used to implement/extend has been changed. Also, the method names has been changed. These are all just the name changes.

2) Need to annotate the class which implements these interfaces with @Service

3) Then need to add some new Java file and a property file.

4) We also have the abstract class which implements the 3rd part abstract class and then there are many concrete classes. So, few methods from the 3rd party abstract class is been overridden in our base abstract class which extends the base abstract class and few methods are there in the concrete abstract class.

I can do the refactoring through Eclipse IDE, but we dont prefer this. I like this to be completely automated like running a script.

I tried with Java reflection to find all the concrete class of an Abstract class and rename the methods. Still, it looks risky.

Is there any other better approach?

Upvotes: 0

Views: 323

Answers (3)

Neville Kuyt
Neville Kuyt

Reputation: 29619

I remember reading somewhere that a programmer is someone who would rather spend 12 hours writing a script to automate a manual task than to spend 20 minutes actually doing that task.

I understand why you want to automate this - the API you're using is making life hard for its clients by renaming things. It's unusual for APIs to break compatibility with naming only - are you sure it's as simple as that?

My strong recommendation is to just bite the bullet and manually refactor. It will almost certainly take less time than automating the process, you'll identify further opportunities to improve your own application's design, and it's unlikely you will ever need to use the refactoring script again.

Upvotes: 2

JnRouvignac
JnRouvignac

Reputation: 787

It depends how much code you need to change, how long it takes to do each step and how many times you repeat the same refactoring. If it is only a few hundred classes and/or simpler refactorings like rename class/interface can do most of the work, then do it by hand.

Otherwise if you really want to, you can try to write rules in a tool like AutoRefactor: https://github.com/JnRouvignac/AutoRefactor

Disclaimer: I am the author of AutoRefactor.

Upvotes: 2

user6874700
user6874700

Reputation:

Unfortunately, I do not now the exact details of you situation. I can point some principles which can simplify life in future according to my experience.

Shortly, if you are using any 3rd party API, try to minimize it's propagation into your code. Hide the 3rd party code behind your own abstractions (interfaces) using patterns like Adapter, Facade etc.

So, in case the 3rd party code changes, you will make changes only in one place. This approach gives you extra freedom: if you'll decide to use another 3rd party API, it will be simple, because the major peace of your code will not touched. Also it is useful while testing: you can mock actual 3rd party functionality.

For example, suppose your project need to have persisting storage. So you can start from declaring interface like this:

interface IStorage {
    void save(Model m);
    Model load(int id);
}

This will allow you:

  • Make decision about storage provider (may be it will be MySQL or MongoDB or simply just XML file on disk) more later.
  • Easily substitute one 3rd party API by another (for example change from file storage to DB).
  • Test you business logic easily by mocking this interface instead of use real storage.
  • Speed up development in case some modules (which another developers have to do) require working storage (they will just use IStorage interface as if it is already implemented).

Upvotes: 1

Related Questions