Is there a way to register overloaded functions in SpEL?

20 Views Asked by At

This question is about how to deal with overloaded functions in SpEL. Here is my usecase:

I have 2 different implementations of the substring function as below.

public static String substring1(String data, int beginIndex) {
    return data != null ? data.substring(beginIndex) : null;
}

public static String substring1(String data, int beginIndex, int endIndex) {
    return data != null ? data.substring(beginIndex, endIndex) : null;
}

I then register these functions as SpEL functions as below:

StandardEvaluationContext context = new StandardEvaluationContext();

context.registerFunction("substring1", StringFunction.class.getDeclaredMethod(
            "substring1", new Class[] { String.class, Integer.TYPE}));

context.registerFunction("substring2", StringFunction.class.getDeclaredMethod(
            "substring2", new Class[] { String.class, Integer.TYPE, Integer.TYPE}));

Then use the context to evaluate the expression. Something like below:

Boolean result = (Boolean) expression.getValue(context);

This works fine. But, I would like this to work as overloaded functions. substring is an overloaded function and I don't want the user to type a substring1 and substring2. They should be able to just type substring and it should work based on the number of parameters given.

Is there a way to register and use overloaded functions in SpEL?

0

There are 0 best solutions below