0

first, this is not similar to other questions because expressions contains parameters.

assume we have this method:

 public static int test(int x, Double y) {
    return somthing
}

and we have string expression like this:

 String expression = "test(1, 2)";

so how can we run this expression ?

I want to write a method with parameter and call it from a expression .

I can't find any library to solve this problem.

please help

meis
  • 3
  • 2
  • you can write your own – Stultuske May 20 '21 at 13:32
  • Do you know what input you are going to have? For example if you are creating a calculator you can enter some expressions like: sum, divide, minus which are limited number and you know them all. Or your use case should cover any kind of method name given in the expression? – Aethernite May 20 '21 at 13:40
  • Your basically talking about writing a parser here. https://stackoverflow.com/questions/34432136/writing-parser-of-a-simple-language – markspace May 20 '21 at 13:42
  • @Aethernite I know that. I edit the question. I want to write a method with parameter and call it as a expression – meis May 20 '21 at 13:45
  • @Stultuske can you tell me how? – meis May 20 '21 at 13:47
  • @markspace thanks. I think you right. can you give a good example? – meis May 20 '21 at 13:49
  • for simple things like this, you can even get it by using simple String operations, what have you tried so far? – Stultuske May 20 '21 at 13:49
  • Check this thread -> https://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string You can use reflection the get the method using its name but for the parameters you have to parse the string to get them. For example you can use a regex to extract them and then parse them into the wanted format. – Aethernite May 20 '21 at 13:51
  • @Stultuske assume this expression ` "func_round(func_areaUnderCurve(x) * y + 10000)"` – meis May 20 '21 at 13:52
  • @Aethernite I know that but maybe I can find a way simpler – meis May 20 '21 at 13:53

1 Answers1

2

You can use the Script Engine with a scripting language, such as JavaScript, to handle your expressions.

package expression;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class App 
{
    public static void main( String[] args )
    {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("nashorn");
        try {
            engine.eval("var test = Packages.expression.App.test");
            Object result = engine.eval("test(1,2)");
            System.out.println(result);
        } catch (ScriptException e) {
            //TODO: handle exception
            e.printStackTrace();
        }
    }

    public static int test(int x, Double y) {
        return x;
    }
}

The line "var test = Packages.expression.App.test" creates an alias to your method.

The line Object result = engine.eval("test(1,2)"); calls your method.

You need to include the following dependency to your maven pom.xml:

<dependency>
  <groupId>org.openjdk.nashorn</groupId>
  <artifactId>nashorn-core</artifactId>
  <version>15.1</version>
</dependency>
felipecrp
  • 999
  • 10
  • 16