Skip to content

Commit a2f721a

Browse files
committed
Use context configurer for ODI defaults
1 parent b07e859 commit a2f721a

4 files changed

Lines changed: 128 additions & 83 deletions

File tree

cdi/src/main/java/org/eclipse/odi/cdi/OdiApplicationContextBuilder.java

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,7 @@
1515
*/
1616
package org.eclipse.odi.cdi;
1717

18-
import io.micronaut.context.BeanResolutionCustomizer;
19-
import io.micronaut.context.BeanRegistration;
20-
import io.micronaut.context.BeanResolutionContext;
2118
import io.micronaut.context.DefaultApplicationContextBuilder;
22-
import io.micronaut.core.reflect.ReflectionUtils;
23-
import io.micronaut.core.type.Argument;
24-
import io.micronaut.inject.BeanDefinition;
25-
import io.micronaut.inject.QualifiedBeanType;
26-
import jakarta.enterprise.context.NormalScope;
27-
import jakarta.enterprise.inject.TransientReference;
28-
29-
import java.lang.reflect.Type;
30-
import java.util.Optional;
31-
import java.util.Set;
3219

3320
/**
3421
* ODI specific {@link DefaultApplicationContextBuilder}.
@@ -37,75 +24,5 @@ public final class OdiApplicationContextBuilder extends DefaultApplicationContex
3724
public OdiApplicationContextBuilder() {
3825
deduceEnvironment(false);
3926
banner(false);
40-
allowEmptyProviders(true);
41-
customScopeRegistry(OdiCustomScopeRegistry::new);
42-
beanResolutionCustomizer(new BeanResolutionCustomizer() {
43-
@Override
44-
public boolean shouldResolveArrayAsBean(Argument<?> injectionPoint) {
45-
return true;
46-
}
47-
48-
@Override
49-
public Argument<?> resolveBeanLookupArgument(Argument<?> beanType) {
50-
Class<?> type = beanType.getType();
51-
if (type.isPrimitive()) {
52-
Class<?> wrapperType = ReflectionUtils.getWrapperType(type);
53-
return Argument.of(wrapperType, beanType.getName(), beanType.getAnnotationMetadata(), beanType.getTypeParameters());
54-
}
55-
return beanType;
56-
}
57-
58-
@Override
59-
public Optional<?> resolveNullBean(Argument<?> requestedBeanType, Argument<?> resolvedBeanType, BeanDefinition<?> beanDefinition) {
60-
Class<?> requestedType = requestedBeanType.getType();
61-
if (requestedType.isPrimitive() && resolvedBeanType.getType() == ReflectionUtils.getWrapperType(requestedType)) {
62-
return Optional.of(primitiveDefaultValue(requestedType));
63-
}
64-
return Optional.empty();
65-
}
66-
67-
@Override
68-
public boolean shouldDestroyDependentBeanAfterResolution(BeanResolutionContext resolutionContext, BeanRegistration<?> beanRegistration) {
69-
return resolutionContext.getPath().currentSegment()
70-
.map(segment -> segment.getArgument().getAnnotationMetadata().hasAnnotation(TransientReference.class))
71-
.orElse(false);
72-
}
73-
74-
@Override
75-
public boolean shouldInitializeBean(BeanResolutionContext resolutionContext, BeanDefinition<?> beanDefinition, Object bean) {
76-
return !(beanDefinition.isProxy() && beanDefinition.getAnnotationMetadata().hasStereotype(NormalScope.class));
77-
}
78-
79-
@Override
80-
public boolean shouldPreserveLazyProxyTargetResolutionPath(BeanResolutionContext resolutionContext, BeanDefinition<?> proxyBeanDefinition) {
81-
return !proxyBeanDefinition.getAnnotationMetadata().hasStereotype(NormalScope.class);
82-
}
83-
84-
@Override
85-
public boolean isCandidateBean(Argument<?> beanType, QualifiedBeanType<?> candidate) {
86-
Type requiredType = OdiTypeUtils.getRequiredType(beanType);
87-
if (requiredType != null) {
88-
Set<Type> beanTypes = OdiTypeUtils.getBeanTypes(candidate.getAnnotationMetadata(), candidate.getBeanType());
89-
if (!beanTypes.isEmpty()) {
90-
return OdiTypeUtils.matchesBeanType(requiredType, beanTypes);
91-
}
92-
}
93-
return candidate.isCandidateBean(beanType);
94-
}
95-
});
96-
}
97-
98-
private static Object primitiveDefaultValue(Class<?> type) {
99-
return switch (type.getName()) {
100-
case "boolean" -> false;
101-
case "byte" -> (byte) 0;
102-
case "short" -> (short) 0;
103-
case "int" -> 0;
104-
case "long" -> 0L;
105-
case "float" -> 0.0f;
106-
case "double" -> 0.0d;
107-
case "char" -> '\0';
108-
default -> throw new IllegalArgumentException("Not a primitive type: " + type.getName());
109-
};
11027
}
11128
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.eclipse.odi.cdi.OdiApplicationContextConfigurer

cdi/src/test/java/org/eclipse/odi/cdi/OdiSeContainerInitializerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
package org.eclipse.odi.cdi;
1818

19+
import io.micronaut.context.ApplicationContext;
20+
import io.micronaut.context.BeanContextConfiguration;
21+
import io.micronaut.context.BeanResolutionCustomizer;
22+
import io.micronaut.core.type.Argument;
1923
import jakarta.enterprise.inject.Instance;
2024
import jakarta.enterprise.inject.literal.NamedLiteral;
2125
import jakarta.enterprise.inject.se.SeContainer;
@@ -36,6 +40,16 @@
3640

3741
public class OdiSeContainerInitializerTest {
3842

43+
@Test
44+
void testOdiContextConfigurationIsAppliedAutomatically() {
45+
BeanContextConfiguration configuration = (BeanContextConfiguration) ApplicationContext.builder();
46+
BeanResolutionCustomizer beanResolutionCustomizer = configuration.beanResolutionCustomizer();
47+
48+
assertTrue(configuration.isAllowEmptyProviders());
49+
assertNotNull(configuration.customScopeRegistryFactory());
50+
assertTrue(beanResolutionCustomizer.shouldResolveArrayAsBean(Argument.of(String[].class)));
51+
}
52+
3953
@Test
4054
void testSeContainerInitializer() {
4155
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {

0 commit comments

Comments
 (0)