From 8f1f8dd6805ae944a40b9cb5f90cfec85a2edc93 Mon Sep 17 00:00:00 2001 From: mnhock Date: Sat, 19 Dec 2015 09:12:41 +0100 Subject: [PATCH 1/2] Use entrySet() rather than using keySet() and then calling get(key) Closes gh-4813 --- .../endpoint/RequestMappingEndpoint.java | 17 +++++++++-------- .../endpoint/jmx/EndpointMBeanExporter.java | 7 ++++--- .../web/WebMvcAutoConfiguration.java | 10 ++++++---- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/RequestMappingEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/RequestMappingEndpoint.java index a4d65af6b75..b7ea7fa0517 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/RequestMappingEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/RequestMappingEndpoint.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import org.springframework.aop.support.AopUtils; import org.springframework.beans.BeansException; @@ -92,11 +93,11 @@ public class RequestMappingEndpoint extends AbstractEndpoint Map methods = applicationContext .getBean(name, AbstractHandlerMethodMapping.class) .getHandlerMethods(); - for (Object key : methods.keySet()) { + for (Entry method : methods.entrySet()) { Map map = new LinkedHashMap(); map.put("bean", name); - map.put("method", methods.get(key).toString()); - result.put(key.toString(), map); + map.put("method", method.getValue().toString()); + result.put( method.getKey().toString(), map); } } } @@ -107,11 +108,11 @@ public class RequestMappingEndpoint extends AbstractEndpoint if (applicationContext != null) { Map mappings = applicationContext .getBeansOfType(AbstractUrlHandlerMapping.class); - for (String name : mappings.keySet()) { - AbstractUrlHandlerMapping mapping = mappings.get(name); - Map handlers = getHandlerMap(mapping); - for (String key : handlers.keySet()) { - result.put(key, Collections.singletonMap("bean", name)); + for (Entry mapping : mappings.entrySet()) { + Map handlers = getHandlerMap(mapping.getValue()); + for (Entry handler : handlers.entrySet()) { + result.put(handler.getKey(), + Collections.singletonMap("bean", mapping.getKey())); } } } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanExporter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanExporter.java index fea94d536c3..102eeaa3ab3 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanExporter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanExporter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.springframework.boot.actuate.endpoint.jmx; import java.util.HashSet; import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import java.util.concurrent.locks.ReentrantLock; @@ -241,8 +242,8 @@ public class EndpointMBeanExporter extends MBeanExporter } StringBuilder builder = new StringBuilder(); - for (Object key : this.objectNameStaticProperties.keySet()) { - builder.append("," + key + "=" + this.objectNameStaticProperties.get(key)); + for (Entry name : this.objectNameStaticProperties.entrySet()) { + builder.append("," + name.getKey() + "=" + name.getValue()); } return builder.toString(); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java index 51839a223f3..68b229feb90 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import javax.servlet.Servlet; @@ -132,7 +133,8 @@ public class WebMvcAutoConfiguration { @EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class }) public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter { - private static final Log logger = LogFactory.getLog(WebMvcConfigurerAdapter.class); + private static final Log logger = LogFactory + .getLog(WebMvcConfigurerAdapter.class); @Autowired private ResourceProperties resourceProperties = new ResourceProperties(); @@ -162,8 +164,8 @@ public class WebMvcAutoConfiguration { @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { Map mediaTypes = this.mvcProperties.getMediaTypes(); - for (String extension : mediaTypes.keySet()) { - configurer.mediaType(extension, mediaTypes.get(extension)); + for (Entry mediaType : mediaTypes.entrySet()) { + configurer.mediaType(mediaType.getKey(), mediaType.getValue()); } } From 93ef7951593145800e66e6b7f15769686dc823a5 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 15 Jan 2016 16:58:21 +0000 Subject: [PATCH 2/2] More use entrySet() rather than using keySet() and get(key) This commit replaces some more occurrances of keySet() and get(key) with more efficient usage of the map's entry set. See gh-4813 --- .../endpoint/RequestMappingEndpoint.java | 18 ++++++------- .../ServiceCapabilitiesReportGenerator.java | 25 ++++++++++++++----- .../PropertiesMergingResourceTransformer.java | 10 +++++--- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/RequestMappingEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/RequestMappingEndpoint.java index b7ea7fa0517..a27701e2590 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/RequestMappingEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/RequestMappingEndpoint.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,7 @@ import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping; * {@link Endpoint} to expose Spring MVC mappings. * * @author Dave Syer + * @author Andy Wilkinson */ @ConfigurationProperties(prefix = "endpoints.mappings", ignoreUnknownFields = false) public class RequestMappingEndpoint extends AbstractEndpoint> @@ -84,20 +85,19 @@ public class RequestMappingEndpoint extends AbstractEndpoint return result; } + @SuppressWarnings("rawtypes") protected void extractMethodMappings(ApplicationContext applicationContext, Map result) { if (applicationContext != null) { - for (String name : applicationContext - .getBeansOfType(AbstractHandlerMethodMapping.class).keySet()) { + for (Entry bean : applicationContext + .getBeansOfType(AbstractHandlerMethodMapping.class).entrySet()) { @SuppressWarnings("unchecked") - Map methods = applicationContext - .getBean(name, AbstractHandlerMethodMapping.class) - .getHandlerMethods(); + Map methods = bean.getValue().getHandlerMethods(); for (Entry method : methods.entrySet()) { Map map = new LinkedHashMap(); - map.put("bean", name); - map.put("method", method.getValue().toString()); - result.put( method.getKey().toString(), map); + map.put("bean", bean.getKey()); + map.put("method", method.getValue().toString()); + result.put(method.getKey().toString(), map); } } } diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ServiceCapabilitiesReportGenerator.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ServiceCapabilitiesReportGenerator.java index a619f18c389..9c33682a541 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ServiceCapabilitiesReportGenerator.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ServiceCapabilitiesReportGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,11 +23,15 @@ import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; +import java.util.SortedSet; +import java.util.TreeSet; /** * A helper class generating a report from the meta-data of a particular service. * * @author Stephane Nicoll + * @author Andy Wilkinson * @since 1.2.0 */ class ServiceCapabilitiesReportGenerator { @@ -104,11 +108,20 @@ class ServiceCapabilitiesReportGenerator { StringBuilder report) { report.append("Available project types:" + NEW_LINE); report.append("------------------------" + NEW_LINE); - List typeIds = new ArrayList(metadata.getProjectTypes().keySet()); - Collections.sort(typeIds); - for (String typeId : typeIds) { - ProjectType type = metadata.getProjectTypes().get(typeId); - report.append(typeId + " - " + type.getName()); + SortedSet> entries = new TreeSet>( + new Comparator>() { + + @Override + public int compare(Entry o1, + Entry o2) { + return o1.getKey().compareTo(o2.getKey()); + } + + }); + entries.addAll(metadata.getProjectTypes().entrySet()); + for (Entry entry : entries) { + ProjectType type = entry.getValue(); + report.append(entry.getKey() + " - " + type.getName()); if (!type.getTags().isEmpty()) { reportTags(report, type); } diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/PropertiesMergingResourceTransformer.java b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/PropertiesMergingResourceTransformer.java index c8a12bea203..6215a1deff8 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/PropertiesMergingResourceTransformer.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/PropertiesMergingResourceTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ 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; @@ -32,6 +33,7 @@ import org.apache.maven.plugins.shade.resource.ResourceTransformer; * to be merged without losing any information. * * @author Dave Syer + * @author Andy Wilkinson */ public class PropertiesMergingResourceTransformer implements ResourceTransformer { @@ -62,9 +64,9 @@ public class PropertiesMergingResourceTransformer implements ResourceTransformer Properties properties = new Properties(); properties.load(is); is.close(); - for (Object key : properties.keySet()) { - String name = (String) key; - String value = properties.getProperty(name); + for (Entry entry : properties.entrySet()) { + String name = (String) entry.getKey(); + String value = (String) entry.getValue(); String existing = this.data.getProperty(name); this.data.setProperty(name, existing == null ? value : existing + "," + value);