This is a quick glance at hacking the JDK. This is ‘hacking’ in the lightest possible sense. We will look at JDK 9.

Monkey patching

You can ‘monkey patch’ in Ruby – sneakily extend or modify code, including Ruby source code.

For example, this is what you would expect when creating a new String:

String.new("here is a new String")
=> "here is a new String"

But thanks to the ability in Ruby to reopen any class and override its methods, you can write the following:

class String
	def self.new input
		"I'm going to ignore your input"
	end
end

Which means that if you try and run String.new("here is a new String") again, your result will be "I'm going to ignore your input" (try it yourself here).

Like many features in Ruby, it is a powerful tool. You can replace methods at runtime, modify behaviour of third party code and apply patches at runtime to objects in memory. It is a subtle way of altering behaviour. As such, it is a solution you would rarely reach for.

Can I do this in Java?

In the sense above, no, you can’t ‘monkey patch’ in Java. To manipulate core Java code you would need to consider using subclasses and overriding the superclass behaviour. Another solution is to look to the JDK. OpenJDK is open source.

If you have 50 minutes, watch this excellent talk by Ted Neward at Devoxx UK 2016, which is the inspiration for this article. If you have a lunch time to kill with set up, and 15 minutes to code, try the following:

Setup

This will take a while, so you may want to do this while your tests are running, code is compiling, production code is being released or lunch is being slowly cooked.

Building JDK 9 requires JDK 8, so ensure you have it available.

Install Mercurial. Check your GNU make utility is version 3.81 (4.0 on windows) or newer by typing:
make -version

Once Mercurial is installed, open up terminal and do the following:

  hg clone http://hg.openjdk.java.net/jdk9/jdk9 JDK9
  cd OJDK9
  bash ./get_source.sh

Once complete, run:

bash ./configure

If you get this error:

configure: error: Could not find freetype!

Either install freetype if it is not currently installed, or you may need XQuartz.

Hopefully this will be successful:

A new configuration has been successfully created in
.../JDK9/build/macosx-x86_64-normal-server-release
using default settings.

And all that is left is to run:

make all

This creates the set of compiled output needed to execute the newly built JDK. This takes a while the first time, and then is much faster to run. If there are any problems, see the builds README.

Finished building target 'all' in configuration 'macosx-x86_64-normal-server-release'

Get stuck in

Now you have the OpenJDK code ready and waiting, you can configure other projects to use the output of the build as their JDK by setting it to:

.../JDK9/build/macosx-x86_64-normal-server-release/images/jdk

Using build output as SDK

For something fun to get started with, have a look at:

langtools/src/jdk.jshell/share/classes/jdk/

Open up langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/resources/l10n.properties

Change the JShell welcome message:

change code welcome msg

jshell.msg.welcome =
Welcome to JShell -- Version {0}n
For an introduction type: /help intron

To include your name, e.g. ‘Welcome to JShell Katharine’.

As before, run

make all

Now you can see the effects of your change by going to:

JDK9/build/macosx-x86_64-normal-server-release/images/jdk

and calling:

bin/jshell -v

Welcome to JShell

|  Welcome to JShell Katharine -- Version 9-internal
|  For an introduction type: /help intro

What now?

This is a lot more involved than our basic Ruby monkey patching example. Apart from for fun and curiosity, you might wonder why you’d ever want to amend the JDK. Being able to play with the source code and tools for the JDK means two things: first, you can explore the nuts and bolts that go into the making of a JVM and the core libraries. Second, you can contribute.

If you enjoyed looking into the JDK, I encourage you to look at Adopting a JSR and using your skills to help the community.

 

Just enough code: hacking the JDK

About The Author
- Katharine is a Java developer by trade, turned Community & Content Manager for Voxxed. Helping developers learn and share knowledge. Contact me at kbe@voxxed.com with any news, articles or tutorials.