lexicore
lexicore

Reputation: 43651

Is there a way to "alias" one class in Java for another?

I have the following API compatibility problem and looking for the ways to solve it.

TL;DR Is there a way to create an "alias" for a class in Java? I.e. some trick to make com.acme.foo.SomeEnum to be an alias for com.acme.bar.SomeEnum?

Long story. (I'm anonimizing it a bit to avoid fingerpointing.)

I'm working with a powerful Java tool which also supports plugins. There's no strictly defined public API (in a sense of what you can and can't touch), just usual private/protected/package/public classes, methods and fields. There are defined extension points (like, extend com.acme.plugin.Plugin class), but then you'll have access to a wide area of the tool's internals.

In the recent minor version update (like, 1.2.3 -> 1.2.4) tool developers have moved one enum class to another package - com.acme.foo.SomeEnum became com.acme.bar.SomeEnum. I think it was thought just as a trivial refactoring, not as a serios restructuring.

This class, however, seems to have been used by a number of plugins. The result is that these plugins are now incompatible with the latest version. Most of the plugins are quite useful, but not actively maintained. People wrote them years ago - and they just worked over the years, with dozens of the version updates. So this has a potential of negative implact on the tool's plugin ecosystem.


My question is, if there's some way in Java to create an "alias" com.acme.foo.SomeEnum for com.acme.bar.SomeEnum? This would allow old plugins to continute working with the new version of the tool.

Some classloader trick? In JavaScript that would've been trivial to shim, but in Java?

Why I am asking this. I'm an author of the Maven plugin which wraps the tool in question. So I could easily add my sugar to this coffee, like classloaders and so on. If there is a technical way to make this work, I'd be in position to save most of the tool's plugins ecosystem - at least for Maven users.

I've contacted the tool's vendor on this, but not sure of the success.

Just to make it clear - I am not the vendor of The Tool in question. I (a) write plugins for The Tools (and have no big trouble updating my plugins) and (b) am an author of the the-tool-maven-plugin which allows execution The Tool in the Maven builds. I also consult a lot on The Tool and care about its ecosystem (there are a lot of very useful plugins).


Update

End of story: developers of The Tool took my points into account, and decided to revert the change. Kudos for that!

Upvotes: 14

Views: 2965

Answers (1)

EJK
EJK

Reputation: 12527

I suggest using jarjar. This is a tool that will allow you to repackage classes in a library. You can run this against the latest version of the tool and move the SomeEnum class to a package that does not conflict with the plugins.

The Getting Started doc has an example, with jaxen.jar, that looks to be relevant to your situation.

Upvotes: 4

Related Questions