17 Modern internals

 

This chapter covers

  • Introducing JVM internals
  • Reflection Internals
  • Method Handles
  • Invokedynamic
  • Recent Internal Changes
  • Unsafe

Java’s virtual machine is an extremely sophisticated runtime environment that has, for decades, prioritized stability and production-grade engineering. For these reasons, many Java developers have never needed to poke about in the internals - because it is simply not necessary to do so most of the time.

This chapter, on the other hand, is for the curious; the people who would like to know more; who would like to draw back the curtain and see some of the details of how the JVM is implemented. Let’s start with method invocation.

17.1 Introducing JVM internals - method invocation

To get going, let’s look at a simple example, defined by the classes Pet, Cat and Bear and the interface Furry. This can be seen in Figure 17.1

Figure 17.1. Simple Inheritance Hierarchy
image

We might also suppose that other subclasses of Pet exist (for example Dog and Fish) that aren’t shown in the diagram in order to keep things clear. We’ll use this example to explain in detail how the different invoke opcodes work, starting with invokevirtual.

17.1.1 Invoking virtual methods

The most common type of method invocation is calling an instance method on an object of a specific class (or a subclass) by use of an invokevirtual bytecode.

17.1.2 Invoking interface methods

17.1.3 Invoking "special" methods

17.1.4 Final methods

17.2 Reflection internals

17.3 Method handles

17.3.1 MethodHandle

17.3.2 MethodType

17.3.3 Looking up method handles

17.3.4 Reflection vs. proxies vs. method handles

17.4 Invokedynamic

17.5.2 Compact strings

sitemap