Skip to content

Commit

Permalink
Fix check for primitive return types
Browse files Browse the repository at this point in the history
  • Loading branch information
yuppie-flu committed Dec 24, 2023
1 parent 8c14662 commit 8d82794
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ public class BaiganConfigClasses {
public BaiganConfigClasses() {}

public void setConfigTypesByKey(Map<String, Type> configTypesByKey) {
configTypesByKey.forEach((key, value) -> {
if (value.getClass().isPrimitive()) {
throw new IllegalArgumentException("Config " + key + " has an illegal return type " + value + ". Primitives are not supported as return type.");
}
});
this.configTypesByKey = configTypesByKey;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,22 @@ private void createAndRegisterBeanDefinitions(final Set<String> packages,
Map<String, Type> configTypesByKey = baiganConfigClasses.stream().flatMap(clazz ->
Arrays.stream(clazz.getMethods()).map(method -> new ConfigType(createKey(clazz, method), method.getGenericReturnType()))
).collect(toMap(c -> c.key, c -> c.type));
assertNoPrimitiveTypes(configTypesByKey);
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(BaiganConfigClasses.class);
beanDefinition.getPropertyValues().add("configTypesByKey", configTypesByKey);
registry.registerBeanDefinition("baiganConfigClasses", beanDefinition);
}

private void assertNoPrimitiveTypes(final Map<String, Type> configTypesByKey) {
configTypesByKey.forEach((key, value) -> {
if (value instanceof Class && ((Class<?>)value).isPrimitive()) {
throw new IllegalArgumentException("Config " + key + " has an illegal return type " + value
+ ". Primitives are not supported as return type.");
}
});
}

private Class<?> registerAsBean(final BeanDefinitionRegistry registry, final GenericBeanDefinition genericDefinition) {
try {
final Class<?> interfaceToImplement = genericDefinition.resolveBeanClass(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,17 @@ public void whenNothingAnnotatedWithConfigurationServiceScan_shouldThrowExceptio
when(metaData.getAnnotationAttributes(ConfigurationServiceScan.class.getName())).thenReturn(ImmutableMap.of());
assertThrows(IllegalArgumentException.class, () -> registrar.registerBeanDefinitions(metaData, registry));
}

@Test
public void whenConfigurationContainsPrimitiveTypes_shouldThrowException() {
when(metaData.getAnnotationAttributes(ConfigurationServiceScan.class.getName())).thenReturn(
ImmutableMap.of(
"value", new String[]{"org.zalando.baigan.context.packaged"},
"basePackages", new String[]{},
"basePackageClasses", new Class[]{}
)
);

assertThrows(IllegalArgumentException.class, () -> registrar.registerBeanDefinitions(metaData, registry));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.zalando.baigan.context.packaged;

import org.zalando.baigan.annotation.BaiganConfig;

@BaiganConfig
public interface PrimitiveTypeConfig {
boolean enabled();
}

0 comments on commit 8d82794

Please sign in to comment.