Get access to source code of a lambda function

Is there any way to get access to source code of a lambda function in runtime?

I’m looking for a way to annotate a parameter of a function, so that during compilation, the source code would be stored alongside with the bytecode as metadata.

Use case: In tests, we’re using Hamcrest matchers. I’ve written a utility to transform a matcher using user-supplied function. Usually it’s a very simple lambda, like I would like to print the transforming function if the match failed. I don’t want to describe the functions manually using a separate argument; I’d like it to be automated.

Perhaps there’s some library or compiler plugin out there?


Don’t have a full answer for you but thought I’d mention this compiler plugin since the use-case of source-code inspection on failure is similar.

1 Like

Wow, thanks! On the first glance looks like something I’d rather use instead of what I was trying to do! Maybe it wasn’t there last time I looked (the utility I was talking about was written a few years ago).

Glad to help! The Advanced Usage section, which shows how to transform any custom function and decide what to do with the text is really neat.

1 Like