Java_Magazine_NovemberDecember_2018

(singke) #1

48


//java at present/


method call and sometimes not. Either way, Java 11 removes these bridge methods and resolves
this performance issue.
From Java 11 onward, the semantics at the bytecode level are much closer to the source
code level when nested classes are compiled. Within the whole nest, private access for methods
and fields is complete and undifferentiated between any nestmates. This is codified in Java 11’s
updated JVM specification, which requires JVM vendors to support the nest access rules.
It’s not just magic, though. To support the access rules, javac must be able to communicate
to the JVM which classes are part of what nests. This is done via class file annotations. The top-
level class file, StringIterable in the example, is called the nest host. It has an attribute added to
it called NestMembers that lists all the other classes in the nest. In this example, the only other
class would be StringIterator. These other nestmates have an attribute, NestHost, that declares
the host class of the nest. To expose these properties via the reflection API, the methods
getNestHost(), getNestMembers(), and isNestmateOf() have been added to java.lang.Class.
If you’re interested in more information about nests and nestmates, see JEP 181.
So far, you’ve seen how nestmates tidied up a rough edge of the Java language and bytecode
specification, but they do more than that: they provide a glimpse into future improvements to
the JVM. The idea of a nest likely will be used by generic specialization from project Valhalla,
replacing the deprecated Unsafe.defineAnonymousClass() API and “sealed” classes that can’t
be extended.

Removing Modules
Java has had a long history of deprecation. It added the @Deprecated annotation in Java 5. What
deprecated means has evolved over time. Originally, the idea of deprecating something in Java
meant that it was intended to be removed in a future version. However, deprecated meth-
ods have tended to not be removed. For example, several methods from the Abstract Window
Toolkit (AWT) were deprecated in Java 1.1 with the intention of removing them in Java 1.2, but
they weren’t removed in that or subsequent releases. With Java 11, however, deprecation gains
some teeth—in fact, several Java modules have been removed.
Free download pdf