While it's certainly possible to achieve what's you're describing, it's infinitely easier to use a scripting language for such dynamic expressions.
JDK still has a JavaScript engine built-in, but you can add 3rd party engines easily. If you really want Java to be the language used in the dynamic scripts, one idea is to use JShell script engine wrapper.
Either way, the approach would go like this:
//Read the code to evaluate
String scriptCode = ...;
//Init the engine of choice
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript"); //Or "jshell" or whatever
//Add all implicit variable values (if any)
Bindings bindings = engine.createBindings();
bindings.put("variableName", variableValue);
//Execute
engine.eval(scriptCode, bindings));
BeanShell or Groovy are other Java-like options.