-2

I want to convert a LambdaExpression like pos -> pos.x < 5 && pos.y < 5 to a String which looks like this: "pos.x < 5 && pos.y < 5"

Is this possible in Java?

In C# I'm using something similar like this

Lucraft
  • 3
  • 1
  • 5
  • Lambda expressions are compiled. You can't get it as a String. You can't turn a String into a lambda expression. – NomadMaker Jan 08 '21 at 20:07

2 Answers2

0

I don't believe that java provides any comparable functionality. You can see some exploration related to lambda values here.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
0

No, it's not possible.

Java is compiled to a set of bytecode instructions. The exact form of all Java source code, including that of lambdas, is lost at the point of compilation. It is a one-way process; certain details are irretrievably discarded (whitespace being one example, but there are other, less trivial ones) so you cannot recreate the source code from bytecode.

Michael
  • 41,989
  • 11
  • 82
  • 128
  • irreversable is a bit strong. there are java decompilers which can reproduce java code pretty well unless the code has been specifically obfuscated – jtahlborn Jan 08 '21 at 20:35
  • i guess our definitions of "irreversable" are a bit different then. you are correct, you cannot get back the _exact same code_ that you passed to it. – jtahlborn Jan 08 '21 at 20:44