Search in sources :

Example 1 with EachProperty

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

the class DefaultApplicationContext method transformIterables.

@Override
protected <T> Collection<BeanDefinition<T>> transformIterables(BeanResolutionContext resolutionContext, Collection<BeanDefinition<T>> candidates, boolean filterProxied) {
    if (!candidates.isEmpty()) {
        List<BeanDefinition<T>> transformedCandidates = new ArrayList<>();
        for (BeanDefinition candidate : candidates) {
            if (candidate.isIterable()) {
                if (candidate.hasDeclaredStereotype(EachProperty.class)) {
                    boolean isList = candidate.booleanValue(EachProperty.class, "list").orElse(false);
                    String property = candidate.stringValue(ConfigurationReader.class, "prefix").map(prefix -> prefix.substring(0, prefix.length() - (isList ? 3 : 2))).orElseGet(() -> candidate.stringValue(EachProperty.class).orElse(null));
                    String primaryPrefix = candidate.stringValue(EachProperty.class, "primary").orElse(null);
                    if (StringUtils.isNotEmpty(property)) {
                        if (isList) {
                            List entries = getEnvironment().getProperty(property, List.class, Collections.emptyList());
                            if (!entries.isEmpty()) {
                                for (int i = 0; i < entries.size(); i++) {
                                    if (entries.get(i) != null) {
                                        BeanDefinitionDelegate delegate = BeanDefinitionDelegate.create(candidate);
                                        String index = String.valueOf(i);
                                        if (primaryPrefix != null && primaryPrefix.equals(index)) {
                                            delegate.put(BeanDefinitionDelegate.PRIMARY_ATTRIBUTE, true);
                                        }
                                        delegate.put("Array", index);
                                        delegate.put(Named.class.getName(), index);
                                        if (delegate.isEnabled(this, resolutionContext)) {
                                            transformedCandidates.add(delegate);
                                        }
                                    }
                                }
                            }
                        } else {
                            Collection<String> propertyEntries = getEnvironment().getPropertyEntries(property);
                            if (!propertyEntries.isEmpty()) {
                                for (String key : propertyEntries) {
                                    BeanDefinitionDelegate delegate = BeanDefinitionDelegate.create(candidate);
                                    if (primaryPrefix != null && primaryPrefix.equals(key)) {
                                        delegate.put(BeanDefinitionDelegate.PRIMARY_ATTRIBUTE, true);
                                    }
                                    delegate.put(EachProperty.class.getName(), delegate.getBeanType());
                                    delegate.put(Named.class.getName(), key);
                                    if (delegate.isEnabled(this, resolutionContext)) {
                                        transformedCandidates.add(delegate);
                                    }
                                }
                            }
                        }
                    } else {
                        throw new IllegalArgumentException("Blank value specified to @Each property for bean: " + candidate);
                    }
                } else if (candidate.hasDeclaredStereotype(EachBean.class)) {
                    Class dependentType = candidate.classValue(EachBean.class).orElse(null);
                    if (dependentType == null) {
                        transformedCandidates.add(candidate);
                        continue;
                    }
                    Collection<BeanDefinition> dependentCandidates = findBeanCandidates(resolutionContext, Argument.of(dependentType), filterProxied, null);
                    if (!dependentCandidates.isEmpty()) {
                        for (BeanDefinition dependentCandidate : dependentCandidates) {
                            BeanDefinitionDelegate<?> delegate = BeanDefinitionDelegate.create(candidate);
                            Optional<Qualifier> optional;
                            if (dependentCandidate instanceof BeanDefinitionDelegate) {
                                BeanDefinitionDelegate<?> parentDelegate = (BeanDefinitionDelegate) dependentCandidate;
                                optional = parentDelegate.get(Named.class.getName(), String.class).map(Qualifiers::byName);
                            } else {
                                Optional<String> qualifierName = dependentCandidate.getAnnotationNameByStereotype(AnnotationUtil.QUALIFIER);
                                optional = qualifierName.map(name -> Qualifiers.byAnnotation(dependentCandidate, name));
                            }
                            if (dependentCandidate.isPrimary()) {
                                delegate.put(BeanDefinitionDelegate.PRIMARY_ATTRIBUTE, true);
                            }
                            optional.ifPresent(qualifier -> {
                                String qualifierKey = AnnotationUtil.QUALIFIER;
                                Argument<?>[] arguments = candidate.getConstructor().getArguments();
                                for (Argument<?> argument : arguments) {
                                    Class<?> argumentType = argument.getType();
                                    if (argumentType.equals(dependentType)) {
                                        Map<? extends Argument<?>, Qualifier> qualifedArg = Collections.singletonMap(argument, qualifier);
                                        delegate.put(qualifierKey, qualifedArg);
                                        break;
                                    }
                                }
                                if (qualifier instanceof Named) {
                                    delegate.put(Named.class.getName(), ((Named) qualifier).getName());
                                }
                                if (delegate.isEnabled(this, resolutionContext)) {
                                    transformedCandidates.add((BeanDefinition<T>) delegate);
                                }
                            });
                        }
                    }
                }
            } else {
                if (candidate.hasStereotype(ConfigurationReader.class)) {
                    final String prefix = candidate.stringValue(ConfigurationReader.class, "prefix").orElse(null);
                    if (prefix != null) {
                        int mapIndex = prefix.indexOf("*");
                        int arrIndex = prefix.indexOf("[*]");
                        boolean isList = arrIndex > -1;
                        boolean isMap = mapIndex > -1;
                        if (isList || isMap) {
                            int startIndex = isList ? arrIndex : mapIndex;
                            String eachProperty = prefix.substring(0, startIndex);
                            if (eachProperty.endsWith(".")) {
                                eachProperty = eachProperty.substring(0, eachProperty.length() - 1);
                            }
                            if (StringUtils.isNotEmpty(eachProperty)) {
                                if (isList) {
                                    List entries = getProperty(eachProperty, List.class, Collections.emptyList());
                                    if (!entries.isEmpty()) {
                                        for (int i = 0; i < entries.size(); i++) {
                                            if (entries.get(i) != null) {
                                                BeanDefinitionDelegate delegate = BeanDefinitionDelegate.create(candidate);
                                                String index = String.valueOf(i);
                                                delegate.put("Array", index);
                                                delegate.put(Named.class.getName(), index);
                                                if (delegate.isEnabled(this, resolutionContext) && containsProperties(prefix.replace("*", index))) {
                                                    transformedCandidates.add(delegate);
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    Map entries = getProperty(eachProperty, Map.class, Collections.emptyMap());
                                    if (!entries.isEmpty()) {
                                        for (Object key : entries.keySet()) {
                                            BeanDefinitionDelegate delegate = BeanDefinitionDelegate.create(candidate);
                                            delegate.put(EachProperty.class.getName(), delegate.getBeanType());
                                            delegate.put(Named.class.getName(), key.toString());
                                            if (delegate.isEnabled(this, resolutionContext) && containsProperties(prefix.replace("*", key.toString()))) {
                                                transformedCandidates.add(delegate);
                                            }
                                        }
                                    }
                                }
                            } else {
                                throw new IllegalArgumentException("Blank value specified to @Each property for bean: " + candidate);
                            }
                        } else {
                            transformedCandidates.add(candidate);
                        }
                    } else {
                        transformedCandidates.add(candidate);
                    }
                } else {
                    transformedCandidates.add(candidate);
                }
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finalized bean definitions candidates: {}", transformedCandidates);
        }
        return transformedCandidates;
    }
    return candidates;
}
Also used : Environment(io.micronaut.context.env.Environment) DefaultEnvironment(io.micronaut.context.env.DefaultEnvironment) Arrays(java.util.Arrays) StringConvention(io.micronaut.core.naming.conventions.StringConvention) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) ArrayList(java.util.ArrayList) EachBean(io.micronaut.context.annotation.EachBean) BootstrapContextCompatible(io.micronaut.context.annotation.BootstrapContextCompatible) Nullable(io.micronaut.core.annotation.Nullable) Map(java.util.Map) ConfigurationReader(io.micronaut.context.annotation.ConfigurationReader) ArgumentUtils(io.micronaut.core.util.ArgumentUtils) Argument(io.micronaut.core.type.Argument) BootstrapPropertySourceLocator(io.micronaut.context.env.BootstrapPropertySourceLocator) ConversionService(io.micronaut.core.convert.ConversionService) CachedEnvironment(io.micronaut.context.env.CachedEnvironment) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) PropertySource(io.micronaut.context.env.PropertySource) Iterator(java.util.Iterator) TypeConverter(io.micronaut.core.convert.TypeConverter) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) Collectors(java.util.stream.Collectors) BeanConfiguration(io.micronaut.inject.BeanConfiguration) NonNull(io.micronaut.core.annotation.NonNull) StringUtils(io.micronaut.core.util.StringUtils) ClassPathResourceLoader(io.micronaut.core.io.scan.ClassPathResourceLoader) List(java.util.List) AnnotationUtil(io.micronaut.core.annotation.AnnotationUtil) Named(io.micronaut.core.naming.Named) Optional(java.util.Optional) EachProperty(io.micronaut.context.annotation.EachProperty) TypeConverterRegistrar(io.micronaut.core.convert.TypeConverterRegistrar) BeanDefinition(io.micronaut.inject.BeanDefinition) BeanDefinitionReference(io.micronaut.inject.BeanDefinitionReference) Collections(java.util.Collections) Named(io.micronaut.core.naming.Named) Optional(java.util.Optional) Argument(io.micronaut.core.type.Argument) EachBean(io.micronaut.context.annotation.EachBean) ArrayList(java.util.ArrayList) BeanDefinition(io.micronaut.inject.BeanDefinition) EachProperty(io.micronaut.context.annotation.EachProperty) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) Map(java.util.Map) ConfigurationReader(io.micronaut.context.annotation.ConfigurationReader)

Aggregations

BootstrapContextCompatible (io.micronaut.context.annotation.BootstrapContextCompatible)1 ConfigurationReader (io.micronaut.context.annotation.ConfigurationReader)1 EachBean (io.micronaut.context.annotation.EachBean)1 EachProperty (io.micronaut.context.annotation.EachProperty)1 BootstrapPropertySourceLocator (io.micronaut.context.env.BootstrapPropertySourceLocator)1 CachedEnvironment (io.micronaut.context.env.CachedEnvironment)1 DefaultEnvironment (io.micronaut.context.env.DefaultEnvironment)1 Environment (io.micronaut.context.env.Environment)1 PropertySource (io.micronaut.context.env.PropertySource)1 ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)1 AnnotationUtil (io.micronaut.core.annotation.AnnotationUtil)1 NonNull (io.micronaut.core.annotation.NonNull)1 Nullable (io.micronaut.core.annotation.Nullable)1 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)1 ConversionService (io.micronaut.core.convert.ConversionService)1 TypeConverter (io.micronaut.core.convert.TypeConverter)1 TypeConverterRegistrar (io.micronaut.core.convert.TypeConverterRegistrar)1 ClassPathResourceLoader (io.micronaut.core.io.scan.ClassPathResourceLoader)1 Named (io.micronaut.core.naming.Named)1 StringConvention (io.micronaut.core.naming.conventions.StringConvention)1