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");
}
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();
}
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;
}
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;
}
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();
}
Aggregations