Skip to content

Commit 1c96cfb

Browse files
authored
feat: upgrade to aerogel v3 (#1524)
### Motivation Aerogel 3 was released and we should upgrade. ### Modification Upgraded to aerogel v3 and addressed the needed changes ### Result Aerogel 3 is used.
1 parent 6ccd461 commit 1c96cfb

76 files changed

Lines changed: 592 additions & 602 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ subprojects {
118118
if (project.path != ":launcher:java8" && project.path != ":launcher:patcher") {
119119
options.compilerArgs.add("--enable-preview")
120120
options.compilerArgs.add("-Xlint:-deprecation,-unchecked,-preview")
121+
options.compilerArgs.add("-proc:full")
121122
}
122123
}
123124

driver/api/src/main/java/eu/cloudnetservice/driver/inject/DefaultInjectionLayer.java

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,32 @@
1616

1717
package eu.cloudnetservice.driver.inject;
1818

19-
import dev.derklaro.aerogel.Element;
20-
import dev.derklaro.aerogel.InjectionContext;
19+
import dev.derklaro.aerogel.InjectionRequest;
2120
import dev.derklaro.aerogel.Injector;
22-
import dev.derklaro.aerogel.SpecifiedInjector;
23-
import dev.derklaro.aerogel.auto.runtime.AutoAnnotationRegistry;
24-
import dev.derklaro.aerogel.binding.BindingConstructor;
25-
import dev.derklaro.aerogel.internal.context.util.ContextInstanceResolveHelper;
26-
import java.util.function.Consumer;
21+
import dev.derklaro.aerogel.auto.AerogelAutoModule;
22+
import dev.derklaro.aerogel.binding.DynamicBinding;
23+
import dev.derklaro.aerogel.binding.UninstalledBinding;
24+
import dev.derklaro.aerogel.binding.key.BindingKey;
25+
import java.io.IOException;
26+
import java.io.UncheckedIOException;
27+
import java.util.function.UnaryOperator;
2728
import lombok.NonNull;
2829
import org.jetbrains.annotations.ApiStatus;
2930
import org.jetbrains.annotations.UnknownNullability;
3031

3132
/**
3233
* The default implementation for of an injector layer.
3334
*
34-
* @param injector the injector to use for the layer.
35-
* @param autoRegistry the auto registry to use for the layer.
36-
* @param name the name of this injection layer.
37-
* @param <I> the type of injector this layer uses.
35+
* @param injector the injector to use for the layer.
36+
* @param autoModule the auto registry to use for the layer.
37+
* @param name the name of this injection layer.
38+
* @param <I> the type of injector this layer uses.
3839
* @since 4.0
3940
*/
4041
@ApiStatus.Internal
4142
record DefaultInjectionLayer<I extends Injector>(
4243
@NonNull I injector,
43-
@NonNull AutoAnnotationRegistry autoRegistry,
44+
@NonNull AerogelAutoModule autoModule,
4445
@NonNull String name
4546
) implements InjectionLayer<I> {
4647

@@ -64,37 +65,37 @@ record DefaultInjectionLayer<I extends Injector>(
6465
* {@inheritDoc}
6566
*/
6667
@Override
67-
public <T> @UnknownNullability T instance(@NonNull Element element) {
68-
return this.injector.instance(element);
68+
public <T> @UnknownNullability T instance(@NonNull BindingKey<T> bindingKey) {
69+
return this.injector.instance(bindingKey);
6970
}
7071

7172
/**
7273
* {@inheritDoc}
7374
*/
7475
@Override
75-
@SuppressWarnings("unchecked")
7676
public <T> @UnknownNullability T instance(
7777
@NonNull Class<T> type,
78-
@NonNull Consumer<InjectionContext.Builder> builder
78+
@NonNull UnaryOperator<InjectionRequest<T>> decorator
7979
) {
80-
// get the binding associated with the given type & construct a context builder
81-
var element = Element.forType(type);
82-
var binding = this.injector.binding(element);
83-
var contextBuilder = InjectionContext.builder(type, binding.provider(element));
84-
85-
// apply the builder decorator to the builder
86-
builder.accept(contextBuilder);
80+
var key = BindingKey.of(type);
81+
var injectionRequest = decorator.apply(this.injector.createInjectionRequest(key));
82+
return injectionRequest.construct();
83+
}
8784

88-
// resolve the instance
89-
return (T) ContextInstanceResolveHelper.resolveInstanceAndRemoveContext(contextBuilder.build());
85+
/**
86+
* {@inheritDoc}
87+
*/
88+
@Override
89+
public void install(@NonNull UninstalledBinding<?> binding) {
90+
this.injector.installBinding(binding);
9091
}
9192

9293
/**
9394
* {@inheritDoc}
9495
*/
9596
@Override
96-
public void install(@NonNull BindingConstructor constructor) {
97-
this.injector.install(constructor);
97+
public void install(@NonNull DynamicBinding binding) {
98+
this.injector.installBinding(binding);
9899
}
99100

100101
/**
@@ -103,7 +104,15 @@ public void install(@NonNull BindingConstructor constructor) {
103104
@Override
104105
public void installAutoConfigureBindings(@NonNull ClassLoader loader, @NonNull String component) {
105106
var fileName = String.format(AUTO_CONFIGURE_FILE_NAME_FORMAT, component);
106-
this.autoRegistry.installBindings(loader, fileName, this.injector);
107+
try (var stream = loader.getResourceAsStream(fileName)) {
108+
if (stream != null) {
109+
this.autoModule.deserializeBindings(stream, loader).installBindings(this.injector);
110+
}
111+
} catch (IOException exception) {
112+
throw new UncheckedIOException(
113+
String.format("Unable to auto configure bindings for component %s with file %s", component, fileName),
114+
exception);
115+
}
107116
}
108117

109118
/**
@@ -129,9 +138,7 @@ public void installAutoConfigureBindings(@NonNull ClassLoader loader, @NonNull S
129138
@Override
130139
public void close() {
131140
// remove the bindings from the parent injector if needed
132-
if (this.injector instanceof SpecifiedInjector specifiedInjector) {
133-
specifiedInjector.removeConstructedBindings();
134-
}
141+
this.injector.close();
135142

136143
// remove this injector from the registry
137144
InjectionLayerProvider.REGISTRY.unregisterLayer(this);

driver/api/src/main/java/eu/cloudnetservice/driver/inject/InjectUtil.java

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,9 @@
1717
package eu.cloudnetservice.driver.inject;
1818

1919
import com.google.common.base.Preconditions;
20-
import dev.derklaro.aerogel.AerogelException;
21-
import dev.derklaro.aerogel.Element;
22-
import dev.derklaro.aerogel.binding.BindingBuilder;
23-
import dev.derklaro.aerogel.binding.BindingConstructor;
24-
import dev.derklaro.aerogel.internal.util.ElementHelper;
20+
import dev.derklaro.aerogel.binding.key.BindingKey;
2521
import java.lang.reflect.Parameter;
26-
import java.lang.reflect.Type;
2722
import java.util.Arrays;
28-
import java.util.Objects;
2923
import lombok.NonNull;
3024
import org.jetbrains.annotations.ApiStatus;
3125

@@ -38,7 +32,7 @@
3832
public final class InjectUtil {
3933

4034
private static final Object[] EMPTY_INSTANCE_ARRAY = new Object[0];
41-
private static final Element[] EMPTY_ELEMENT_ARRAY = new Element[0];
35+
private static final BindingKey<?>[] EMPTY_ELEMENT_ARRAY = new BindingKey[0];
4236

4337
private InjectUtil() {
4438
throw new UnsupportedOperationException();
@@ -51,7 +45,7 @@ private InjectUtil() {
5145
* @return an array of element each representing a given parameter, in order.
5246
* @throws NullPointerException if the given parameter array is null.
5347
*/
54-
public static @NonNull Element[] buildElementsForParameters(@NonNull Parameter[] parameters) {
48+
public static @NonNull BindingKey<?>[] buildElementsForParameters(@NonNull Parameter[] parameters) {
5549
return buildElementsForParameters(parameters, 0);
5650
}
5751

@@ -63,90 +57,69 @@ private InjectUtil() {
6357
* @return an array of element each representing a given parameter, in order.
6458
* @throws NullPointerException if the given parameter array is null.
6559
*/
66-
public static @NonNull Element[] buildElementsForParameters(@NonNull Parameter[] parameters, int offset) {
60+
public static @NonNull BindingKey<?>[] buildElementsForParameters(@NonNull Parameter[] parameters, int offset) {
6761
// return an empty element array if the given parameters are empty
6862
if (parameters.length <= offset) {
6963
return EMPTY_ELEMENT_ARRAY;
7064
}
7165

7266
// construct the element array
7367
var params = Arrays.copyOfRange(parameters, offset, parameters.length);
74-
var elements = new Element[params.length];
68+
var elements = new BindingKey[params.length];
7569
for (int i = 0; i < params.length; i++) {
7670
var parameter = params[i];
77-
elements[i] = ElementHelper.buildElement(parameter, parameter.getDeclaredAnnotations());
71+
elements[i] = BindingKey.of(parameter.getParameterizedType()).selectQualifier(parameter.getAnnotations());
7872
}
7973
return elements;
8074
}
8175

8276
/**
8377
* Finds all instances that are requested by the given element array in the given injection layer.
8478
*
85-
* @param layer the layer to retrieve the needed instances from.
86-
* @param elements the elements to get the instances of.
79+
* @param layer the layer to retrieve the needed instances from.
80+
* @param keys the keys to get the instances of.
8781
* @return the instances represented by the given elements, in order.
8882
* @throws NullPointerException if the given injection layer or elements array is null.
89-
* @throws AerogelException if an exception occurs while constructing a requested instance.
9083
*/
91-
public static @NonNull Object[] findAllInstances(@NonNull InjectionLayer<?> layer, @NonNull Element[] elements) {
92-
return findAllInstances(layer, elements, 0);
84+
public static @NonNull Object[] findAllInstances(
85+
@NonNull InjectionLayer<?> layer,
86+
@NonNull BindingKey<?>[] keys
87+
) {
88+
return findAllInstances(layer, keys, 0);
9389
}
9490

9591
/**
9692
* Finds all instances that are requested by the given element array in the given injection layer.
9793
*
98-
* @param layer the layer to retrieve the needed instances from.
99-
* @param elements the elements to get the instances of.
100-
* @param offset the offset to start writing the elements from in the resulting array, must be bigger than zero.
94+
* @param layer the layer to retrieve the needed instances from.
95+
* @param keys the keys to get the instances of.
96+
* @param offset the offset to start writing the elements from in the resulting array, must be bigger than zero.
10197
* @return the instances represented by the given elements, in order.
10298
* @throws NullPointerException if the given injection layer or elements array is null.
10399
* @throws IllegalArgumentException if the given offset is smaller than zero.
104-
* @throws AerogelException if an exception occurs while constructing a requested instance.
105100
*/
106101
public static @NonNull Object[] findAllInstances(
107102
@NonNull InjectionLayer<?> layer,
108-
@NonNull Element[] elements,
103+
@NonNull BindingKey<?>[] keys,
109104
int offset
110105
) {
111106
Preconditions.checkArgument(offset >= 0, "offset must be >= 0");
112107

113108
// return an empty array if the offset is 0 and no elements are present
114-
if (elements.length == 0 && offset == 0) {
109+
if (keys.length == 0 && offset == 0) {
115110
return EMPTY_INSTANCE_ARRAY;
116111
}
117112

118113
// return an array of the expected size if an offset is present but no elements are present
119-
if (elements.length == 0) {
114+
if (keys.length == 0) {
120115
return new Object[offset];
121116
}
122117

123118
// find the instances
124-
var instances = new Object[elements.length + offset];
125-
for (int i = 0; i < elements.length; i++) {
126-
instances[i + offset] = layer.instance(elements[i]);
119+
var instances = new Object[keys.length + offset];
120+
for (int i = 0; i < keys.length; i++) {
121+
instances[i + offset] = layer.instance(keys[i]);
127122
}
128123
return instances;
129124
}
130-
131-
/**
132-
* Creates a fixed binding constructor without setting any required name or annotation. It is only based on the type.
133-
*
134-
* @param value the value that is bound to the type.
135-
* @param types the types to bind the given value to.
136-
* @return the new binding constructor.
137-
* @throws NullPointerException if the given type or value is null.
138-
* @throws IndexOutOfBoundsException if the given type array is empty.
139-
*/
140-
public static @NonNull BindingConstructor createFixedBinding(@NonNull Object value, @NonNull Type... types) {
141-
Objects.checkIndex(0, types.length);
142-
143-
// extract the root binding type
144-
if (types.length == 1) {
145-
// only one type given, no need for further checks
146-
return BindingBuilder.create().bindFully(types[0]).toInstance(value);
147-
} else {
148-
// bind all given types fully
149-
return BindingBuilder.create().bindAllFully(types).toInstance(value);
150-
}
151-
}
152125
}

0 commit comments

Comments
 (0)