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

Ordering improvements #10488

Merged
merged 1 commit into from
Feb 12, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private Interceptor[] resolveToNone(ExecutableMethod<?, ?> method,
selectedInterceptorRegistrations.add(beanRegistration);
}
}
selectedInterceptorRegistrations.sort(OrderUtil.COMPARATOR);
selectedInterceptorRegistrations.sort(OrderUtil.ORDERED_COMPARATOR);

List<Interceptor<T, ?>> selectedInterceptors = new ArrayList<>(selectedInterceptorRegistrations.size());
for (BeanRegistration<Interceptor<T, ?>> beanRegistration : selectedInterceptorRegistrations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
import io.micronaut.context.AbstractMessageSource;
import io.micronaut.context.MessageSource;
import io.micronaut.context.annotation.Primary;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.order.OrderUtil;
import io.micronaut.core.util.ArgumentUtils;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* Composite message source that is the primary message source.
Expand All @@ -46,7 +45,7 @@ public final class CompositeMessageSource extends AbstractMessageSource {
*/
public CompositeMessageSource(@Nullable Collection<MessageSource> messageSources) {
if (messageSources != null) {
this.messageSources = OrderUtil.sort(messageSources.stream()).collect(Collectors.toList());
this.messageSources = OrderUtil.sortOrderedCollection(messageSources);
} else {
this.messageSources = Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.AnnotationValueBuilder;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NextMajorVersion;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.beans.BeanConstructor;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.ConversionServiceProvider;
import io.micronaut.core.expressions.EvaluatedExpressionReference;
import io.micronaut.core.naming.NameUtils;
import io.micronaut.core.order.OrderUtil;
import io.micronaut.core.reflect.ClassUtils;
import io.micronaut.core.reflect.ReflectionUtils;
import io.micronaut.core.type.Argument;
Expand Down Expand Up @@ -160,6 +162,7 @@
*/
@Internal
public class BeanDefinitionWriter extends AbstractClassFileWriter implements BeanDefinitionVisitor, BeanElement, Toggleable {
@NextMajorVersion("Inline as true")
public static final String OMIT_CONFPROP_INJECTION_POINTS = "micronaut.processing.omit.confprop.injectpoints";

public static final String CLASS_SUFFIX = "$Definition";
Expand Down Expand Up @@ -1176,6 +1179,7 @@ public void accept(Map<String, ClassElement> stringClassElementMap) {
buildCheckIfShouldLoadMethod(checkIfShouldLoadMethodVisitor, annotationInjectionPoints);
checkIfShouldLoadMethodVisitor.visitMaxs(DEFAULT_MAX_STACK, 10);
}
addGetOrder();

getInterceptedType().ifPresent(t -> implementInterceptedTypeMethod(t, this.classWriter));

Expand Down Expand Up @@ -1311,6 +1315,14 @@ private void addGetExposedTypes(GeneratorAdapter staticInit) {
}
}

private void addGetOrder() {
int order = OrderUtil.getOrder(annotationMetadata);
GeneratorAdapter getOrderMethod = startPublicMethod(classWriter, "getOrder", int.class.getName());
getOrderMethod.push(order);
getOrderMethod.returnValue();
getOrderMethod.endMethod();
}

private void pushStoreClassesAsSet(GeneratorAdapter writer, String[] classes) {
if (classes.length > 1) {
writer.newInstance(Type.getType(HashSet.class));
Expand Down
9 changes: 8 additions & 1 deletion core/src/main/java/io/micronaut/core/beans/BeanInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import io.micronaut.core.annotation.AnnotationMetadataProvider;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.order.OrderUtil;
import io.micronaut.core.order.Ordered;
import io.micronaut.core.type.Argument;
import io.micronaut.core.type.ArgumentCoercible;

Expand All @@ -26,7 +28,7 @@
* @param <T> The type of the bean
* @since 4.0.0
*/
public interface BeanInfo<T> extends AnnotationMetadataProvider, ArgumentCoercible<T> {
public interface BeanInfo<T> extends AnnotationMetadataProvider, ArgumentCoercible<T>, Ordered {
/**
* @return The bean type
*/
Expand All @@ -45,4 +47,9 @@ default Argument<T> getGenericBeanType() {
default Argument<T> asArgument() {
return getGenericBeanType();
}

@Override
default int getOrder() {
return OrderUtil.getOrder(getAnnotationMetadata());
}
}
84 changes: 78 additions & 6 deletions core/src/main/java/io/micronaut/core/order/OrderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Order;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
Expand All @@ -36,12 +38,32 @@ public class OrderUtil {
/**
* Provide a comparator for collections.
*/
public static final Comparator<Object> COMPARATOR = (o1, o2) -> {
int order1 = getOrder(o1);
int order2 = getOrder(o2);
return Integer.compare(order1, order2);
// Keep as an anonymous class to avoid lambda overhead during the startup
public static final Comparator<Object> COMPARATOR = new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
int order1 = getOrder(o1);
int order2 = getOrder(o2);
return Integer.compare(order1, order2);
}
};

/**
* The comparator of elements implementing {@link Ordered}.
*/
// Keep as an anonymous class to avoid lambda overhead during the startup
public static final Comparator<Ordered> ORDERED_COMPARATOR = new Comparator<Ordered>() {
@Override
public int compare(Ordered o1, Ordered o2) {
return Integer.compare(o1.getOrder(), o2.getOrder());
}
};

/**
* The reverse comparator of elements implementing {@link Ordered}.
*/
public static final Comparator<Ordered> REVERSE_ORDERED_COMPARATOR = Collections.reverseOrder(ORDERED_COMPARATOR);

/**
* Provide a comparator, in reversed order, for collections.
*/
Expand All @@ -67,6 +89,54 @@ public static <T> Stream<T> sort(Stream<T> list) {
return list.sorted(COMPARATOR);
}

/**
* Sort the given stream.
*
* @param list The list to sort
* @param <T> The stream generic type
* @return The sorted stream
* @since 4.4.0
*/
public static <T extends Ordered> Stream<T> sortOrdered(Stream<T> list) {
return list.sorted(ORDERED_COMPARATOR);
}

/**
* Sort the given list.
*
* @param list The list to sort
* @param <T> The type
* @return The sorted collection
* @since 4.4.0
*/
public static <T extends Ordered> List<T> sortOrderedCollection(Collection<T> list) {
var newList = new ArrayList<>(list);
newList.sort(ORDERED_COMPARATOR);
return newList;
}

/**
* Sort the given list.
*
* @param list The list to sort
* @param <T> The type
* @since 4.4.0
*/
public static <T extends Ordered> void sortOrdered(List<T> list) {
list.sort(ORDERED_COMPARATOR);
}

/**
* Sort the given list.
*
* @param list The list to sort
* @param <T> The type
* @since 4.4.0
*/
public static <T extends Ordered> void reverseSortOrdered(List<T> list) {
list.sort(REVERSE_ORDERED_COMPARATOR);
}

/**
* Sort the given list.
*
Expand Down Expand Up @@ -111,7 +181,7 @@ public static void sort(Object[] objects) {
*/
public static int getOrder(Object o) {
if (o instanceof Ordered ordered) {
return getOrder(ordered);
return ordered.getOrder();
} else if (o instanceof AnnotationMetadata metadata) {
return getOrder(metadata);
}
Expand All @@ -128,7 +198,7 @@ public static int getOrder(Object o) {
*/
public static int getOrder(AnnotationMetadata annotationMetadata, Object o) {
if (o instanceof Ordered ordered) {
return getOrder(ordered);
return ordered.getOrder();
}
return getOrder(annotationMetadata);
}
Expand All @@ -148,7 +218,9 @@ public static int getOrder(@NonNull AnnotationMetadata annotationMetadata) {
*
* @param o The ordered object
* @return the order
* @deprecated Inline method
*/
@Deprecated(since = "4.4.0", forRemoval = true)
public static int getOrder(Ordered o) {
return o.getOrder();
}
Expand Down
4 changes: 2 additions & 2 deletions http/src/main/java/io/micronaut/http/filter/FilterRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private static void checkOrdered(List<? extends GenericHttpFilter> filters) {
*/
public static void sort(@NonNull List<GenericHttpFilter> filters) {
checkOrdered(filters);
OrderUtil.sort(filters);
OrderUtil.sortOrdered((List) filters); // All filters are implementing ordered
}

/**
Expand All @@ -92,7 +92,7 @@ public static void sort(@NonNull List<GenericHttpFilter> filters) {
*/
public static void sortReverse(@NonNull List<GenericHttpFilter> filters) {
checkOrdered(filters);
OrderUtil.reverseSort(filters);
OrderUtil.reverseSortOrdered((List) filters); // All filters are implementing ordered
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class BeanRegistration<T> implements Ordered, CreatedBean<T>, BeanType<T>
final BeanIdentifier identifier;
final BeanDefinition<T> beanDefinition;
final T bean;
private final int order;

/**
* @param identifier The bean identifier
Expand All @@ -52,6 +53,11 @@ public BeanRegistration(BeanIdentifier identifier, BeanDefinition<T> beanDefinit
this.identifier = identifier;
this.beanDefinition = beanDefinition;
this.bean = bean;
if (bean == null) {
this.order = beanDefinition == null ? 0 : beanDefinition.getOrder();
} else {
this.order = beanDefinition == null ? OrderUtil.getOrder(bean) : OrderUtil.getOrder(beanDefinition, bean);
}
}

/**
Expand Down Expand Up @@ -103,7 +109,7 @@ public static <K> BeanRegistration<K> of(@NonNull BeanContext beanContext,

@Override
public int getOrder() {
return OrderUtil.getOrder(beanDefinition.getAnnotationMetadata(), bean);
return order;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ private static ApplicationContextConfigurer loadApplicationContextCustomizer(@Nu
if (configurers.size() == 1) {
return configurers.get(0);
}
OrderUtil.sort(configurers);
OrderUtil.sortOrdered(configurers);
return new ApplicationContextConfigurer() {

@Override
Expand Down
Loading
Loading