Search in sources :

Example 1 with BeanResolutionContext

use of io.micronaut.context.BeanResolutionContext in project micronaut-core by micronaut-projects.

the class AbstractProviderDefinition method build.

@Override
public T build(BeanResolutionContext resolutionContext, BeanContext context, BeanDefinition<T> definition) throws BeanInstantiationException {
    final BeanResolutionContext.Segment<?> segment = resolutionContext.getPath().currentSegment().orElse(null);
    if (segment != null) {
        final InjectionPoint<?> injectionPoint = segment.getInjectionPoint();
        if (injectionPoint instanceof ArgumentCoercible) {
            Argument<?> injectionPointArgument = ((ArgumentCoercible<?>) injectionPoint).asArgument();
            Argument<?> resolveArgument = injectionPointArgument;
            if (resolveArgument.isOptional()) {
                resolveArgument = resolveArgument.getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT);
            }
            @SuppressWarnings("unchecked") Argument<Object> argument = (Argument<Object>) resolveArgument.getFirstTypeVariable().orElse(null);
            if (argument != null) {
                Qualifier<Object> qualifier = (Qualifier<Object>) resolutionContext.getCurrentQualifier();
                if (qualifier == null && segment.getDeclaringType().isIterable()) {
                    final Object n = resolutionContext.getAttribute(Named.class.getName());
                    if (n != null) {
                        qualifier = Qualifiers.byName(n.toString());
                    }
                }
                boolean hasBean = context.containsBean(argument, qualifier);
                if (hasBean) {
                    return buildProvider(resolutionContext, context, argument, qualifier, definition.isSingleton());
                } else {
                    if (injectionPointArgument.isOptional()) {
                        return (T) Optional.empty();
                    } else if (injectionPointArgument.isNullable()) {
                        throw new DisabledBeanException("Nullable bean doesn't exist");
                    } else {
                        if (qualifier instanceof AnyQualifier || isAllowEmptyProviders(context)) {
                            return buildProvider(resolutionContext, context, argument, qualifier, definition.isSingleton());
                        } else {
                            throw new NoSuchBeanException(argument, qualifier);
                        }
                    }
                }
            }
        }
    }
    throw new UnsupportedOperationException("Cannot inject provider for Object type");
}
Also used : Named(io.micronaut.core.naming.Named) Argument(io.micronaut.core.type.Argument) NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) ArgumentCoercible(io.micronaut.core.type.ArgumentCoercible) DisabledBeanException(io.micronaut.context.exceptions.DisabledBeanException) AnyQualifier(io.micronaut.inject.qualifiers.AnyQualifier) Qualifier(io.micronaut.context.Qualifier) AnyQualifier(io.micronaut.inject.qualifiers.AnyQualifier) BeanResolutionContext(io.micronaut.context.BeanResolutionContext)

Example 2 with BeanResolutionContext

use of io.micronaut.context.BeanResolutionContext in project micronaut-core by micronaut-projects.

the class AopProxyWriter method writeInterceptedTargetMethod.

private void writeInterceptedTargetMethod(ClassWriter proxyClassWriter, Type targetType) {
    // add interceptedTarget() method
    GeneratorAdapter interceptedTargetVisitor = startPublicMethod(proxyClassWriter, "interceptedTarget", Object.class.getName());
    if (lazy) {
        if (cacheLazyTarget) {
            // Object local = this.$target;
            int targetLocal = interceptedTargetVisitor.newLocal(targetType);
            interceptedTargetVisitor.loadThis();
            interceptedTargetVisitor.getField(proxyType, FIELD_TARGET, targetType);
            interceptedTargetVisitor.storeLocal(targetLocal, targetType);
            // if (local == null) {
            interceptedTargetVisitor.loadLocal(targetLocal, targetType);
            Label returnLabel = new Label();
            interceptedTargetVisitor.ifNonNull(returnLabel);
            // synchronized (this) {
            Label synchronizationEnd = new Label();
            interceptedTargetVisitor.loadThis();
            interceptedTargetVisitor.monitorEnter();
            Label tryLabel = new Label();
            Label catchLabel = new Label();
            interceptedTargetVisitor.visitTryCatchBlock(tryLabel, returnLabel, catchLabel, null);
            // Try body
            interceptedTargetVisitor.visitLabel(tryLabel);
            // local = this.$target
            interceptedTargetVisitor.loadThis();
            interceptedTargetVisitor.getField(proxyType, FIELD_TARGET, targetType);
            interceptedTargetVisitor.storeLocal(targetLocal, targetType);
            // if (local == null) {
            interceptedTargetVisitor.loadLocal(targetLocal, targetType);
            interceptedTargetVisitor.ifNonNull(synchronizationEnd);
            // this.$target =
            interceptedTargetVisitor.loadThis();
            pushResolveLazyProxyTargetBean(interceptedTargetVisitor, targetType);
            interceptedTargetVisitor.putField(proxyType, FIELD_TARGET, targetType);
            // cleanup this.$beanResolutionContext
            interceptedTargetVisitor.loadThis();
            interceptedTargetVisitor.push((String) null);
            interceptedTargetVisitor.putField(proxyType, FIELD_BEAN_RESOLUTION_CONTEXT, Type.getType(BeanResolutionContext.class));
            interceptedTargetVisitor.goTo(synchronizationEnd);
            // Catch body
            interceptedTargetVisitor.visitLabel(catchLabel);
            interceptedTargetVisitor.loadThis();
            interceptedTargetVisitor.monitorExit();
            interceptedTargetVisitor.throwException();
            // Synchronization end label
            interceptedTargetVisitor.visitLabel(synchronizationEnd);
            interceptedTargetVisitor.loadThis();
            interceptedTargetVisitor.monitorExit();
            interceptedTargetVisitor.goTo(returnLabel);
            // Return label just loads and returns value
            interceptedTargetVisitor.visitLabel(returnLabel);
            interceptedTargetVisitor.loadThis();
            interceptedTargetVisitor.getField(proxyType, FIELD_TARGET, targetType);
            interceptedTargetVisitor.returnValue();
        } else {
            pushResolveLazyProxyTargetBean(interceptedTargetVisitor, targetType);
            interceptedTargetVisitor.returnValue();
        }
    } else {
        int localRef = -1;
        Label l1 = null;
        Label l2 = null;
        if (hotswap) {
            Label l0 = new Label();
            l1 = new Label();
            l2 = new Label();
            interceptedTargetVisitor.visitTryCatchBlock(l0, l1, l2, null);
            // add read lock
            readLock(interceptedTargetVisitor);
            interceptedTargetVisitor.visitLabel(l0);
        }
        interceptedTargetVisitor.loadThis();
        interceptedTargetVisitor.getField(proxyType, FIELD_TARGET, targetType);
        if (hotswap) {
            // release read lock
            localRef = interceptedTargetVisitor.newLocal(targetType);
            interceptedTargetVisitor.storeLocal(localRef);
            interceptedTargetVisitor.visitLabel(l1);
            readUnlock(interceptedTargetVisitor);
            interceptedTargetVisitor.loadLocal(localRef);
        }
        interceptedTargetVisitor.returnValue();
        if (localRef > -1) {
            interceptedTargetVisitor.visitLabel(l2);
            // release read lock in finally
            int var = interceptedTargetVisitor.newLocal(targetType);
            interceptedTargetVisitor.storeLocal(var);
            readUnlock(interceptedTargetVisitor);
            interceptedTargetVisitor.loadLocal(var);
            interceptedTargetVisitor.throwException();
        }
    }
    interceptedTargetVisitor.visitMaxs(1, 2);
    interceptedTargetVisitor.visitEnd();
}
Also used : Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) BeanResolutionContext(io.micronaut.context.BeanResolutionContext)

Example 3 with BeanResolutionContext

use of io.micronaut.context.BeanResolutionContext in project micronaut-core by micronaut-projects.

the class AopProxyWriter method initConstructor.

private void initConstructor(MethodElement constructor) {
    final ClassElement interceptorList = ClassElement.of(List.class, AnnotationMetadata.EMPTY_METADATA, Collections.singletonMap("E", ClassElement.of(BeanRegistration.class, AnnotationMetadata.EMPTY_METADATA, Collections.singletonMap("T", ClassElement.of(Interceptor.class)))));
    this.interceptorParameter = ParameterElement.of(interceptorList, "$interceptors");
    this.qualifierParameter = ParameterElement.of(Qualifier.class, "$qualifier");
    ClassElement proxyClass = ClassElement.of(proxyType.getClassName());
    ParameterElement[] constructorParameters = constructor.getParameters();
    List<ParameterElement> newConstructorParameters = new ArrayList<>(constructorParameters.length + 4);
    newConstructorParameters.addAll(Arrays.asList(constructorParameters));
    newConstructorParameters.add(ParameterElement.of(BeanResolutionContext.class, "$beanResolutionContext"));
    newConstructorParameters.add(ParameterElement.of(BeanContext.class, "$beanContext"));
    newConstructorParameters.add(qualifierParameter);
    newConstructorParameters.add(interceptorParameter);
    this.newConstructor = MethodElement.of(proxyClass, constructor.getAnnotationMetadata(), proxyClass, proxyClass, "<init>", newConstructorParameters.toArray(new ParameterElement[0]));
    this.beanResolutionContextArgumentIndex = constructorParameters.length;
    this.beanContextArgumentIndex = constructorParameters.length + 1;
    this.qualifierIndex = constructorParameters.length + 2;
    this.interceptorArgumentIndex = constructorParameters.length + 3;
}
Also used : BeanContext(io.micronaut.context.BeanContext) DefaultBeanContext(io.micronaut.context.DefaultBeanContext) ArrayList(java.util.ArrayList) ClassElement(io.micronaut.inject.ast.ClassElement) Qualifier(io.micronaut.context.Qualifier) ParameterElement(io.micronaut.inject.ast.ParameterElement) BeanResolutionContext(io.micronaut.context.BeanResolutionContext)

Example 4 with BeanResolutionContext

use of io.micronaut.context.BeanResolutionContext in project micronaut-core by micronaut-projects.

the class BeanCreationException method resolveRootBeanDefinition.

private BeanType resolveRootBeanDefinition(BeanResolutionContext resolutionContext) {
    BeanType rootBeanType = null;
    if (resolutionContext != null) {
        BeanResolutionContext.Path path = resolutionContext.getPath();
        if (!path.isEmpty()) {
            BeanResolutionContext.Segment segment = path.peek();
            rootBeanType = segment.getDeclaringType();
        } else {
            rootBeanType = resolutionContext.getRootDefinition();
        }
    }
    return rootBeanType;
}
Also used : BeanType(io.micronaut.inject.BeanType) BeanResolutionContext(io.micronaut.context.BeanResolutionContext)

Example 5 with BeanResolutionContext

use of io.micronaut.context.BeanResolutionContext in project micronaut-core by micronaut-projects.

the class MessageUtils method buildMessage.

/**
 * Builds an appropriate error message.
 *
 * @param resolutionContext The resolution context
 * @param message           The message
 * @return The message
 */
static String buildMessage(BeanResolutionContext resolutionContext, String message) {
    BeanResolutionContext.Path path = resolutionContext.getPath();
    BeanDefinition declaringType;
    boolean hasPath = !path.isEmpty();
    if (hasPath) {
        BeanResolutionContext.Segment segment = path.peek();
        declaringType = segment.getDeclaringType();
    } else {
        declaringType = resolutionContext.getRootDefinition();
    }
    String ls = CachedEnvironment.getProperty("line.separator");
    StringBuilder builder = new StringBuilder("Error instantiating bean of type  [");
    builder.append(declaringType.getName()).append("]").append(ls).append(ls);
    if (message != null) {
        builder.append("Message: ").append(message).append(ls);
    }
    if (hasPath) {
        String pathString = path.toString();
        builder.append("Path Taken: ").append(pathString);
    }
    return builder.toString();
}
Also used : BeanDefinition(io.micronaut.inject.BeanDefinition) BeanResolutionContext(io.micronaut.context.BeanResolutionContext) AbstractBeanResolutionContext(io.micronaut.context.AbstractBeanResolutionContext)

Aggregations

BeanResolutionContext (io.micronaut.context.BeanResolutionContext)5 Qualifier (io.micronaut.context.Qualifier)2 AbstractBeanResolutionContext (io.micronaut.context.AbstractBeanResolutionContext)1 BeanContext (io.micronaut.context.BeanContext)1 DefaultBeanContext (io.micronaut.context.DefaultBeanContext)1 DisabledBeanException (io.micronaut.context.exceptions.DisabledBeanException)1 NoSuchBeanException (io.micronaut.context.exceptions.NoSuchBeanException)1 Named (io.micronaut.core.naming.Named)1 Argument (io.micronaut.core.type.Argument)1 ArgumentCoercible (io.micronaut.core.type.ArgumentCoercible)1 BeanDefinition (io.micronaut.inject.BeanDefinition)1 BeanType (io.micronaut.inject.BeanType)1 ClassElement (io.micronaut.inject.ast.ClassElement)1 ParameterElement (io.micronaut.inject.ast.ParameterElement)1 AnyQualifier (io.micronaut.inject.qualifiers.AnyQualifier)1 ArrayList (java.util.ArrayList)1 Label (org.objectweb.asm.Label)1 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)1