Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix check for primitive return types #96

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ The individual methods may have arbitrary classes as return types, in particular

**Note**: Primitives are not supported as return types as they cannot be null and therefore cannot express a missing configuration value.

> [!CAUTION]
> Primitives are not supported as return types as they cannot be null and therefore cannot express a missing configuration value.
> If you use Baigan with Kotlin, it means you need to use nullable primitive types, e.g. `Int?` instead of `Int`.

The above example code enables the application to inject _ExpressFeature_ spring bean into any other Spring bean:

```Java
Expand Down
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 @@ -32,13 +32,15 @@
import org.zalando.baigan.annotation.BaiganConfig;
import org.zalando.baigan.annotation.ConfigurationServiceScan;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toMap;
import static org.zalando.baigan.proxy.ProxyUtils.createKey;
Expand Down Expand Up @@ -112,15 +114,25 @@ 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));
Map<String, Type> configTypesByKey = baiganConfigClasses.stream()
.flatMap(clazz -> Stream.of(clazz.getMethods()).map(method -> mapToConfigType(clazz, method)))
.collect(toMap(c -> c.key, c -> c.type));
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(BaiganConfigClasses.class);
beanDefinition.getPropertyValues().add("configTypesByKey", configTypesByKey);
registry.registerBeanDefinition("baiganConfigClasses", beanDefinition);
}

private ConfigType mapToConfigType(final Class<?> clazz, final Method method) {
final String key = createKey(clazz, method);
final Class<?> returnType = method.getReturnType();
if (returnType.isPrimitive()) {
throw new IllegalArgumentException("Config " + key + " has an illegal return type " + returnType +
". Primitives are not supported as return type!");
}
return new ConfigType(key, method.getGenericReturnType());
}

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();
}