DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to create an API for users who don’t want to depend on your code?

How to create an API for users who don’t want to depend on your code?

Allan Gregersen user avatar by
Allan Gregersen
·
May. 15, 12 · Interview
Like (0)
Save
Tweet
Share
8.56K Views

Join the DZone community and get the full member experience.

Join For Free

First of all, how would you ever end up asking yourself that question? Well, if you’re the implementer of a class reloading system (e.g. www.javeleon.org) then this question makes perfect sense. Most users take advantage of the basic capabilities of Javeleon simply by adding a Java agent to the JVM arguments.

That’s all good; no direct linking is required to gain the class reloading capabilities in e.g. Java SE applications. Other users will also want to add class reloading capabilities to classes loaded by custom class loaders. This is quite tricky since Javeleon requires integration with classloaders to function properly. In essence, Javeleon needs to know which class loader objects should be made reload-aware. A reload-aware class loader will load only reloadable classes meaning that whenever a reload is triggered Javeleon will reload those classes and you’ll see the changed code immediately in the running application. In order to reload all the classes loaded by a custom class loader Javeleon needs a fresh class loader instance during reload, thus a listener approach would come in handy. The following simple API would suffice to communicate with the Javeleon runtime in this case.

public final class JaveleonReloadManager {
	public static void register(ClassLoader loader, JaveleonClassLoaderListener listener) {}
	

	public static void unregister(ClassLoader loader) {}
   
	public static interface JaveleonClassLoaderListener {
        	public ClassLoader createClassLoader();
    	}
}

The above code would make it possible to register and un-register class loaders. When a reload happens Javeleon will ask the listener associated with the registered class loader object to create a new fresh class loader instance. Now, this approach assumes that users link against the Javeleon API code directly, which can be a problem in some projects. Especially, since Javeleon is only a development dependency, thus users don’t want to execute Javeleon-specific code in their production system (for good reasons). One way to avoid executing Javeleon-specific code is to hide behind the powers of reflection.

You could test reflectively that Javeleon is enabled in the running system, simply be trying to load a known Javeleon class. This would make you able to guard all invocations to the Javeleon “API” by a simple “isJaveleonPresent” check. This approach is actually taken in the integration for the NetBeans Platform and such code is present in the official code base of the NetBeans project. This solution is OK, but very clumsy, since you have no compile-time verification. On the other hand providing an official Javeleon API that users can link against is often too intrusive remembering that Javeleon is only a development dependency. This brings us back to the main question - How do you create an API for users who don’t want to depend on your code?

Well, there is (at least) one path that will lead us there. It turns out that some clever bytecode tricks carry us on to a usable solution. The main idea of our approach is simple. We provide a source file containing the classes as shown above with no implementation within any methods. Users then copy this piece of code to an appropriate package of their choice. Now users can link against this code that does nothing if Javeleon is not present in the system.

If for example the JaveleonReloadManager.register method is invoked nothing will happen since this method is empty. Now, this doesn’t sound too promising since no code will be executed. The trick is that when Javeleon is enabled for this project it will inject code into the register and unregister methods at load-time. This will allow users to communicate with Javeleon without having to link with a specific Javeleon API. However, there is still one big problem with this solution!

The interface JaveleonClassLoaderListener is not compatible with any type internally known by Javleon. Of course we could just use reflection to hack around this but then we have just moved the burden to our side of the equation, which is better but by no means optimal. Instead, we define an internal interface inside Javeleon called e.g. InternalJaveleonClassLoaderListener declaring the exact same methods (here only one method). At load time we then add the internal interface as a superinterface of the interface defined in user code like this:

public static interface JaveleonClassLoaderListener extends InternalJaveleonClassLoaderListener {
	ClassLoader createClassLoader();
}

Now, all of a sudden we have a compatible callback instance as obtained by the register method of which we can safely invoke the createClassLoader method on. That’s it! It is as simple as that!


This approach only requires users to define a small amount of “unknown” code in their project that does absolutely nothing if Javeleon is not around. When Javeleon is enabled they can invoke methods directly into an “API” which is then linked together at load-time so that a communication channel is established without the need to rely on reflective code.

API

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • HTTP vs Messaging for Microservices Communications
  • Rust vs Go: Which Is Better?
  • Use Golang for Data Processing With Amazon Kinesis and AWS Lambda
  • Fargate vs. Lambda: The Battle of the Future

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: