When lambda expression is used Java actually creates an anonymous (non-static) class. Non-static inner classes always contain references their enclosing objects.
When this lambda expression is called from another library that may invoke lambda in a different process that invocation crashes with class not found exception because it cannot find enclosing object's class in another process.
Consider this example:
public class MyClass {
public void doSomething() {
remoteLambdaExecutor.executeLambda(value -> value.equals("test"));
}
}
Java will create an anonymous inner class that implements certain functional interface and pass it as a parameter to executeLambda(). Then remoteLambdaExecutor will take that anonymous class across the process to run remotely. Remote process knows nothing about MyClass and will throw
java.lang.ClassNotFoundException: MyClass
Because it needs MyClass for that enclosing object reference.
I can always use a static implementation of the functional interface expected by an API, but that defeats the purpose and doesn't utilize lambda functionality.
Is there way to solve it using lambda expressions?
UPDATE: I cannot use static class either unless it's somehow exported to that other process.
A lambda expression body can't throw any exceptions that haven't specified in a functional interface. If the lambda expression can throw an exception then the "throws" clause of a functional interface must declare the same exception or one of its subtype.
You do not have to create a functional interface in order to create lambda function.
The method references can only be used to replace a single method of the lambda expression.
Short answer: no. For stateless lambdas (those that do not capture anything from their lexical context), only one instance will ever be created (lazily), and cached at the capture site.
Your initial premise is wrong. The JRE will not generate an anonymous inner class. It may generate a class, but if your lambda expression does not access this
or a non-static
member of the class, it will not keep a reference to the this
instance.
However, this does not imply that the class itself is unnecessary. Since the class hosts the code of the lambda expression, it will always be needed. In this regard, your solution of using a static
nested class doesn’t change anything about it, as then, it’s the static
nested class that is needed for the execution of the code.
There is no way to transfer an object to a remote execution facility without transferring the class that contains the code to execute (unless the class already exists at the remote site).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With