All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.smallrye.faulttolerance.internal.FallbackMethodCandidates Maven / Gradle / Ivy

There is a newer version: 6.7.1
Show newest version
package io.smallrye.faulttolerance.internal;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.smallrye.faulttolerance.config.FaultToleranceOperation;

public final class FallbackMethodCandidates {
    private final FallbackMethod withoutExceptionParam;
    private final Map, FallbackMethod> withExceptionParam;

    private FallbackMethodCandidates(Method withoutExceptionParam, List withExceptionParam) {
        this.withoutExceptionParam = FallbackMethod.withoutExceptionParameter(withoutExceptionParam);

        Map, FallbackMethod> map = new HashMap<>();
        for (Method method : withExceptionParam) {
            int exceptionParameterPosition = method.getParameterCount() - 1;
            if (KotlinSupport.isSuspendingFunction(method)) {
                exceptionParameterPosition--;
            }

            map.put(method.getParameterTypes()[exceptionParameterPosition],
                    FallbackMethod.withExceptionParameter(method, exceptionParameterPosition));
        }
        this.withExceptionParam = map;
    }

    public boolean isEmpty() {
        return withoutExceptionParam == null && withExceptionParam.isEmpty();
    }

    public FallbackMethod select(Class exceptionType) {
        if (!withExceptionParam.isEmpty()) {
            Class type = exceptionType;
            while (type != null) {
                FallbackMethod candidate = withExceptionParam.get(type);
                if (candidate != null) {
                    return candidate;
                }
                type = type.getSuperclass();
            }
        }

        // may be null
        return withoutExceptionParam;
    }

    public static FallbackMethodCandidates create(FaultToleranceOperation operation, boolean allowExceptionParam) {
        Method withoutExceptionParam = operation.getFallbackMethod();
        List withExceptionParam = Collections.emptyList();
        if (allowExceptionParam && operation.getFallbackMethodsWithExceptionParameter() != null) {
            withExceptionParam = operation.getFallbackMethodsWithExceptionParameter();
        }

        return new FallbackMethodCandidates(withoutExceptionParam, withExceptionParam);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy