Skip to content
Advertisement

Lambda expressions and anonymous classes don’t work when loaded as hidden classes

I am trying to compile and load dynamically generated Java code during runtime. Since both ClassLoader::defineClass and Unsafe::defineAnonymousClass have serious drawbacks in this scenario, I tried using hidden classes via Lookup::defineHiddenClass instead. This works fine for all classes that I tried to load, except for those that call lambda expressions or contain anonymous classes.

Calling a lambda expression throws the following exception:

JavaScript

Executing code that instantiates an anonymous class throws the following error:

JavaScript

This is a short example that recreates the problem:

JavaScript

I’ve already tried compiling and running the code with different JDKs, using different ways to create new instances of the hidden class, searching for bugs at https://bugs.openjdk.java.net/, messing with the bytecode itself and several other things. I am not an expert on Java internals, so I am not sure whether I have not understood the JEP that introduced hidden classes correctly.

Am I doing something wrong, is this just impossible or is this a bug?

Edit: The JEP states

Migration should take the following into account: To invoke private nestmate instance methods from code in a hidden class, use invokevirtual or invokeinterface instead of invokespecial. Generated bytecode that uses invokespecial to invoke a private nestmate instance method will fail verification. invokespecial should only be used to invoke private nestmate constructors.

This might be the problem for the anonymous class. Is there a way to compile the code such that invokespecial is avoided in the bytecode?

Advertisement

Answer

You can not turn arbitrary classes into hidden classes.

The documentation of defineHiddenClass contains the sentence

  • On any attempt to resolve the entry in the run-time constant pool indicated by this_class, the symbolic reference is considered to be resolved to C and resolution always succeeds immediately.

What it doesn’t spell out explicitly is that this is the only place where a type resolution ever ends up at the hidden class.

But it has been said unambiguously in bug report JDK-8222730:

For a hidden class, its specified hidden name should only be accessible through the hidden class’s ‘this_class’ constant pool entry.

The class should not be accessible by specifying its original name in, for example, a method or field signature even within the hidden class.

Which we can check. Even a simple case like

JavaScript

will already fail. Note that it is a special case that the attempt to resolve the original class name LambdaRunner within the hidden class will not fail, as you used an existing class as template. So you get an IncompatibleClassChangeError or a VerifierError due to mismatches between the hidden class and the existing LambdaRunner class. When you don’t use a class definition of an existing class, you’d get a NoClassDefFoundError.

The same applies to

JavaScript

As the cited bug report said, neither field nor methods can refer to the hidden class in their signature.

A less intuitive example is

JavaScript

which will fail depending on the compiler and options, as when the StringConcatFactory is used, the behavior is like an invocation of a method having all non-constant parts as parameters and returning a String. So this is another case of having the hidden class in a method signature.


Lambda expressions are special, as a class like

JavaScript

gets compiled similar to

JavaScript

which doesn’t have the hidden class in the method signature, but has to refer to the method holding the body of the lambda expression as a MethodReference. Within the constant pool, the description of this method refers to its declaring class using the this_class entry. So it gets redirected to the hidden class as described in the documentation.

But the construction of the MethodType as part of the MethodReference does not use this information to load a Class like a class literal would do. Instead, it tries to load the hidden class through the defining class loader, which fails with the NoClassDefFoundError you have posted.

This seems to be related to JDK-8130087 which suggests that ordinary method resolution differs from the way, MethodType works, which can make MethodType fail where just invoking the method would work.

But it’s possible to demonstrate that even fixing this issue wouldn’t solve the general problem:

JavaScript

This bypasses the problem described above and calls the LambdaMetafactory manually. When being redefined as hidden class, it will print:

JavaScript

which shows that all obstacles have been circumvented, but when it comes to the actual invocation from the generated Runnable to the method holding the lambda body, it will fail due to the fact that the target class is hidden. A JVM with eager resolution of symbolic references might fail earlier, i.e. the example might not print got runnable then.

Unlike the old JVM anonymous classes, there is no way to link to a hidden class, not even from another hidden class.


The bottom line is, as said at the beginning, you can not turn arbitrary classes into hidden classes. Lambda expressions are not the only feature not working with hidden classes. It’s not a good idea to try and get surprised. Hidden classes should only be used in conjunction with bytecode generators carefully using only features known to work.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement