private static SerializedLambda serializeLambda()

in pojo/src/main/java/com/spotify/hamcrest/pojo/IsPojo.java [223:246]


  private static SerializedLambda serializeLambda(final Object lambda) {
    requireNonNull(lambda);

    final Method writeReplace;
    try {
      writeReplace =
          AccessController.doPrivileged(
              (PrivilegedExceptionAction<Method>)
                  () -> {
                    Method method = lambda.getClass().getDeclaredMethod("writeReplace");
                    method.setAccessible(true);
                    return method;
                  });
    } catch (PrivilegedActionException e) {
      throw new IllegalStateException("Cannot serialize lambdas in unprivileged context", e);
    }

    try {
      return (SerializedLambda) writeReplace.invoke(lambda);
    } catch (ClassCastException | IllegalAccessException | InvocationTargetException e) {
      throw new IllegalArgumentException(
          "Could not serialize as a lambda (is it a lambda?): " + lambda, e);
    }
  }