Use lambdas for map entry iteration where possible
See gh-12626
This commit is contained in:
committed by
Phillip Webb
parent
78a94cafe1
commit
69bc19e0ca
@@ -18,13 +18,13 @@ package org.springframework.boot.autoconfigureprocessor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.processing.AbstractProcessor;
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
@@ -34,7 +34,6 @@ import javax.lang.model.element.AnnotationMirror;
|
||||
import javax.lang.model.element.AnnotationValue;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.ElementKind;
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.type.DeclaredType;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
@@ -158,23 +157,19 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<Object> getValues(AnnotationMirror annotation) {
|
||||
List<Object> result = new ArrayList<>();
|
||||
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotation
|
||||
.getElementValues().entrySet()) {
|
||||
return annotation .getElementValues().entrySet().stream().filter(entry -> {
|
||||
String attributeName = entry.getKey().getSimpleName().toString();
|
||||
if ("name".equals(attributeName) || "value".equals(attributeName)) {
|
||||
Object value = entry.getValue().getValue();
|
||||
if (value instanceof List) {
|
||||
for (AnnotationValue annotationValue : (List<AnnotationValue>) value) {
|
||||
result.add(processValue(annotationValue.getValue()));
|
||||
}
|
||||
}
|
||||
else {
|
||||
result.add(processValue(value));
|
||||
}
|
||||
return "name".equals(attributeName) || "value".equals(attributeName);
|
||||
}).map((entry) -> {
|
||||
Object value = entry.getValue().getValue();
|
||||
if (value instanceof List) {
|
||||
return ((List<AnnotationValue>) value).stream().
|
||||
map(annotationValue -> processValue(annotationValue.getValue())).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
else {
|
||||
return Collections.singletonList(processValue(value));
|
||||
}
|
||||
}).flatMap(List::stream).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private Object processValue(Object value) {
|
||||
|
||||
@@ -93,17 +93,9 @@ public class SimpleConfigurationMetadataRepository
|
||||
}
|
||||
else {
|
||||
// Merge properties
|
||||
for (Map.Entry<String, ConfigurationMetadataProperty> entry : group
|
||||
.getProperties().entrySet()) {
|
||||
putIfAbsent(existingGroup.getProperties(), entry.getKey(),
|
||||
entry.getValue());
|
||||
}
|
||||
group.getProperties().forEach((key, value) -> putIfAbsent(existingGroup.getProperties(), key, value));
|
||||
// Merge sources
|
||||
for (Map.Entry<String, ConfigurationMetadataSource> entry : group
|
||||
.getSources().entrySet()) {
|
||||
putIfAbsent(existingGroup.getSources(), entry.getKey(),
|
||||
entry.getValue());
|
||||
}
|
||||
group.getSources().forEach((key, value) -> putIfAbsent(existingGroup.getSources(), key, value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,6 @@ import javax.annotation.processing.RoundEnvironment;
|
||||
import javax.annotation.processing.SupportedAnnotationTypes;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.AnnotationMirror;
|
||||
import javax.lang.model.element.AnnotationValue;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.ElementKind;
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
@@ -282,10 +281,7 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
private void processSimpleTypes(String prefix, TypeElement element,
|
||||
ExecutableElement source, TypeElementMembers members,
|
||||
Map<String, Object> fieldValues) {
|
||||
for (Map.Entry<String, ExecutableElement> entry : members.getPublicGetters()
|
||||
.entrySet()) {
|
||||
String name = entry.getKey();
|
||||
ExecutableElement getter = entry.getValue();
|
||||
members.getPublicGetters().forEach((name, getter) -> {
|
||||
TypeMirror returnType = getter.getReturnType();
|
||||
ExecutableElement setter = members.getPublicSetter(name, returnType);
|
||||
VariableElement field = members.getFields().get(name);
|
||||
@@ -305,7 +301,7 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
dataType, sourceType, null, description, defaultValue,
|
||||
(deprecated ? getItemDeprecation(getter) : null)));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private ItemDeprecation getItemDeprecation(ExecutableElement getter) {
|
||||
@@ -325,11 +321,9 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
private void processSimpleLombokTypes(String prefix, TypeElement element,
|
||||
ExecutableElement source, TypeElementMembers members,
|
||||
Map<String, Object> fieldValues) {
|
||||
for (Map.Entry<String, VariableElement> entry : members.getFields().entrySet()) {
|
||||
String name = entry.getKey();
|
||||
VariableElement field = entry.getValue();
|
||||
members.getFields().forEach((name, field) -> {
|
||||
if (!isLombokField(field, element)) {
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
TypeMirror returnType = field.asType();
|
||||
Element returnTypeElement = this.processingEnv.getTypeUtils()
|
||||
@@ -348,32 +342,27 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
dataType, sourceType, null, description, defaultValue,
|
||||
(deprecated ? new ItemDeprecation() : null)));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void processNestedTypes(String prefix, TypeElement element,
|
||||
ExecutableElement source, TypeElementMembers members) {
|
||||
for (Map.Entry<String, ExecutableElement> entry : members.getPublicGetters()
|
||||
.entrySet()) {
|
||||
String name = entry.getKey();
|
||||
ExecutableElement getter = entry.getValue();
|
||||
members.getPublicGetters().forEach((name, getter) -> {
|
||||
VariableElement field = members.getFields().get(name);
|
||||
processNestedType(prefix, element, source, name, getter, field,
|
||||
getter.getReturnType());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void processNestedLombokTypes(String prefix, TypeElement element,
|
||||
ExecutableElement source, TypeElementMembers members) {
|
||||
for (Map.Entry<String, VariableElement> entry : members.getFields().entrySet()) {
|
||||
String name = entry.getKey();
|
||||
VariableElement field = entry.getValue();
|
||||
members.getFields().forEach((name, field) -> {
|
||||
if (isLombokField(field, element)) {
|
||||
ExecutableElement getter = members.getPublicGetter(name, field.asType());
|
||||
processNestedType(prefix, element, source, name, getter, field,
|
||||
field.asType());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private boolean isLombokField(VariableElement field, TypeElement element) {
|
||||
@@ -544,11 +533,8 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
|
||||
private Map<String, Object> getAnnotationElementValues(AnnotationMirror annotation) {
|
||||
Map<String, Object> values = new LinkedHashMap<>();
|
||||
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotation
|
||||
.getElementValues().entrySet()) {
|
||||
values.put(entry.getKey().getSimpleName().toString(),
|
||||
entry.getValue().getValue());
|
||||
}
|
||||
annotation.getElementValues().forEach((key, value) ->
|
||||
values.put(key.getSimpleName().toString(), value.getValue()));
|
||||
return values;
|
||||
}
|
||||
|
||||
|
||||
@@ -78,13 +78,11 @@ class TypeElementMembers {
|
||||
processField(field);
|
||||
}
|
||||
try {
|
||||
Map<String, Object> fieldValues = this.fieldValuesParser
|
||||
.getFieldValues(element);
|
||||
for (Map.Entry<String, Object> entry : fieldValues.entrySet()) {
|
||||
if (!this.fieldValues.containsKey(entry.getKey())) {
|
||||
this.fieldValues.put(entry.getKey(), entry.getValue());
|
||||
this.fieldValuesParser.getFieldValues(element).forEach((key, value) -> {
|
||||
if (!this.fieldValues.containsKey(key)) {
|
||||
this.fieldValues.put(key, value);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// continue
|
||||
|
||||
@@ -62,9 +62,7 @@ class TypeUtils {
|
||||
|
||||
static {
|
||||
Map<String, TypeKind> primitives = new HashMap<>();
|
||||
for (Map.Entry<TypeKind, Class<?>> entry : PRIMITIVE_WRAPPERS.entrySet()) {
|
||||
primitives.put(entry.getValue().getName(), entry.getKey());
|
||||
}
|
||||
PRIMITIVE_WRAPPERS.forEach((key, value) -> primitives.put(value.getName(), key));
|
||||
WRAPPER_TO_PRIMITIVE = primitives;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.gradle.api.Action;
|
||||
import org.gradle.api.Project;
|
||||
@@ -110,9 +109,7 @@ public class BuildInfo extends ConventionTask {
|
||||
|
||||
private Map<String, String> coerceToStringValues(Map<String, Object> input) {
|
||||
Map<String, String> output = new HashMap<>();
|
||||
for (Entry<String, Object> entry : input.entrySet()) {
|
||||
output.put(entry.getKey(), entry.getValue().toString());
|
||||
}
|
||||
input.forEach((key, value) -> output.put(key, value.toString()));
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@ import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
@@ -79,10 +78,7 @@ public final class BuildPropertiesWriter {
|
||||
DateTimeFormatter.ISO_INSTANT.format(project.getTime()));
|
||||
}
|
||||
if (project.getAdditionalProperties() != null) {
|
||||
for (Map.Entry<String, String> entry : project.getAdditionalProperties()
|
||||
.entrySet()) {
|
||||
properties.put("build." + entry.getKey(), entry.getValue());
|
||||
}
|
||||
project.getAdditionalProperties().forEach((key, value) -> properties.put("build." + key, value));
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
@@ -118,11 +114,11 @@ public final class BuildPropertiesWriter {
|
||||
private static void validateAdditionalProperties(
|
||||
Map<String, String> additionalProperties) {
|
||||
if (additionalProperties != null) {
|
||||
for (Entry<String, String> property : additionalProperties.entrySet()) {
|
||||
if (property.getValue() == null) {
|
||||
throw new NullAdditionalPropertyValueException(property.getKey());
|
||||
additionalProperties.forEach((key, value) -> {
|
||||
if (value == null) {
|
||||
throw new NullAdditionalPropertyValueException(key);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ package org.springframework.boot.maven;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Properties;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarOutputStream;
|
||||
@@ -64,13 +63,13 @@ public class PropertiesMergingResourceTransformer implements ResourceTransformer
|
||||
Properties properties = new Properties();
|
||||
properties.load(is);
|
||||
is.close();
|
||||
for (Entry<Object, Object> entry : properties.entrySet()) {
|
||||
String name = (String) entry.getKey();
|
||||
String value = (String) entry.getValue();
|
||||
properties.forEach((key, valueObject) -> {
|
||||
String name = (String) key;
|
||||
String value = (String) valueObject;
|
||||
String existing = this.data.getProperty(name);
|
||||
this.data.setProperty(name,
|
||||
existing == null ? value : existing + "," + value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -140,13 +140,10 @@ public final class Verify {
|
||||
}
|
||||
|
||||
private ZipEntry getEntryStartingWith(String entryName) {
|
||||
for (Map.Entry<String, ZipEntry> entry : this.content.entrySet()) {
|
||||
if (entry.getKey().startsWith(entryName)) {
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException(
|
||||
"Unable to find entry starting with " + entryName);
|
||||
return this.content.entrySet().stream().
|
||||
filter(entry -> entry.getKey().startsWith(entryName)).
|
||||
map(Map.Entry::getValue).findFirst().orElseThrow(() ->
|
||||
new IllegalStateException("Unable to find entry starting with " + entryName));
|
||||
}
|
||||
|
||||
public boolean hasEntry(String entry) {
|
||||
|
||||
Reference in New Issue
Block a user