Bethan Palmer Bethan is a Java developer and a Java Champion. She has spoken at conferences including JavaOne/Code One, DevFest and NetBeans days. She has a degree in English Literature.

Java 12’s JVM Constants API explained in 5 minutes

1 min read

what's new

This month we are focusing on Java 12 new features you should know about. In other articles, we looked at improvements to Garbage Collectionkey takeaways from upgrading to Java 12, Java 12 made microbenchmarking easier and Java 12 Switch Expressions explained in 5 minutes

In this article, we are looking at the JVM Constants API.

The purpose of this new API (JEP 344) is to make it easier for programs that manipulate class files to model bytecode instructions, which have to handle loadable constants. This is a very technical change, so it is more about helping the developers of Java to make it better than giving general developers a specific new feature.

So firstly, what are loadable constants?

Every Java class has something called a constant pool table. Each entry in the table is a loadable constant and has the following format:

cp_info {
    u1 tag;
    u1 info[];
}

The tag specifies what type the constant is, e.g. 7 means it’s Class, 10 is a Method Reference, 8 is a String etc.

The info array gives more information about the constant (this differs depending on the type).

JVM instructions, e.g. ldc and invokedynamic instructions, rely on the information in this table (rather than the run-time layout of classes, interfaces etc).  When these instructions are executed, the loadable constant becomes a live value, e.g. a Class, String, int etc.

Programs that work with class files have to model these bytecode instructions (and therefore deal with loadable constants). If the constant type is something like a String or an Integer, this works without issue. But it can become complicated if the constant type is a Class.

Loading classes is not always straightforward, and there are several ways it can fail, for example if the Class does not exist or cannot be accessed.

This is where JEP 334 comes in. 

In Java 12 there is now an API so that these values can be handled symbolically. For example there is an interface called ClassDesc which can be used to symbolically represent a loadable constant of type Class, MethodHandleDesc to represent method handle constants, MethodTypeDesc to represent method type constants and so on.

The full draft Javadocs for the API can be found here.

An example of a class that will benefit from this new feature is LambdaMetaFactory, which provides bootstrap methods for invokedynamic call sites. The new API will mean that how this class works can be hugely simplified.

Jlink plugins will also benefit, as they have to deal with classes which are not yet available when they are described.

What are your thoughts on the changes?

 

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Bethan Palmer Bethan is a Java developer and a Java Champion. She has spoken at conferences including JavaOne/Code One, DevFest and NetBeans days. She has a degree in English Literature.

How to choose JPG versus JPEG2000 for image files

Since we started to support both JPG and JPG2000 as image file outputs in our software, we have found that this is a very...
Mark Stephens
1 min read

What’s new in Java13?

Nirali
1 min read

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2019. All rights reserved.