Is there a function which accepts a reference to a lambda expression and returns a boolean saying whether the lambda expression is stateless or not? How can the statefulness of
Here's a simple and stupid idea. Just check if your lambda has fields.
For instance, consider the following stateful lambda.
List serialStorage = new ArrayList<>();
Function super Integer, ? extends Integer> statefulLambda =
e -> { serialStorage.add(e); return e; };
This statefulLambda
has a private final internal field arg$1
which obviously references serialStorage
. So
statefulLambda.getClass().getDeclaredFields().length > 0
could be used as indicator that lambda is stateful.
However I have no idea if this will generally work.