Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/4.2.x' into 4.2.x
Browse files Browse the repository at this point in the history
# Conflicts:
#	core/src/main/java/io/micronaut/core/convert/DefaultMutableConversionService.java
#	gradle.properties
#	inject/src/main/java/io/micronaut/context/env/PropertySourcePropertyResolver.java
#	inject/src/main/java/io/micronaut/inject/annotation/EnvironmentConvertibleValuesMap.java
  • Loading branch information
altro3 committed Aug 23, 2023
2 parents d57c1d9 + 4a503e6 commit 45d0e92
Show file tree
Hide file tree
Showing 37 changed files with 1,973 additions and 1,327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import io.micronaut.inject.ast.annotation.MutableAnnotationMetadataDelegate;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;

/**
* Represents a generic element that can appear as a type argument.
*
Expand Down Expand Up @@ -55,4 +57,25 @@ default MutableAnnotationMetadataDelegate<AnnotationMetadata> getGenericTypeAnno
return (MutableAnnotationMetadataDelegate<AnnotationMetadata>) MutableAnnotationMetadataDelegate.EMPTY;
}

/**
* In some cases the class element can be a resolved as a generic element and this element should return the actual
* type that is generic element is representing.
*
* @return The resolved value of the generic element.
* @since 4.2.0
*/
default Optional<ClassElement> getResolved() {
return Optional.empty();
}

/**
* Tries to resolve underneath type using {@link #getResolved()} or returns this type otherwise.
*
* @return The resolved value of the generic element or this type.
* @since 4.2.0
*/
default ClassElement resolved() {
return getResolved().orElse(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micronaut.inject.ast;

import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.NextMajorVersion;
import io.micronaut.core.annotation.NonNull;

import java.util.List;
Expand Down Expand Up @@ -68,6 +69,8 @@ default Element getRequiredDeclaringElement() {
* @return The resolved value of the placeholder.
* @since 4.0.0
*/
@NextMajorVersion("Remove this method. There is an equivalent in the super class.")
@Override
default Optional<ClassElement> getResolved() {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class AnnotationClassValue<T> implements CharSequence, Named {
public static final AnnotationClassValue<?>[] EMPTY_ARRAY = new AnnotationClassValue[0];

private final String name;
private final Class<T> theClass;
final Class<T> theClass;
private final T instance;
private final boolean instantiated;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,21 @@
import java.lang.annotation.Annotation;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Array;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* A runtime representation of the annotation and its values.
Expand Down Expand Up @@ -1564,8 +1575,11 @@ public static Class<?>[] resolveClassValues(@Nullable Object value) {
if (len == 1) {
return annotationValues[0].classValues();
} else {
return Arrays.stream(annotationValues)
.flatMap(annotationValue -> Stream.of(annotationValue.classValues())).toArray(Class[]::new);
List<Class<?>> list = new ArrayList<>(5);
for (AnnotationValue<?> annotationValue : annotationValues) {
list.addAll(Arrays.asList(annotationValue.classValues()));
}
return list.toArray(new Class[0]);
}
}
return null;
Expand All @@ -1577,14 +1591,18 @@ public static Class<?>[] resolveClassValues(@Nullable Object value) {
if (values instanceof Class<?>[] classes) {
return classes;
} else {
return Arrays.stream(values).flatMap(o -> {
List<Class<?>> list = new ArrayList<>(5);
for (Object o : values) {
if (o instanceof AnnotationClassValue<?> annotationClassValue) {
return annotationClassValue.getType().stream();
if (annotationClassValue.theClass != null) {
list.add(annotationClassValue.theClass);
}
} else if (o instanceof Class<?> aClass) {
return Stream.of(aClass);
list.add(aClass);
}
return Stream.empty();
}).toArray(Class[]::new);
}
return list.toArray(new Class[0]);

}
}
if (value instanceof Class<?> aClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ public <T> Optional<T> convert(Object object, Class<T> targetType, ConversionCon
return Optional.of((T) object);
}
targetType = targetType.isPrimitive() ? (Class<T>) ReflectionUtils.getWrapperType(targetType) : targetType;

if (targetType.isInstance(object) && !(object instanceof Iterable) && !(object instanceof Map)) {
return Optional.of((T) object);
}
Expand Down Expand Up @@ -302,8 +301,7 @@ private void registerDefaultConverters() {

// InputStream -> String
addConverter(InputStream.class, String.class, (object, targetType, context) -> {
BufferedReader reader = new BufferedReader(new InputStreamReader(object));
try {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(object))) {
return Optional.of(IOUtils.readText(reader));
} catch (IOException e) {
context.reject(e);
Expand Down Expand Up @@ -331,8 +329,7 @@ private void registerDefaultConverters() {

// Reader -> String
addConverter(Reader.class, String.class, (object, targetType, context) -> {
BufferedReader reader = object instanceof BufferedReader br ? br : new BufferedReader(object);
try {
try (BufferedReader reader = object instanceof BufferedReader bufferedReader ? bufferedReader : new BufferedReader(object)) {
return Optional.of(IOUtils.readText(reader));
} catch (IOException e) {
context.reject(e);
Expand Down Expand Up @@ -427,7 +424,7 @@ private void registerDefaultConverters() {
return Optional.empty();
}
try {
return Optional.ofNullable(Paths.get(object.toString()));
return Optional.of(Paths.get(object.toString()));
} catch (Exception e) {
context.reject("Invalid path [" + object + " ]: " + e.getMessage(), e);
return Optional.empty();
Expand Down Expand Up @@ -549,15 +546,10 @@ private void registerDefaultConverters() {
// String -> Boolean
addConverter(CharSequence.class, Boolean.class, (CharSequence object, Class<Boolean> targetType, ConversionContext context) -> {
String booleanString = object.toString().toLowerCase(Locale.ENGLISH);
switch (booleanString) {
case "yes":
case "y":
case "on":
case "true":
return Optional.of(Boolean.TRUE);
default:
return Optional.of(Boolean.FALSE);
}
return switch (booleanString) {
case "yes", "y", "on", "true" -> Optional.of(Boolean.TRUE);
default -> Optional.of(Boolean.FALSE);
};
});

// String -> URL
Expand Down Expand Up @@ -652,9 +644,9 @@ private void registerDefaultConverters() {

// String -> Array
addConverter(CharSequence.class, Object[].class, (CharSequence object, Class<Object[]> targetType, ConversionContext context) -> {
if (object instanceof AnnotationClassValue value && targetType.equals(AnnotationClassValue[].class)) {
AnnotationClassValue[] array = new AnnotationClassValue[1];
array[0] = value;
if (object instanceof AnnotationClassValue<?> annotationClassValue && targetType.equals(AnnotationClassValue[].class)) {
AnnotationClassValue<?>[] array = new AnnotationClassValue<?>[1];
array[0] = annotationClassValue;
return Optional.of(array);
}

Expand Down Expand Up @@ -756,8 +748,8 @@ private void registerDefaultConverters() {
return Optional.of(object.doubleValue());
}
if (targetNumberType == BigInteger.class) {
if (object instanceof BigDecimal decimal) {
return Optional.of(decimal.toBigInteger());
if (object instanceof BigDecimal bigDecimal) {
return Optional.of(bigDecimal.toBigInteger());
}
return Optional.of(BigInteger.valueOf(object.longValue()));
}
Expand All @@ -775,9 +767,9 @@ private void registerDefaultConverters() {

Class<?> targetComponentType = ReflectionUtils.getWrapperType(componentType.getType());
String[] strings = object.toString().split(",");
List list = new ArrayList();
List<Object> list = new ArrayList<>();
for (String string : strings) {
Optional converted = convert(string, targetComponentType, newContext);
Optional<?> converted = convert(string, targetComponentType, newContext);
if (converted.isPresent()) {
list.add(converted.get());
}
Expand All @@ -803,12 +795,12 @@ private void registerDefaultConverters() {

addConverter(Object.class, OptionalInt.class, (object, targetType, context) -> {
Optional<Integer> converted = convert(object, Integer.class, context);
return converted.map(integer -> Optional.of(OptionalInt.of(integer))).orElseGet(() -> Optional.of(OptionalInt.empty()));
return converted.map(OptionalInt::of).or(() -> Optional.of(OptionalInt.empty()));
});

addConverter(Object.class, OptionalLong.class, (object, targetType, context) -> {
Optional<Long> converted = convert(object, Long.class, context);
return converted.map(longValue -> Optional.of(OptionalLong.of(longValue))).orElseGet(() -> Optional.of(OptionalLong.empty()));
return converted.map(OptionalLong::of).or(() -> Optional.of(OptionalLong.empty()));
});

// Iterable -> String
Expand Down Expand Up @@ -848,7 +840,7 @@ private void registerDefaultConverters() {
if (targetType.isInstance(object) && targetComponentType == Object.class) {
return Optional.of(object);
}
List list = new ArrayList();
List<Object> list = new ArrayList<>();
ConversionContext newContext = context.with(componentType);
for (Object o : object) {
Optional<?> converted = convert(o, targetComponentType, newContext);
Expand All @@ -865,7 +857,7 @@ private void registerDefaultConverters() {
// Object[] -> Object[] (inner type conversion)
addConverter(Object[].class, Object[].class, (object, targetType, context) -> {
Class<?> targetComponentType = targetType.getComponentType();
List results = new ArrayList();
List<Object> results = new ArrayList<>(object.length);
for (Object o : object) {
Optional<?> converted = convert(o, targetComponentType, context);
if (converted.isPresent()) {
Expand All @@ -878,7 +870,7 @@ private void registerDefaultConverters() {
// Iterable -> Object[]
addConverter(Iterable.class, Object[].class, (object, targetType, context) -> {
Class<?> targetComponentType = targetType.getComponentType();
List results = new ArrayList();
List<Object> results = new ArrayList<>();
for (Object o : object) {
Optional<?> converted = convert(o, targetComponentType, context);
if (converted.isPresent()) {
Expand All @@ -896,12 +888,10 @@ private void registerDefaultConverters() {
Class<?> targetComponentType = targetType.getComponentType();
Optional<?> converted = convert(object, targetComponentType);
if (converted.isPresent()) {

Object[] result = (Object[]) Array.newInstance(targetComponentType, 1);
result[0] = converted.get();
return Optional.of(result);
}

return Optional.empty();
});

Expand All @@ -920,22 +910,22 @@ private void registerDefaultConverters() {
ConversionContext keyContext = context.with(keyArgument);
ConversionContext valContext = context.with(valArgument);

Map newMap = isProperties ? new Properties() : new LinkedHashMap();
Map<Object, Object> newMap = isProperties ? new Properties() : new LinkedHashMap<>();

for (Object o : object.entrySet()) {
Map.Entry entry = (Map.Entry) o;
Map.Entry<?, ?> entry = (Map.Entry) o;
Object key = entry.getKey();
Object value = entry.getValue();
if (!keyType.isInstance(key)) {
Optional convertedKey = convert(key, keyType, keyContext);
Optional<?> convertedKey = convert(key, keyType, keyContext);
if (convertedKey.isPresent()) {
key = convertedKey.get();
} else {
continue;
}
}
if (!valueType.isInstance(value) || value instanceof Map || value instanceof Collection) {
Optional converted = convert(value, valueType, valContext);
Optional<?> converted = convert(value, valueType, valContext);
if (converted.isPresent()) {
value = converted.get();
} else {
Expand Down Expand Up @@ -1036,8 +1026,8 @@ private NumberFormat resolveNumberFormat(ConversionContext context) {

private <S, T> ConvertiblePair newPair(Class<S> sourceType, Class<T> targetType, TypeConverter<S, T> typeConverter) {
ConvertiblePair pair;
if (typeConverter instanceof FormattingTypeConverter converter) {
pair = new ConvertiblePair(sourceType, targetType, converter.annotationType().getName());
if (typeConverter instanceof FormattingTypeConverter<S, T, ?> formattingTypeConverter) {
pair = new ConvertiblePair(sourceType, targetType, formattingTypeConverter.annotationType().getName());
} else {
pair = new ConvertiblePair(sourceType, targetType);
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
projectVersion=4.1.2-SNAPSHOT
projectVersion=4.2.0-SNAPSHOT
projectGroupId=io.micronaut
projectDesc=Natively Cloud Native
title=Micronaut Framework
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public List<GenericHttpFilter> resolveFilters(HttpRequest<?> request, List<Filte
List<GenericHttpFilter> filterList = new ArrayList<>(filterEntries.size());
for (FilterEntry filterEntry : filterEntries) {
final GenericHttpFilter filter = filterEntry.getFilter();
if (filter instanceof GenericHttpFilter.AroundLegacy al && !al.isEnabled()) {
if (!GenericHttpFilter.isEnabled(filter)) {
continue;
}
if (matchesFilterEntry(method, requestPath, filterEntry)) {
Expand Down Expand Up @@ -167,7 +167,7 @@ private ClientFilterEntry createClientFilterEntry(@NonNull AnnotationMetadataRes
FilterPatternStyle patternStyle = annotationMetadata.enumValue(Filter.class,
"patternStyle", FilterPatternStyle.class).orElse(FilterPatternStyle.ANT);
return new ClientFilterEntry(
new GenericHttpFilter.AroundLegacy(httpClientFilter, new FilterOrder.Dynamic(OrderUtil.getOrder(annotationMetadata))),
GenericHttpFilter.createLegacyFilter(httpClientFilter, new FilterOrder.Dynamic(OrderUtil.getOrder(annotationMetadata))),
annotationMetadata,
methodsForFilter(annotationMetadata),
patternStyle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ protected <I, R extends io.micronaut.http.HttpResponse<?>> Publisher<R> applyFil
filterResolver.resolveFilters(request, clientFilterEntries);

FilterRunner.sortReverse(filters);
filters.add(new GenericHttpFilter.TerminalReactive(responsePublisher));
filters.add(GenericHttpFilter.terminalReactiveFilter(responsePublisher));

FilterRunner runner = new FilterRunner(filters);
return Mono.from(ReactiveExecutionFlow.fromFlow((ExecutionFlow<R>) runner.run(request)).toPublisher());
Expand Down
Loading

0 comments on commit 45d0e92

Please sign in to comment.