Skip to content

Commit d6b47db

Browse files
committed
Stabilize registration order
1 parent afe7d9a commit d6b47db

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/main/java/com/coditory/quark/context/ContextBuilder.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22

33
import com.coditory.quark.context.annotations.Bean;
44
import com.coditory.quark.context.annotations.Configuration;
5-
import com.coditory.quark.context.annotations.Init;
6-
import com.coditory.quark.context.annotations.PostInit;
75
import com.coditory.quark.context.events.ContextEventHandler;
86
import com.coditory.quark.eventbus.DispatchExceptionHandler;
97
import com.coditory.quark.eventbus.EventBus;
108
import com.coditory.quark.eventbus.EventBusBuilder;
11-
import com.coditory.quark.eventbus.EventEmitter;
129
import com.coditory.quark.eventbus.EventListener;
1310
import com.coditory.quark.eventbus.Subscription;
1411
import org.jetbrains.annotations.NotNull;
1512

1613
import java.lang.reflect.Method;
1714
import java.time.Duration;
1815
import java.util.ArrayList;
16+
import java.util.Arrays;
17+
import java.util.Comparator;
1918
import java.util.LinkedHashMap;
2019
import java.util.LinkedHashSet;
2120
import java.util.List;
@@ -32,7 +31,6 @@
3231
import static com.coditory.quark.context.Preconditions.expect;
3332
import static com.coditory.quark.context.Preconditions.expectNonBlank;
3433
import static com.coditory.quark.context.Preconditions.expectNonNull;
35-
import static java.util.stream.Collectors.toCollection;
3634

3735
public final class ContextBuilder {
3836
public static final String CONTEXT_EVENT_BUS_NAME = "ContextEventBus";
@@ -187,12 +185,14 @@ private <T> void addBeansFromConfiguration(Configuration configuration, Class<T>
187185
BeanConfig config = BeanConfig.fromAnnotationOrDefault(configuration);
188186
BeanHolder<T> holder = holder(descriptor(type, name), fromConstructor(type), config);
189187
addBeanHolder(holder);
190-
for (Method method : type.getDeclaredMethods()) {
191-
if (method.isAnnotationPresent(Bean.class)) {
192-
method.setAccessible(true);
193-
addFromAnnotatedConfiguration(holder, method, method.getReturnType());
194-
}
195-
}
188+
Arrays.stream(type.getDeclaredMethods())
189+
.filter((Method method) -> method.isAnnotationPresent(Bean.class))
190+
// sort by name to stabilize registration order
191+
.sorted(Comparator.comparing(Method::getName))
192+
.forEach((Method method) -> {
193+
method.setAccessible(true);
194+
addFromAnnotatedConfiguration(holder, method, method.getReturnType());
195+
});
196196
}
197197

198198
private <T> void addFromAnnotatedConfiguration(BeanHolder<?> holder, Method method, Class<T> returnType) {

0 commit comments

Comments
 (0)