Skip to content

Commit

Permalink
Allow injection of entire interface hierarchy when using @ConfigMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jul 8, 2021
1 parent 310964a commit 0dc3171
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jboss.jandex.ClassType;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.Type;
import org.jboss.jandex.Type.Kind;
Expand Down Expand Up @@ -80,6 +81,7 @@ public class ConfigBuildStep {
private static final DotName LIST_NAME = DotName.createSimple(List.class.getName());
private static final DotName SUPPLIER_NAME = DotName.createSimple(Supplier.class.getName());
private static final DotName CONFIG_VALUE_NAME = DotName.createSimple(io.smallrye.config.ConfigValue.class.getName());
public static final AnnotationInstance[] EMPTY_ANNOTATION_INSTANCES = {};

@BuildStep
void additionalBeans(BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
Expand Down Expand Up @@ -326,6 +328,7 @@ void generateConfigClasses(
void beanConfigClasses(
List<ConfigClassBuildItem> configClasses,
BeanRegistrationPhaseBuildItem beanRegistrationPhase,
CombinedIndexBuildItem combinedIndex,
BuildProducer<BeanConfiguratorBuildItem> beanConfigurationRegistry) {

for (ConfigClassBuildItem configClass : configClasses) {
Expand All @@ -340,17 +343,53 @@ void beanConfigClasses(
new AnnotationValue[] { createStringValue("prefix", configClass.getPrefix()) }));
}

collectTypes(combinedIndex, configClass);

beanConfigurationRegistry.produce(new BeanConfiguratorBuildItem(
beanRegistrationPhase.getContext()
.configure(configClass.getConfigClass())
.types(configClass.getConfigClass())
.qualifiers(qualifiers.toArray(new AnnotationInstance[] {}))
.types(collectTypes(combinedIndex, configClass))
.qualifiers(qualifiers.toArray(EMPTY_ANNOTATION_INSTANCES))
.creator(ConfigMappingCreator.class)
.param("type", configClass.getConfigClass())
.param("prefix", configClass.getPrefix())));
}
}

private Type[] collectTypes(CombinedIndexBuildItem combinedIndex, ConfigClassBuildItem configClass) {
IndexView index = combinedIndex.getIndex();
DotName configIfaceName = DotName.createSimple(configClass.getConfigClass().getName());
ClassInfo configIfaceInfo = index.getClassByName(configIfaceName);
if ((configIfaceInfo == null) || configIfaceInfo.interfaceNames().isEmpty()) {
return new Type[] { Type.create(configIfaceName, Kind.CLASS) };
}

Set<DotName> allIfaces = new HashSet<>();
allIfaces.add(configIfaceName);
collectInterfacesRec(configIfaceInfo, index, allIfaces);
Type[] result = new Type[allIfaces.size()];
int i = 0;
for (DotName iface : allIfaces) {
result[i++] = Type.create(iface, Kind.CLASS);
}
return result;
}

private static void collectInterfacesRec(ClassInfo current, IndexView index, Set<DotName> result) {
List<DotName> interfaces = current.interfaceNames();
if (interfaces.isEmpty()) {
return;
}
for (DotName iface : interfaces) {
ClassInfo classByName = index.getClassByName(iface);
if (classByName == null) {
continue; // just ignore this type
}
result.add(iface);
collectInterfacesRec(classByName, index, result);
}
}

@BuildStep
@Record(RUNTIME_INIT)
void registerConfigClasses(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
Expand Down Expand Up @@ -60,7 +61,10 @@ public class ConfigMappingTest {
"override.server.host=localhost\n" +
"override.server.port=8080\n" +
"cloud.server.host=cloud\n" +
"cloud.server.port=9000\n"), "application.properties"));
"cloud.server.port=9000\n" +
"cloud.server.port=9000\n" +
"hierarchy.foo=bar"),
"application.properties"));
@Inject
Config config;

Expand Down Expand Up @@ -290,4 +294,26 @@ public String convert(final String value) {
void converters() {
assertEquals("bar", converters.foo());
}

public interface Base {

String foo();
}

@ConfigMapping(prefix = "hierarchy")
public interface ExtendsBase extends Base {
}

@Inject
Base base;

@Inject
ExtendsBase extendsBase;

@Test
void hierarchy() {
assertSame(base, extendsBase);
assertEquals("bar", extendsBase.foo());
}

}

0 comments on commit 0dc3171

Please sign in to comment.