Java in Functions and Closures

Java in Functions and Closures

You can write CFML code directly in a function or a closure.

Functions

Inside the function, you can write regular Java code. The arguments and return type definition must be Java types.

int function echoInt(int i) type="java" {
    if (i == 1) throw new Exception("Upsi dupsi!!!");
    return i * 2;
}

Components

Of course, the function can also be part of a component.

component {
    int function echoInt(int i) type="java" {
        if (i == 1) throw new Exception("Test output!!!");
        return i * 2;
    }
}

Java Lambda Functions

If the interface of a function matches a functional Java interface (Lambda), Lucee automatically implements that interface.

In the following example, we implement the IntUnaryOperator implicitly. You can then pass it to Java and use it as such.

int function echoInt(int i) type="java" {
    if (i == 1) throw new Exception("Test");
    return i * 2;
}
dump(echoInt(1));

See also