Search in sources :

Example 1 with UsedByGeneratedCode

use of io.micronaut.core.annotation.UsedByGeneratedCode in project micronaut-core by micronaut-projects.

the class AbstractInitializableBeanDefinition method getBeanForMethodArgument.

/**
 * Obtains a bean definition for the method at the given index and the argument at the given index
 * <p>
 * Warning: this method is used by internal generated code and should not be called by user code.
 *
 * @param resolutionContext The resolution context
 * @param context           The context
 * @param methodIndex       The method index
 * @param argIndex          The argument index
 * @param qualifier         The qualifier
 * @return The resolved bean
 */
@Internal
@SuppressWarnings("WeakerAccess")
@UsedByGeneratedCode
protected final Object getBeanForMethodArgument(BeanResolutionContext resolutionContext, BeanContext context, int methodIndex, int argIndex, Qualifier qualifier) {
    MethodReference methodRef = methodInjection[methodIndex];
    Argument argument = resolveArgument(context, argIndex, methodRef.arguments);
    try (BeanResolutionContext.Path ignored = resolutionContext.getPath().pushMethodArgumentResolve(this, methodRef.methodName, argument, methodRef.arguments, methodRef.requiresReflection)) {
        return resolveBean(resolutionContext, context, argument, qualifier, true);
    }
}
Also used : DefaultArgument(io.micronaut.core.type.DefaultArgument) Argument(io.micronaut.core.type.Argument) Internal(io.micronaut.core.annotation.Internal) UsedByGeneratedCode(io.micronaut.core.annotation.UsedByGeneratedCode)

Example 2 with UsedByGeneratedCode

use of io.micronaut.core.annotation.UsedByGeneratedCode in project micronaut-core by micronaut-projects.

the class AbstractInitializableBeanDefinition method getPropertyPlaceholderValueForConstructorArgument.

/**
 * Obtains a property value for a bean definition for a constructor at the given index
 * <p>
 * Warning: this method is used by internal generated code and should not be called by user code.
 *
 * @param resolutionContext The resolution context
 * @param context           The context
 * @param argIndex          The argument index
 * @param propertyValue     The property value
 * @return The resolved bean
 */
@Internal
@UsedByGeneratedCode
protected final Object getPropertyPlaceholderValueForConstructorArgument(BeanResolutionContext resolutionContext, BeanContext context, int argIndex, String propertyValue) {
    MethodReference constructorRef = (MethodReference) constructor;
    Argument<?> argument = constructorRef.arguments[argIndex];
    try (BeanResolutionContext.Path ignored = resolutionContext.getPath().pushConstructorResolve(this, argument)) {
        try {
            Object result = resolvePropertyValue(resolutionContext, context, argument, propertyValue, null, true);
            if (this instanceof ValidatedBeanDefinition) {
                ((ValidatedBeanDefinition) this).validateBeanArgument(resolutionContext, getConstructor(), argument, argIndex, result);
            }
            return result;
        } catch (NoSuchBeanException | BeanInstantiationException e) {
            throw new DependencyInjectionException(resolutionContext, argument, e);
        }
    }
}
Also used : DependencyInjectionException(io.micronaut.context.exceptions.DependencyInjectionException) NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) BeanInstantiationException(io.micronaut.context.exceptions.BeanInstantiationException) ValidatedBeanDefinition(io.micronaut.inject.ValidatedBeanDefinition) Internal(io.micronaut.core.annotation.Internal) UsedByGeneratedCode(io.micronaut.core.annotation.UsedByGeneratedCode)

Example 3 with UsedByGeneratedCode

use of io.micronaut.core.annotation.UsedByGeneratedCode in project micronaut-core by micronaut-projects.

the class AbstractInitializableBeanDefinition method getBeansOfTypeForMethodArgument.

/**
 * Obtains all bean definitions for a method argument at the given index.
 * <p>
 *
 * @param resolutionContext The resolution context
 * @param context           The context
 * @param methodIndex       The method index
 * @param argumentIndex     The argument index
 * @param genericType       The generic type
 * @param qualifier         The qualifier
 * @return The resolved bean
 */
@Internal
@UsedByGeneratedCode
protected final Collection<Object> getBeansOfTypeForMethodArgument(BeanResolutionContext resolutionContext, BeanContext context, int methodIndex, int argumentIndex, Argument genericType, Qualifier qualifier) {
    MethodReference methodRef = methodInjection[methodIndex];
    Argument argument = resolveArgument(context, argumentIndex, methodRef.arguments);
    try (BeanResolutionContext.Path ignored = resolutionContext.getPath().pushMethodArgumentResolve(this, methodRef.methodName, argument, methodRef.arguments, methodRef.requiresReflection)) {
        return resolveBeansOfType(resolutionContext, context, argument, resolveEnvironmentArgument(context, genericType), qualifier);
    }
}
Also used : DefaultArgument(io.micronaut.core.type.DefaultArgument) Argument(io.micronaut.core.type.Argument) Internal(io.micronaut.core.annotation.Internal) UsedByGeneratedCode(io.micronaut.core.annotation.UsedByGeneratedCode)

Example 4 with UsedByGeneratedCode

use of io.micronaut.core.annotation.UsedByGeneratedCode in project micronaut-core by micronaut-projects.

the class AbstractInitializableBeanDefinition method getStreamOfTypeForField.

/**
 * Obtains a bean definition for the field at the given index and the argument at the given index
 * <p>
 * Warning: this method is used by internal generated code and should not be called by user code.
 *
 * @param resolutionContext The resolution context
 * @param context           The context
 * @param fieldIndex        The field index
 * @param genericType       The generic type
 * @param qualifier         The qualifier
 * @return The resolved bean
 */
@Internal
@UsedByGeneratedCode
protected final Stream getStreamOfTypeForField(BeanResolutionContext resolutionContext, BeanContext context, int fieldIndex, Argument genericType, Qualifier qualifier) {
    FieldReference fieldRef = fieldInjection[fieldIndex];
    Argument argument = resolveEnvironmentArgument(context, fieldRef.argument);
    try (BeanResolutionContext.Path ignored = resolutionContext.getPath().pushFieldResolve(this, argument, fieldRef.requiresReflection)) {
        return resolveStreamOfType(resolutionContext, context, argument, resolveEnvironmentArgument(context, genericType), qualifier);
    }
}
Also used : DefaultArgument(io.micronaut.core.type.DefaultArgument) Argument(io.micronaut.core.type.Argument) Internal(io.micronaut.core.annotation.Internal) UsedByGeneratedCode(io.micronaut.core.annotation.UsedByGeneratedCode)

Example 5 with UsedByGeneratedCode

use of io.micronaut.core.annotation.UsedByGeneratedCode in project micronaut-core by micronaut-projects.

the class AnnotationMetadataWriter method instantiateNewMetadataHierarchy.

/**
 * Writes out the byte code necessary to instantiate the given {@link AnnotationMetadataHierarchy}.
 *
 * @param owningType       The owning type
 * @param classWriter      The declaring class writer
 * @param generatorAdapter The generator adapter
 * @param hierarchy        The annotation metadata
 * @param defaultsStorage  The annotation defaults
 * @param loadTypeMethods  The generated load type methods
 */
@Internal
@UsedByGeneratedCode
public static void instantiateNewMetadataHierarchy(Type owningType, ClassWriter classWriter, GeneratorAdapter generatorAdapter, AnnotationMetadataHierarchy hierarchy, Map<String, Integer> defaultsStorage, Map<String, GeneratorAdapter> loadTypeMethods) {
    if (hierarchy.isEmpty()) {
        generatorAdapter.getStatic(Type.getType(AnnotationMetadata.class), "EMPTY_METADATA", Type.getType(AnnotationMetadata.class));
        return;
    }
    List<AnnotationMetadata> notEmpty = CollectionUtils.iterableToList(hierarchy).stream().filter(h -> !h.isEmpty()).collect(Collectors.toList());
    if (notEmpty.size() == 1) {
        pushNewAnnotationMetadataOrReference(owningType, classWriter, generatorAdapter, defaultsStorage, loadTypeMethods, notEmpty.get(0));
        return;
    }
    generatorAdapter.visitTypeInsn(NEW, TYPE_DEFAULT_ANNOTATION_METADATA_HIERARCHY.getInternalName());
    generatorAdapter.visitInsn(DUP);
    pushNewArray(generatorAdapter, AnnotationMetadata.class, 2);
    pushStoreInArray(generatorAdapter, 0, 2, () -> {
        final AnnotationMetadata rootMetadata = hierarchy.getRootMetadata();
        pushNewAnnotationMetadataOrReference(owningType, classWriter, generatorAdapter, defaultsStorage, loadTypeMethods, rootMetadata);
    });
    pushStoreInArray(generatorAdapter, 1, 2, () -> {
        final AnnotationMetadata declaredMetadata = hierarchy.getDeclaredMetadata();
        pushNewAnnotationMetadataOrReference(owningType, classWriter, generatorAdapter, defaultsStorage, loadTypeMethods, declaredMetadata);
    });
    // invoke the constructor
    generatorAdapter.invokeConstructor(TYPE_DEFAULT_ANNOTATION_METADATA_HIERARCHY, CONSTRUCTOR_ANNOTATION_METADATA_HIERARCHY);
}
Also used : ClassWriter(org.objectweb.asm.ClassWriter) Arrays(java.util.Arrays) MethodVisitor(org.objectweb.asm.MethodVisitor) Array(java.lang.reflect.Array) ArrayUtils(io.micronaut.core.util.ArrayUtils) Label(org.objectweb.asm.Label) ClassElement(io.micronaut.inject.ast.ClassElement) HashMap(java.util.HashMap) Internal(io.micronaut.core.annotation.Internal) Type(org.objectweb.asm.Type) LinkedHashMap(java.util.LinkedHashMap) Method(org.objectweb.asm.commons.Method) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Map(java.util.Map) AbstractClassFileWriter(io.micronaut.inject.writer.AbstractClassFileWriter) ClassWriterOutputVisitor(io.micronaut.inject.writer.ClassWriterOutputVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) AnnotationClassValue(io.micronaut.core.annotation.AnnotationClassValue) AbstractAnnotationMetadataWriter(io.micronaut.inject.writer.AbstractAnnotationMetadataWriter) OutputStream(java.io.OutputStream) Opcodes(org.objectweb.asm.Opcodes) ClassGenerationException(io.micronaut.inject.writer.ClassGenerationException) Collection(java.util.Collection) UsedByGeneratedCode(io.micronaut.core.annotation.UsedByGeneratedCode) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ReflectionUtils(io.micronaut.core.reflect.ReflectionUtils) Objects(java.util.Objects) List(java.util.List) AnnotationUtil(io.micronaut.core.annotation.AnnotationUtil) CollectionUtils(io.micronaut.core.util.CollectionUtils) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Collections(java.util.Collections) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Internal(io.micronaut.core.annotation.Internal) UsedByGeneratedCode(io.micronaut.core.annotation.UsedByGeneratedCode)

Aggregations

Internal (io.micronaut.core.annotation.Internal)14 UsedByGeneratedCode (io.micronaut.core.annotation.UsedByGeneratedCode)14 Argument (io.micronaut.core.type.Argument)8 DefaultArgument (io.micronaut.core.type.DefaultArgument)8 BeanInstantiationException (io.micronaut.context.exceptions.BeanInstantiationException)3 DependencyInjectionException (io.micronaut.context.exceptions.DependencyInjectionException)3 NoSuchBeanException (io.micronaut.context.exceptions.NoSuchBeanException)3 ValidatedBeanDefinition (io.micronaut.inject.ValidatedBeanDefinition)3 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 BeanRegistration (io.micronaut.context.BeanRegistration)1 DefaultBeanContext (io.micronaut.context.DefaultBeanContext)1 AnnotationClassValue (io.micronaut.core.annotation.AnnotationClassValue)1 AnnotationUtil (io.micronaut.core.annotation.AnnotationUtil)1 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)1 NonNull (io.micronaut.core.annotation.NonNull)1 ReflectionUtils (io.micronaut.core.reflect.ReflectionUtils)1 ArrayUtils (io.micronaut.core.util.ArrayUtils)1 CollectionUtils (io.micronaut.core.util.CollectionUtils)1 PropertyResolver (io.micronaut.core.value.PropertyResolver)1 AbstractEnvironmentAnnotationMetadata (io.micronaut.inject.annotation.AbstractEnvironmentAnnotationMetadata)1