Skip to content

Commit

Permalink
javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Nov 12, 2024
1 parent 90c0ed0 commit 7dd4805
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* private SomeConfig someConfig;
* }
*
* \@Settings
* public record SomeConfig(@Setting(key = "foo.bar.baz") String fooValue){ }
* </pre>
*
Expand Down Expand Up @@ -79,11 +80,17 @@ public Result<Void> setTargetValue(Object configObject) throws IllegalAccessExce
return Result.success();
}

/**
* Not used here, will always return null
*/
@Override
public ServiceProvider getDefaultServiceProvider() {
return null;
}

/**
* Not used here
*/
@Override
public void setDefaultServiceProvider(ServiceProvider defaultServiceProvider) {

Expand All @@ -92,22 +99,22 @@ public void setDefaultServiceProvider(ServiceProvider defaultServiceProvider) {
@Override
public Object resolve(ServiceExtensionContext context, DefaultServiceSupplier defaultServiceSupplier) {

// all fields annotated with the @Value annotation
var valueAnnotatedFields = resolveConfigValueFields(context, configurationObject.getType().getDeclaredFields());
// all fields annotated with the @Setting annotation
var settingsFields = resolveSettingsFields(context, configurationObject.getType().getDeclaredFields());

// records are treated specially, because they only contain final fields, and must be constructed with a non-default CTOR
// where every constructor arg MUST be named the same as the field value. We can't rely on this with normal classes
if (configurationObject.getType().isRecord()) {
// find matching constructor
var constructor = Stream.of(configurationObject.getType().getDeclaredConstructors())
.filter(constructorFilter(valueAnnotatedFields))
.filter(constructorFilter(settingsFields))
.findFirst()
.orElseThrow(() -> new EdcInjectionException("No suitable constructor found on record class '%s'".formatted(configurationObject.getType())));

try {
// invoke CTor with the previously resolved config values
constructor.setAccessible(true);
return constructor.newInstance(valueAnnotatedFields.stream().map(FieldValue::value).toArray());
return constructor.newInstance(settingsFields.stream().map(FieldValue::value).toArray());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new EdcInjectionException(e);
}
Expand All @@ -120,7 +127,7 @@ public Object resolve(ServiceExtensionContext context, DefaultServiceSupplier de
var instance = defaultCtor.newInstance();

// set the field values on the newly-constructed object instance
valueAnnotatedFields.forEach(fe -> {
settingsFields.forEach(fe -> {
try {
var field = pojoClass.getDeclaredField(fe.fieldName());
field.setAccessible(true);
Expand Down Expand Up @@ -168,7 +175,7 @@ private Predicate<Constructor<?>> constructorFilter(List<FieldValue> args) {

}

private @NotNull List<FieldValue> resolveConfigValueFields(ServiceExtensionContext context, Field[] fields) {
private @NotNull List<FieldValue> resolveSettingsFields(ServiceExtensionContext context, Field[] fields) {
return injectionPointsFrom(fields)
.map(ip -> {
var val = ip.resolve(context, null /*the default supplier arg is not used anyway*/);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public interface InjectionPoint<T> {
*
* @param dependencyMap a map containing the current dependency list
* @param context the fully constructed {@link ServiceExtensionContext}
* @return success if it can be resolved, a failure otherwise.
* @return successful result containing a (potentially empty) list of injection containers that can provide this service, a failure otherwise.
*/
Result<List<InjectionContainer<T>>> getProviders(Map<Class<?>, List<InjectionContainer<T>>> dependencyMap, ServiceExtensionContext context);

Expand Down

0 comments on commit 7dd4805

Please sign in to comment.