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

io.micronaut.context.ExecutionHandleLocator Maven / Gradle / Ivy

There is a newer version: 4.7.5
Show newest version
/*
 * Copyright 2017-2020 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.context;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.UsedByGeneratedCode;
import io.micronaut.core.type.Argument;
import io.micronaut.inject.BeanDefinition;
import io.micronaut.inject.ExecutableMethod;
import io.micronaut.inject.MethodExecutionHandle;

import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Interface for components that are able to locate and return {@link io.micronaut.inject.ExecutionHandle} instances
 * for beans.
 *
 * @author Graeme Rocher
 * @since 1.0
 */
public interface ExecutionHandleLocator {

    /**
     * A empty no-op locator.
     */
    ExecutionHandleLocator EMPTY = new ExecutionHandleLocator() { };

    /**
     * Finds an optimized execution handle for invoking a bean method. The execution handle may or may not be
     * implemented by generated byte code.
     *
     * @param  The target bean
     * @param        The result type of the execution handle
     * @param beanType  The bean type
     * @param method    The method
     * @param arguments The arguments
     * @return The execution handle
     */
    default  Optional> findExecutionHandle(Class beanType, String method, Class... arguments) {
        return Optional.empty();
    }

    /**
     * Finds an optimized execution handle for invoking a bean method. The execution handle may or may not be
     * implemented by generated byte code.
     *
     * @param  The target bean
     * @param        The result type of the execution handle
     * @param beanType  The bean type
     * @param qualifier The bean qualifer
     * @param method    The method
     * @param arguments The arguments
     * @return The execution handle
     */
    default  Optional> findExecutionHandle(Class beanType, Qualifier qualifier, String method, Class... arguments) {
        return Optional.empty();
    }

    /**
     * Finds an optimized execution handle for invoking a bean method. The execution handle may or may not be
     * implemented by generated byte code.
     *
     * @param  The target bean
     * @param        The result type of the execution handle
     * @param bean      The bean to invoke the method on
     * @param method    The method
     * @param arguments The arguments
     * @return The execution handle
     */
    default  Optional> findExecutionHandle(T bean, String method, Class... arguments) {
        return Optional.empty();
    }

    /**
     * Finds an optimized execution handle for invoking a bean method. The execution handle may or may not be
     * implemented by generated byte code.
     *
     * @param        The bean type class
     * @param        The result type of the execution handle
     * @param beanType  The bean type
     * @param method    The method
     * @param arguments The arguments
     * @return The execution handle
     */
    default  Optional> findExecutableMethod(Class beanType, String method, Class... arguments) {
        return Optional.empty();
    }

    /**
     * Finds the original unproxied method for a {@link io.micronaut.inject.ProxyBeanDefinition}.
     *
     * @param        The bean type class
     * @param        The result type of the execution handle
     * @param beanType  The bean type
     * @param method    The method
     * @param arguments The arguments
     * @return The execution handle
     */
    default  Optional> findProxyTargetMethod(Class beanType, String method, Class... arguments) {
        return Optional.empty();
    }

    /**
     * Finds the original unproxied method for a {@link io.micronaut.inject.ProxyBeanDefinition}.
     *
     * @param        The bean type class
     * @param        The result type of the execution handle
     * @param beanType  The bean type
     * @param qualifier The qualifier
     * @param method    The method
     * @param arguments The arguments
     * @return The execution handle
     */
    default  Optional> findProxyTargetMethod(Class beanType, Qualifier qualifier, String method, Class... arguments) {
        return Optional.empty();
    }

    /**
     * Finds the original unproxied method for a {@link io.micronaut.inject.ProxyBeanDefinition}.
     *
     * @param        The bean type class
     * @param        The result type of the execution handle
     * @param beanType  The bean type
     * @param qualifier The qualifier
     * @param method    The method
     * @param arguments The arguments
     * @return The execution handle
     */
    default  Optional> findProxyTargetMethod(Argument beanType, Qualifier qualifier, String method, Class... arguments) {
        return Optional.empty();
    }

    /**
     * Finds an optimized execution handle for invoking a bean method. The execution handle may or may not be
     * implemented by generated byte code.
     *
     * @param        The bean type class
     * @param        The result type of the execution handle
     * @param beanType  The bean type
     * @param method    The method
     * @param arguments The arguments
     * @return The execution handle
     * @throws NoSuchMethodException if the method cannot be found
     */
    default  ExecutableMethod getExecutableMethod(Class beanType, String method, Class... arguments) throws NoSuchMethodException {
        Optional> executableMethod = this.findExecutableMethod(beanType, method, arguments);
        return executableMethod.orElseThrow(() -> newNoSuchMethodException(beanType.getName(), method, arguments));
    }

    /**
     * Finds an optimized execution handle for invoking a bean method. The execution handle may or may not be
     * implemented by generated byte code.
     *
     * @param        The bean type class
     * @param        The result type of the execution handle
     * @param beanType  The bean type
     * @param method    The method
     * @param arguments The arguments
     * @return The execution handle
     * @throws NoSuchMethodException if the method cannot be found
     */
    @UsedByGeneratedCode
    default  ExecutableMethod getProxyTargetMethod(Class beanType, String method, Class... arguments) throws NoSuchMethodException {
        Optional> executableMethod = this.findProxyTargetMethod(beanType, method, arguments);
        return executableMethod.orElseThrow(() -> newNoSuchMethodException(beanType.getName(), method, arguments));
    }

    /**
     * Finds an optimized execution handle for invoking a bean method. The execution handle may or may not be
     * implemented by generated byte code.
     *
     * @param        The bean type class
     * @param        The result type of the execution handle
     * @param beanType  The bean type
     * @param qualifier The qualifier
     * @param method    The method
     * @param arguments The arguments
     * @return The execution handle
     * @throws NoSuchMethodException if the method cannot be found
     */
    @UsedByGeneratedCode
    default  ExecutableMethod getProxyTargetMethod(Class beanType, Qualifier qualifier, String method, Class... arguments) throws NoSuchMethodException {
        Optional> executableMethod = this.findProxyTargetMethod(beanType, qualifier, method, arguments);
        return executableMethod.orElseThrow(() -> newNoSuchMethodException(beanType.getName(), method, arguments));
    }

    /**
     * Finds an optimized execution handle for invoking a bean method. The execution handle may or may not be
     * implemented by generated byte code.
     *
     * @param        The bean type class
     * @param        The result type of the execution handle
     * @param beanType  The bean type
     * @param qualifier The qualifier
     * @param method    The method
     * @param arguments The arguments
     * @return The execution handle
     * @throws NoSuchMethodException if the method cannot be found
     * @since 3.0.0
     */
    @UsedByGeneratedCode
    default  ExecutableMethod getProxyTargetMethod(Argument beanType, Qualifier qualifier, String method, Class... arguments) throws NoSuchMethodException {
        return this.findProxyTargetMethod(beanType, qualifier, method, arguments)
                .orElseThrow(() -> newNoSuchMethodException(beanType.getName(), method, arguments));
    }

    /**
     * Finds an optimized execution handle for invoking a bean method. The execution handle may or may not be
     * implemented by generated byte code.
     *
     * @param  The target bean
     * @param        The result type of the execution handle
     * @param beanType  The bean type
     * @param method    The method
     * @param arguments The arguments
     * @return The execution handle
     * @throws NoSuchMethodException if the method cannot be found
     */
    default  MethodExecutionHandle getExecutionHandle(Class beanType, String method, Class... arguments) throws NoSuchMethodException {
        return this.findExecutionHandle(beanType, method, arguments)
                .orElseThrow(() -> newNoSuchMethodException(beanType.getName(), method, arguments));
    }

    /**
     * Finds an optimized execution handle for invoking a bean method. The execution handle may or may not be implemented by generated byte code.
     *
     * @param  The target bean
     * @param        The result type of the execution handle
     * @param bean      The bean to invoke the method on
     * @param method    The method
     * @param arguments The arguments
     * @return The execution handle
     * @throws NoSuchMethodException if the method cannot be found
     */
    default  MethodExecutionHandle getExecutionHandle(T bean, String method, Class... arguments) throws NoSuchMethodException {
        return this.findExecutionHandle(bean, method, arguments)
                .orElseThrow(() -> newNoSuchMethodException(bean.toString(), method, arguments));
    }

    /**
     * Create an execution handle for the given bean definition and method.
     * @param beanDefinition The bean definition
     * @param method The method
     * @return The execution handle
     */
    default MethodExecutionHandle createExecutionHandle(BeanDefinition beanDefinition, ExecutableMethod method) {
        throw new UnsupportedOperationException("No such method [" + method + "(" + Arrays.stream(method.getArgumentTypes()).map(Class::getName).collect(Collectors.joining(",")) + ") ] for bean [" + beanDefinition.getBeanType() + "]");
    }

    @NonNull
    private static NoSuchMethodException newNoSuchMethodException(@NonNull String bean, @NonNull String method, @NonNull Class[] arguments) {
        return new NoSuchMethodException("No such method [" + method + "(" + Arrays.stream(arguments).map(Class::getName).collect(Collectors.joining(",")) + ")] for bean [" + bean + "]");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy