apache/flink · error · InvalidTypesException

Given class: {} is not a FunctionalInterface. It has more th

Error message

Given class: {} is not a FunctionalInterface. It has more than one abstract method.

What it means

Thrown by TypeExtractionUtils.getSingleAbstractMethod when the interface has more than one abstract method. A functional interface must have exactly one abstract method; having multiple means the JVM cannot determine which method is the 'lambda' target. The method iterates all declared methods and aborts as soon as it finds a second abstract method.

Source

Thrown at flink-core/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractionUtils.java:248

     *
     * @param baseClass a class that is a FunctionalInterface to retrieve a SAM from
     * @throws InvalidTypesException if the given class does not implement FunctionalInterface
     * @return single abstract method of the given class
     */
    public static Method getSingleAbstractMethod(Class<?> baseClass) {

        if (!baseClass.isInterface()) {
            throw new InvalidTypesException(
                    "Given class: " + baseClass + "is not a FunctionalInterface.");
        }

        Method sam = null;
        for (Method method : baseClass.getMethods()) {
            if (Modifier.isAbstract(method.getModifiers())) {
                if (sam == null) {
                    sam = method;
                } else {
                    throw new InvalidTypesException(
                            "Given class: "
                                    + baseClass
                                    + " is not a FunctionalInterface. It has more than one abstract method.");
                }
            }
        }

        if (sam == null) {
            throw new InvalidTypesException(
                    "Given class: "
                            + baseClass
                            + " is not a FunctionalInterface. It does not have any abstract methods.");
        }

        return sam;
    }

    /** Returns all declared methods of a class including methods of superclasses. */

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Ensure your function interface has exactly one abstract method (annotate with @FunctionalInterface to get compile-time validation).
  2. Split the interface into separate single-method interfaces.
  3. Implement ResultTypeQueryable on the function class to bypass SAM-based extraction.
  4. Use Flink's built-in function interfaces which are already valid functional interfaces.

Example fix

// before
public interface MyFunction<T, O> extends MapFunction<T, O>, FilterFunction<T> {
    // two abstract methods: map() and filter()
}
// after
// Use MapFunction and FilterFunction as separate operations
ds.map(new MapFunction<String, Integer>() {
    public Integer map(String s) { return s.length(); }
})
Defensive patterns

Strategy: type-guard

Validate before calling

long abstractCount = Arrays.stream(baseClass.getMethods())
    .filter(m -> Modifier.isAbstract(m.getModifiers()))
    .count();
if (abstractCount > 1) {
    throw new IllegalArgumentException(
        baseClass + " has " + abstractCount + " abstract methods; expected exactly 1");
}

Type guard

static boolean hasSingleAbstractMethod(Class<?> clazz) {
    if (!clazz.isInterface()) return false;
    long count = Arrays.stream(clazz.getMethods())
        .filter(m -> Modifier.isAbstract(m.getModifiers())).count();
    return count == 1;
}

Prevention

When it happens

Trigger: The baseClass interface declares multiple abstract methods (not a valid functional interface). The interface inherits abstract methods from multiple parent interfaces that together total more than one. Default methods are fine, but multiple non-default, non-static abstract methods trigger this.

Common situations: Custom function interface that adds a second abstract method beyond the SAM method. Combining multiple Flink interfaces into one custom interface that has overlapping abstract methods. Inheriting from two interfaces that don't share a common abstract method signature.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/20228e7fe7a84084. Report an issue: GitHub.