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

Correct how maps/sets are initialized with a correct size #9834

Merged
merged 1 commit into from
Sep 11, 2023
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
4 changes: 2 additions & 2 deletions aop/src/main/java/io/micronaut/aop/InvocationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import io.micronaut.core.type.ArgumentValue;
import io.micronaut.core.type.Executable;
import io.micronaut.core.type.MutableArgumentValue;
import io.micronaut.core.util.CollectionUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
Expand Down Expand Up @@ -117,7 +117,7 @@ default InvocationContext<T, R> setAttribute(@NonNull CharSequence name, Object
default @NonNull Map<String, Object> getParameterValueMap() {
Argument<?>[] arguments = getArguments();
Object[] parameterValues = getParameterValues();
Map<String, Object> valueMap = new LinkedHashMap<>(arguments.length);
Map<String, Object> valueMap = CollectionUtils.newLinkedHashMap(arguments.length);
for (int i = 0; i < parameterValues.length; i++) {
Object parameterValue = parameterValues[i];
Argument arg = arguments[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.annotation.Vetoed;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.inject.annotation.AnnotationMetadataHierarchy;
import io.micronaut.inject.ast.ClassElement;
Expand All @@ -49,7 +50,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -617,7 +617,7 @@ private void visitAdaptedMethod(MethodElement sourceMethod) {
}

Map<String, ClassElement> typeVariables = interfaceToAdapt.getTypeArguments();
Map<String, ClassElement> genericTypes = new LinkedHashMap<>(paramLen);
Map<String, ClassElement> genericTypes = CollectionUtils.newLinkedHashMap(paramLen);
for (int i = 0; i < paramLen; i++) {
ParameterElement targetParam = targetParams[i];
ParameterElement sourceParam = sourceParams[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public BeanElementBuilder typeArgumentsForType(ClassElement type, @NonNull Class
private Map<String, ClassElement> resolveTypeArguments(Map<String, ClassElement> typeArguments, ClassElement... types) {
Map<String, ClassElement> resolvedTypes = null;
if (typeArguments.size() == types.length) {
resolvedTypes = new LinkedHashMap<>(typeArguments.size());
resolvedTypes = CollectionUtils.newLinkedHashMap(typeArguments.size());
final Iterator<String> i = typeArguments.keySet().iterator();
for (ClassElement type : types) {
final String variable = i.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.inject.ast.Element;

import java.util.*;
Expand All @@ -36,7 +37,7 @@ final class DefaultOriginatingElements implements OriginatingElements {
* @param originatingElements The elements
*/
DefaultOriginatingElements(Element... originatingElements) {
this.originatingElements = new LinkedHashMap<>(originatingElements != null ? originatingElements.length : 5);
this.originatingElements = CollectionUtils.newLinkedHashMap(originatingElements != null ? originatingElements.length : 5);
if (originatingElements != null) {
for (Element originatingElement : originatingElements) {
if (originatingElement != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.micronaut.core.reflect.exception.InstantiationException;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.core.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -93,7 +94,7 @@ public static Optional<?> tryInstantiate(String name, ClassLoader classLoader) {
try {
if (constructorArguments.length > 0) {

Map bindMap = new LinkedHashMap(propertiesMap.size());
Map bindMap = CollectionUtils.newLinkedHashMap(propertiesMap.size());
Set<Map.Entry<?, ?>> entries = propertiesMap.entrySet();
for (Map.Entry<?, ?> entry : entries) {
Object key = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -295,7 +294,7 @@ public int hashCode() {
private static Map<String, Argument<?>> initializeTypeParameters(Argument<?>[] genericTypes) {
Map<String, Argument<?>> typeParameters;
if (genericTypes != null && genericTypes.length > 0) {
typeParameters = new LinkedHashMap<>(genericTypes.length);
typeParameters = CollectionUtils.newLinkedHashMap(genericTypes.length);
for (Argument<?> genericType : genericTypes) {
typeParameters.put(genericType.getName(), genericType);
}
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/io/micronaut/core/type/ReturnType.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
package io.micronaut.core.type;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.AnnotationMetadataProvider;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.util.CollectionUtils;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
Expand Down Expand Up @@ -73,7 +74,7 @@ default boolean isSingleResult() {
* @return A {@link ReturnType}
*/
static <T1> ReturnType<T1> of(Class<T1> type, Argument<?>... typeArguments) {
Map<String, Argument<?>> argumentMap = new LinkedHashMap<>(typeArguments.length);
Map<String, Argument<?>> argumentMap = CollectionUtils.newLinkedHashMap(typeArguments.length);
for (Argument<?> argument : typeArguments) {
argumentMap.put(argument.getName(), argument);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.http.HttpMethod;
import io.micronaut.jackson.core.tree.JsonNodeTreeCodec;
import io.micronaut.json.JsonMapper;
Expand All @@ -31,7 +32,6 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -125,7 +125,7 @@ private static InputStream openMetadataUrl(URL url, int connectionTimeoutMs, int
@Experimental
public static void populateMetadata(AbstractComputeInstanceMetadata instanceMetadata, JsonNode metadata) {
if (metadata != null) {
Map<String, String> finalMetadata = new HashMap<>(metadata.size());
Map<String, String> finalMetadata = CollectionUtils.newHashMap(metadata.size());
for (Map.Entry<String, JsonNode> entry : metadata.entries()) {
JsonNode value = entry.getValue();
if (value.isString()) {
Expand Down Expand Up @@ -155,7 +155,7 @@ public static Optional<String> stringValue(com.fasterxml.jackson.databind.JsonNo
*/
public static void populateMetadata(AbstractComputeInstanceMetadata instanceMetadata, Map<?, ?> metadata) {
if (metadata != null) {
Map<String, String> finalMetadata = new HashMap<>(metadata.size());
Map<String, String> finalMetadata = CollectionUtils.newHashMap(metadata.size());
for (Map.Entry<?, ?> entry : metadata.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micronaut.function.client;

import io.micronaut.core.async.publisher.Publishers;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.discovery.DiscoveryClient;
import io.micronaut.discovery.ServiceInstance;
import io.micronaut.function.LocalFunctionRegistry;
Expand All @@ -27,7 +28,6 @@

import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

Expand All @@ -52,7 +52,7 @@ public class DefaultFunctionDiscoveryClient implements FunctionDiscoveryClient {
*/
public DefaultFunctionDiscoveryClient(DiscoveryClient discoveryClient, FunctionDefinitionProvider[] providers, FunctionDefinition... definitions) {
this.discoveryClient = discoveryClient;
this.functionDefinitionMap = new HashMap<>(definitions.length);
this.functionDefinitionMap = CollectionUtils.newHashMap(definitions.length);
for (FunctionDefinition definition : definitions) {
functionDefinitionMap.put(definition.getName(), definition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -79,7 +78,7 @@ public DefaultLoadBalancerResolver(
List<ServiceInstanceList> serviceInstanceLists) {
this.beanContext = beanContext;
if (CollectionUtils.isNotEmpty(serviceInstanceLists)) {
this.serviceInstanceLists = new HashMap<>(serviceInstanceLists.size());
this.serviceInstanceLists = CollectionUtils.newHashMap(serviceInstanceLists.size());
for (ServiceInstanceList provider : serviceInstanceLists) {
this.serviceInstanceLists.put(provider.getID(), provider);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.value.ConvertibleMultiValuesMap;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.http.MutableHttpParameters;

import java.util.ArrayList;
Expand Down Expand Up @@ -54,7 +55,7 @@ public class NettyHttpParameters implements MutableHttpParameters {
public NettyHttpParameters(Map<String, List<String>> parameters,
ConversionService conversionService,
@Nullable BiConsumer<CharSequence, List<String>> onChange) {
this.valuesMap = new LinkedHashMap<>(parameters.size());
this.valuesMap = CollectionUtils.newLinkedHashMap(parameters.size());
this.values = new ConvertibleMultiValuesMap<>(valuesMap, conversionService);
this.onChange = onChange;
for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.micronaut.core.bind.exceptions.UnsatisfiedArgumentException;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Consumes;
Expand Down Expand Up @@ -61,7 +62,6 @@

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -559,7 +559,7 @@ private BoundExecutable bindMethod(HttpRequest<?> request, ArgumentBinderRegistr
}

private Map<Argument<?>, Object> prepareBoundVariables(ExecutableMethod<?, ?> executable, List<?> parameters) {
Map<Argument<?>, Object> preBound = new HashMap<>(executable.getArguments().length);
Map<Argument<?>, Object> preBound = CollectionUtils.newHashMap(executable.getArguments().length);
for (Argument argument : executable.getArguments()) {
Class<?> type = argument.getType();
for (Object object : parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.ObjectUtils;

import java.util.ArrayList;
Expand Down Expand Up @@ -200,7 +201,7 @@ public UriMatchInfo tryMatch(@NonNull String uri) {
return new DefaultUriMatchInfo(uri, Collections.emptyMap(), variables);
} else {
int count = matcher.groupCount();
Map<String, Object> variableMap = new LinkedHashMap<>(count);
Map<String, Object> variableMap = CollectionUtils.newLinkedHashMap(count);
for (int j = 0; j < variables.size(); j++) {
int index = (j * 2) + 2;
if (index > count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package io.micronaut.ast.groovy.utils

import groovy.transform.CompileStatic
import io.micronaut.core.util.ArrayUtils
import io.micronaut.core.util.CollectionUtils
import io.micronaut.inject.visitor.VisitorContext
import org.codehaus.groovy.ast.ClassHelper
import org.codehaus.groovy.ast.ClassNode
Expand Down Expand Up @@ -245,10 +246,10 @@ class AstGenericUtils {

populateTypeArguments(classNode, typeArguments)

Map<String, Map<String, ClassNode>> elements = new LinkedHashMap<>(typeArguments.size())
Map<String, Map<String, ClassNode>> elements = CollectionUtils.newLinkedHashMap(typeArguments.size())
for (Map.Entry<String, Map<String, Object>> entry : typeArguments.entrySet()) {
Map<String, Object> value = entry.getValue()
HashMap<String, ClassNode> submap = new LinkedHashMap<>(value.size())
HashMap<String, ClassNode> submap = CollectionUtils.newLinkedHashMap(value.size())
for (Map.Entry<String, Object> genericEntry : value.entrySet()) {
def v = genericEntry.getValue()
ClassNode te = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,20 @@ protected TypeMirror resolveTypeReference(TypeMirror mirror, Map<String, TypeMir
* @return The type bounds
*/
protected Map<String, TypeMirror> resolveBoundTypes(DeclaredType type) {
Map<String, TypeMirror> boundTypes = new LinkedHashMap<>(2);
TypeElement element = (TypeElement) type.asElement();

List<? extends TypeParameterElement> typeParameters = element.getTypeParameters();
List<? extends TypeMirror> typeArguments = type.getTypeArguments();
if (typeArguments.size() == typeParameters.size()) {
Map<String, TypeMirror> boundTypes = CollectionUtils.newLinkedHashMap(typeParameters.size());
Iterator<? extends TypeMirror> i = typeArguments.iterator();
for (TypeParameterElement typeParameter : typeParameters) {
boundTypes.put(typeParameter.toString(), resolveTypeReference(i.next(), boundTypes));
}
return boundTypes;
}

return boundTypes;
return Collections.emptyMap();
}

private void resolveGenericTypeParameter(Map<String, TypeMirror> resolvedParameters, String parameterName, TypeMirror mirror, Map<String, TypeMirror> boundTypes) {
Expand Down Expand Up @@ -297,7 +298,7 @@ private void populateTypeArguments(TypeElement typeElement, Map<String, Map<Stri
typeArguments.put(name, types);
} else {
List<? extends TypeParameterElement> typeParameters = current.getTypeParameters();
Map<String, TypeMirror> types = new LinkedHashMap<>(typeParameters.size());
Map<String, TypeMirror> types = CollectionUtils.newLinkedHashMap(typeParameters.size());
if (typeParameters.size() == superArguments.size()) {
Iterator<? extends TypeMirror> i = superArguments.iterator();
for (TypeParameterElement typeParameter : typeParameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ private Object resolvePlaceHoldersIfNecessary(Object value) {
if (o instanceof CharSequence) {
i.set(resolvePlaceHoldersIfNecessary(o));
} else if (o instanceof Map<?,?> submap) {
Map<Object, Object> newMap = new LinkedHashMap<>(submap.size());
Map<Object, Object> newMap = CollectionUtils.newLinkedHashMap(submap.size());
for (Map.Entry<?, ?> entry : submap.entrySet()) {
final Object k = entry.getKey();
final Object v = entry.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,9 @@ private void putValues(String annotation, Map<CharSequence, Object> values, Map<
}
} else {
if (!hasValues) {
existing = existing == null ? new LinkedHashMap<>(3) : existing;
existing = existing == null ? CollectionUtils.newLinkedHashMap(3) : existing;
} else {
existing = new LinkedHashMap<>(values.size());
existing = CollectionUtils.newLinkedHashMap(values.size());
existing.putAll(values);
}
currentAnnotationValues.put(annotation, existing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ private static final class ObjectBuilder implements ValueBuilder {

@Override
public JsonNode build() {
Map<String, JsonNode> built = new LinkedHashMap<>(values.size());
Map<String, JsonNode> built = CollectionUtils.newLinkedHashMap(values.size());
for (Map.Entry<String, ValueBuilder> entry : values.entrySet()) {
built.put(entry.getKey(), entry.getValue().build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micronaut.context.annotation.Requires;
import io.micronaut.context.env.Environment;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.health.HealthStatus;
import io.micronaut.management.endpoint.health.HealthEndpoint;
import io.micronaut.management.endpoint.health.HealthLevelOfDetail;
Expand All @@ -29,7 +30,6 @@
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -112,7 +112,7 @@ protected Flux<HealthResult> aggregateResults(HealthIndicator[] indicators) {
* @return The aggregated details for the results
*/
protected Object aggregateDetails(List<HealthResult> results) {
Map<String, Object> details = new HashMap<>(results.size());
Map<String, Object> details = CollectionUtils.newHashMap(results.size());
results.forEach(r -> details.put(r.getName(), buildResult(r.getStatus(), r.getDetails(), HealthLevelOfDetail.STATUS_DESCRIPTION_DETAILS)));
return details;
}
Expand Down