|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.eclipse.odi.cdi; |
| 17 | + |
| 18 | +import io.micronaut.context.ApplicationContextBuilder; |
| 19 | +import io.micronaut.context.ApplicationContextConfigurer; |
| 20 | +import io.micronaut.context.BeanRegistration; |
| 21 | +import io.micronaut.context.BeanResolutionContext; |
| 22 | +import io.micronaut.context.BeanResolutionCustomizer; |
| 23 | +import io.micronaut.context.annotation.ContextConfigurer; |
| 24 | +import io.micronaut.core.reflect.ReflectionUtils; |
| 25 | +import io.micronaut.core.type.Argument; |
| 26 | +import io.micronaut.inject.BeanDefinition; |
| 27 | +import io.micronaut.inject.QualifiedBeanType; |
| 28 | +import jakarta.enterprise.context.NormalScope; |
| 29 | +import jakarta.enterprise.inject.TransientReference; |
| 30 | + |
| 31 | +import java.lang.reflect.Type; |
| 32 | +import java.util.Optional; |
| 33 | +import java.util.Set; |
| 34 | + |
| 35 | +/** |
| 36 | + * ODI specific {@link ApplicationContextConfigurer}. |
| 37 | + */ |
| 38 | +@ContextConfigurer |
| 39 | +public final class OdiApplicationContextConfigurer implements ApplicationContextConfigurer { |
| 40 | + @Override |
| 41 | + public void configure(ApplicationContextBuilder builder) { |
| 42 | + builder.allowEmptyProviders(true) |
| 43 | + .customScopeRegistry(OdiCustomScopeRegistry::new) |
| 44 | + .beanResolutionCustomizer(new BeanResolutionCustomizer() { |
| 45 | + @Override |
| 46 | + public boolean shouldResolveArrayAsBean(Argument<?> injectionPoint) { |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public Argument<?> resolveBeanLookupArgument(Argument<?> beanType) { |
| 52 | + Class<?> type = beanType.getType(); |
| 53 | + if (type.isPrimitive()) { |
| 54 | + Class<?> wrapperType = ReflectionUtils.getWrapperType(type); |
| 55 | + return Argument.of(wrapperType, beanType.getName(), beanType.getAnnotationMetadata(), beanType.getTypeParameters()); |
| 56 | + } |
| 57 | + return beanType; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Optional<?> resolveNullBean(Argument<?> requestedBeanType, Argument<?> resolvedBeanType, BeanDefinition<?> beanDefinition) { |
| 62 | + Class<?> requestedType = requestedBeanType.getType(); |
| 63 | + if (requestedType.isPrimitive() && resolvedBeanType.getType() == ReflectionUtils.getWrapperType(requestedType)) { |
| 64 | + return Optional.of(primitiveDefaultValue(requestedType)); |
| 65 | + } |
| 66 | + return Optional.empty(); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public boolean shouldDestroyDependentBeanAfterResolution(BeanResolutionContext resolutionContext, BeanRegistration<?> beanRegistration) { |
| 71 | + return resolutionContext.getPath().currentSegment() |
| 72 | + .map(segment -> segment.getArgument().getAnnotationMetadata().hasAnnotation(TransientReference.class)) |
| 73 | + .orElse(false); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean shouldInitializeBean(BeanResolutionContext resolutionContext, BeanDefinition<?> beanDefinition, Object bean) { |
| 78 | + return !(beanDefinition.isProxy() && beanDefinition.getAnnotationMetadata().hasStereotype(NormalScope.class)); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean shouldPreserveLazyProxyTargetResolutionPath(BeanResolutionContext resolutionContext, BeanDefinition<?> proxyBeanDefinition) { |
| 83 | + return !proxyBeanDefinition.getAnnotationMetadata().hasStereotype(NormalScope.class); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean isCandidateBean(Argument<?> beanType, QualifiedBeanType<?> candidate) { |
| 88 | + Type requiredType = OdiTypeUtils.getRequiredType(beanType); |
| 89 | + if (requiredType != null) { |
| 90 | + Set<Type> beanTypes = OdiTypeUtils.getBeanTypes(candidate.getAnnotationMetadata(), candidate.getBeanType()); |
| 91 | + if (!beanTypes.isEmpty()) { |
| 92 | + return OdiTypeUtils.matchesBeanType(requiredType, beanTypes); |
| 93 | + } |
| 94 | + } |
| 95 | + return candidate.isCandidateBean(beanType); |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + private static Object primitiveDefaultValue(Class<?> type) { |
| 101 | + return switch (type.getName()) { |
| 102 | + case "boolean" -> false; |
| 103 | + case "byte" -> (byte) 0; |
| 104 | + case "short" -> (short) 0; |
| 105 | + case "int" -> 0; |
| 106 | + case "long" -> 0L; |
| 107 | + case "float" -> 0.0f; |
| 108 | + case "double" -> 0.0d; |
| 109 | + case "char" -> '\0'; |
| 110 | + default -> throw new IllegalArgumentException("Not a primitive type: " + type.getName()); |
| 111 | + }; |
| 112 | + } |
| 113 | +} |
0 commit comments