Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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!

like image 603
lexicore Avatar asked Dec 11 '14 22:12

lexicore


People also ask

Does Java allow aliasing?

The Java language provides no aliasing mechanism.

Can two classes have same name in Java?

Note that you can't import two classes with the same name in two different packages. The classes Integer and String belongs to the package java. lang but they don't need to be imported as the java.

What is alias in class?

A class alias definition is a mapping of one or more old class names to a new name. Creating alias definitions enables you to rename classes while maintaining backward compatibility with code and MAT-files that use one or more older class names.

Is it possible that one class belongs to two packages at once?

No, it cannot be. But however there can be classes with same name in different packages, but no two similarly named classes in the same package. Show activity on this post. You cannot put package declaration twice in the same class.


1 Answers

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.

like image 78
EJK Avatar answered Oct 06 '22 01:10

EJK