Search in sources :

Example 6 with MethodExecutionHandle

use of io.micronaut.inject.MethodExecutionHandle in project micronaut-core by micronaut-projects.

the class DefaultBeanContext method createExecutionHandle.

@Override
public MethodExecutionHandle<?, Object> createExecutionHandle(BeanDefinition<? extends Object> beanDefinition, ExecutableMethod<Object, ?> method) {
    return new MethodExecutionHandle<Object, Object>() {

        private Object target;

        @NonNull
        @Override
        public AnnotationMetadata getAnnotationMetadata() {
            return method.getAnnotationMetadata();
        }

        @SuppressWarnings("rawtypes")
        @Override
        public Object getTarget() {
            Object target = this.target;
            if (target == null) {
                synchronized (this) {
                    // double check
                    target = this.target;
                    if (target == null) {
                        try (BeanResolutionContext context = newResolutionContext(beanDefinition, null)) {
                            BeanDefinition rawDefinition = beanDefinition;
                            target = getBeanForDefinition(context, Argument.of(rawDefinition.getBeanType()), rawDefinition.getDeclaredQualifier(), true, rawDefinition);
                        }
                        this.target = target;
                    }
                }
            }
            return target;
        }

        @Override
        public Class getDeclaringType() {
            return beanDefinition.getBeanType();
        }

        @Override
        public String getMethodName() {
            return method.getMethodName();
        }

        @Override
        public Argument[] getArguments() {
            return method.getArguments();
        }

        @Override
        public Method getTargetMethod() {
            return method.getTargetMethod();
        }

        @Override
        public ReturnType getReturnType() {
            return method.getReturnType();
        }

        @Override
        public Object invoke(Object... arguments) {
            return method.invoke(getTarget(), arguments);
        }

        @NonNull
        @Override
        public ExecutableMethod<?, Object> getExecutableMethod() {
            return (ExecutableMethod<?, Object>) method;
        }
    };
}
Also used : Argument(io.micronaut.core.type.Argument) MethodExecutionHandle(io.micronaut.inject.MethodExecutionHandle) ExecutableMethod(io.micronaut.inject.ExecutableMethod) ProxyBeanDefinition(io.micronaut.inject.ProxyBeanDefinition) BeanDefinition(io.micronaut.inject.BeanDefinition) ValidatedBeanDefinition(io.micronaut.inject.ValidatedBeanDefinition) DisposableBeanDefinition(io.micronaut.inject.DisposableBeanDefinition) InitializingBeanDefinition(io.micronaut.inject.InitializingBeanDefinition)

Aggregations

MethodExecutionHandle (io.micronaut.inject.MethodExecutionHandle)6 Publishers (io.micronaut.core.async.publisher.Publishers)2 Argument (io.micronaut.core.type.Argument)2 HttpRequest (io.micronaut.http.HttpRequest)2 ExecutableMethod (io.micronaut.inject.ExecutableMethod)2 RoutingException (io.micronaut.web.router.exceptions.RoutingException)2 NettyByteBufferFactory (io.micronaut.buffer.netty.NettyByteBufferFactory)1 Internal (io.micronaut.core.annotation.Internal)1 ArgumentBinderRegistry (io.micronaut.core.bind.ArgumentBinderRegistry)1 BoundExecutable (io.micronaut.core.bind.BoundExecutable)1 DefaultExecutableBinder (io.micronaut.core.bind.DefaultExecutableBinder)1 ExecutableBinder (io.micronaut.core.bind.ExecutableBinder)1 UnsatisfiedArgumentException (io.micronaut.core.bind.exceptions.UnsatisfiedArgumentException)1 ConversionService (io.micronaut.core.convert.ConversionService)1 ArgumentValue (io.micronaut.core.type.ArgumentValue)1 MutableArgumentValue (io.micronaut.core.type.MutableArgumentValue)1 CollectionUtils (io.micronaut.core.util.CollectionUtils)1 StringUtils (io.micronaut.core.util.StringUtils)1 HttpAttributes (io.micronaut.http.HttpAttributes)1 MediaType (io.micronaut.http.MediaType)1