From edb660863b9c0f161927ce06c3b8c7313b3429a7 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 24 Jan 2014 16:59:42 +0100 Subject: [PATCH] Polishing --- .../FormattingConversionServiceTests.java | 24 ++++++++++++- .../io/support/SpringFactoriesLoader.java | 8 ++--- .../util/SystemPropertyUtils.java | 30 +++++++++------- .../support/ReflectiveMethodResolver.java | 34 ++++++++++--------- .../support/WebApplicationContextUtils.java | 8 ++--- .../PortletApplicationContextUtils.java | 29 ++++++++-------- 6 files changed, 80 insertions(+), 53 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java b/spring-context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java index 417ded1a45..a2930e3e7e 100644 --- a/spring-context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java +++ b/spring-context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -306,6 +306,28 @@ public class FormattingConversionServiceTests { TypeDescriptor.valueOf(String.class)); } + @Test + public void testRegisterDefaultValueViaFormatter() { + registerDefaultValue(Date.class, new Date()); + } + + private void registerDefaultValue(Class clazz, final T defaultValue) { + formattingService.addFormatterForFieldType(clazz, new Formatter() { + @Override + public T parse(String text, Locale locale) throws ParseException { + return defaultValue; + } + @Override + public String print(T t, Locale locale) { + return defaultValue.toString(); + } + @Override + public String toString() { + return defaultValue.toString(); + } + }); + } + public static class ValueBean { diff --git a/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java b/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java index 13ae7ca91f..6c33d76817 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -59,8 +59,8 @@ public abstract class SpringFactoriesLoader { /** - * Loads the factory implementations of the given type from the default location, using - * the given class loader. + * Load the factory implementations of the given type from the default location, + * using the given class loader. *

The returned factories are ordered in accordance with the {@link OrderComparator}. * @param factoryClass the interface or abstract class representing the factory * @param classLoader the ClassLoader to use for loading (can be {@code null} to use the default) @@ -82,7 +82,7 @@ public abstract class SpringFactoriesLoader { return result; } - private static List loadFactoryNames(Class factoryClass, ClassLoader classLoader) { + public static List loadFactoryNames(Class factoryClass, ClassLoader classLoader) { String factoryClassName = factoryClass.getName(); try { List result = new ArrayList(); diff --git a/spring-core/src/main/java/org/springframework/util/SystemPropertyUtils.java b/spring-core/src/main/java/org/springframework/util/SystemPropertyUtils.java index 19ee4478e7..6c3acd7e1d 100644 --- a/spring-core/src/main/java/org/springframework/util/SystemPropertyUtils.java +++ b/spring-core/src/main/java/org/springframework/util/SystemPropertyUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -16,14 +16,12 @@ package org.springframework.util; -import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; - /** * Helper class for resolving placeholders in texts. Usually applied to file paths. * - *

A text may contain {@code ${...}} placeholders, to be resolved as system properties: e.g. - * {@code ${user.dir}}. Default values can be supplied using the ":" separator between key - * and value. + *

A text may contain {@code ${...}} placeholders, to be resolved as system properties: + * e.g. {@code ${user.dir}}. Default values can be supplied using the ":" separator + * between key and value. * * @author Juergen Hoeller * @author Rob Harrop @@ -53,7 +51,8 @@ public abstract class SystemPropertyUtils { /** - * Resolve ${...} placeholders in the given text, replacing them with corresponding system property values. + * Resolve {@code ${...}} placeholders in the given text, replacing them with + * corresponding system property values. * @param text the String to resolve * @return the resolved String * @see #PLACEHOLDER_PREFIX @@ -65,15 +64,16 @@ public abstract class SystemPropertyUtils { } /** - * Resolve ${...} placeholders in the given text, replacing them with corresponding system property values. - * Unresolvable placeholders with no default value are ignored and passed through unchanged if the - * flag is set to true. + * Resolve {@code ${...}} placeholders in the given text, replacing them with + * corresponding system property values. Unresolvable placeholders with no default + * value are ignored and passed through unchanged if the flag is set to {@code true}. * @param text the String to resolve - * @param ignoreUnresolvablePlaceholders flag to determine is unresolved placeholders are ignored + * @param ignoreUnresolvablePlaceholders whether unresolved placeholders are to be ignored * @return the resolved String * @see #PLACEHOLDER_PREFIX * @see #PLACEHOLDER_SUFFIX - * @throws IllegalArgumentException if there is an unresolvable placeholder and the flag is false + * @throws IllegalArgumentException if there is an unresolvable placeholder + * and the "ignoreUnresolvablePlaceholders" flag is {@code false} */ public static String resolvePlaceholders(String text, boolean ignoreUnresolvablePlaceholders) { PropertyPlaceholderHelper helper = (ignoreUnresolvablePlaceholders ? nonStrictHelper : strictHelper); @@ -81,7 +81,11 @@ public abstract class SystemPropertyUtils { } - private static class SystemPropertyPlaceholderResolver implements PlaceholderResolver { + /** + * PlaceholderResolver implementation that resolves against system properties + * and system environment variables. + */ + private static class SystemPropertyPlaceholderResolver implements PropertyPlaceholderHelper.PlaceholderResolver { private final String text; diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java b/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java index 7ec088fd4d..042490cffb 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java @@ -53,20 +53,21 @@ import org.springframework.expression.spel.SpelMessage; */ public class ReflectiveMethodResolver implements MethodResolver { - private Map, MethodFilter> filters = null; - // Using distance will ensure a more accurate match is discovered, // more closely following the Java rules. - private boolean useDistance = false; + private final boolean useDistance; + + private Map, MethodFilter> filters; public ReflectiveMethodResolver() { + this.useDistance = false; } /** * This constructors allows the ReflectiveMethodResolver to be configured such that it will * use a distance computation to check which is the better of two close matches (when there - * are multiple matches). Using the distance computation is intended to ensure matches + * are multiple matches). Using the distance computation is intended to ensure matches * are more closely representative of what a Java compiler would do when taking into * account boxing/unboxing and whether the method candidates are declared to handle a * supertype of the type (of the argument) being passed in. @@ -77,6 +78,19 @@ public class ReflectiveMethodResolver implements MethodResolver { } + public void registerMethodFilter(Class type, MethodFilter filter) { + if (this.filters == null) { + this.filters = new HashMap, MethodFilter>(); + } + if (filter != null) { + this.filters.put(type, filter); + } + else { + this.filters.remove(type); + } + } + + /** * Locate a method on a type. There are three kinds of match that might occur: *

    @@ -187,18 +201,6 @@ public class ReflectiveMethodResolver implements MethodResolver { } } - public void registerMethodFilter(Class type, MethodFilter filter) { - if (this.filters == null) { - this.filters = new HashMap, MethodFilter>(); - } - if (filter == null) { - this.filters.remove(type); - } - else { - this.filters.put(type,filter); - } - } - private Method[] getMethods(Class type, Object targetObject) { if (targetObject instanceof Class) { Set methods = new HashSet(); diff --git a/spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java b/spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java index 16d500e626..0a023c20f3 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java @@ -45,10 +45,9 @@ import org.springframework.web.context.request.SessionScope; import org.springframework.web.context.request.WebRequest; /** - * Convenience methods for retrieving the root - * {@link org.springframework.web.context.WebApplicationContext} for a given - * {@code ServletContext}. This is useful for programmatically accessing a - * Spring application context from within custom web views or MVC actions. + * Convenience methods for retrieving the root {@link WebApplicationContext} for + * a given {@link ServletContext}. This is useful for programmatically accessing + * a Spring application context from within custom web views or MVC actions. * *

    Note that there are more convenient ways of accessing the root context for * many web frameworks, either part of Spring or available as an external library. @@ -232,6 +231,7 @@ public abstract class WebApplicationContextUtils { */ public static void initServletPropertySources( MutablePropertySources propertySources, ServletContext servletContext) { + initServletPropertySources(propertySources, servletContext, null); } diff --git a/spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/context/PortletApplicationContextUtils.java b/spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/context/PortletApplicationContextUtils.java index 40d39905e1..f25303d5bb 100644 --- a/spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/context/PortletApplicationContextUtils.java +++ b/spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/context/PortletApplicationContextUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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,7 +21,6 @@ import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; - import javax.portlet.PortletConfig; import javax.portlet.PortletContext; import javax.portlet.PortletRequest; @@ -42,9 +41,9 @@ import org.springframework.web.context.request.WebRequest; import org.springframework.web.context.support.WebApplicationContextUtils; /** - * Convenience methods for retrieving the root WebApplicationContext for a given - * PortletContext. This is e.g. useful for accessing a Spring context from - * within custom Portlet implementations. + * Convenience methods for retrieving the root {@link WebApplicationContext} for + * a given {@link PortletContext}. This is useful for programmatically accessing + * a Spring application context from within custom Portlet implementations. * * @author Juergen Hoeller * @author John A. Lewis @@ -98,7 +97,7 @@ public abstract class PortletApplicationContextUtils { * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE */ public static ApplicationContext getRequiredWebApplicationContext(PortletContext pc) - throws IllegalStateException { + throws IllegalStateException { ApplicationContext wac = getWebApplicationContext(pc); if (wac == null) { @@ -156,16 +155,16 @@ public abstract class PortletApplicationContextUtils { if (!bf.containsBean(WebApplicationContext.CONTEXT_PARAMETERS_BEAN_NAME)) { Map parameterMap = new HashMap(); if (pc != null) { - Enumeration paramNameEnum = pc.getInitParameterNames(); + Enumeration paramNameEnum = pc.getInitParameterNames(); while (paramNameEnum.hasMoreElements()) { - String paramName = (String) paramNameEnum.nextElement(); + String paramName = paramNameEnum.nextElement(); parameterMap.put(paramName, pc.getInitParameter(paramName)); } } if (config != null) { - Enumeration paramNameEnum = config.getInitParameterNames(); + Enumeration paramNameEnum = config.getInitParameterNames(); while (paramNameEnum.hasMoreElements()) { - String paramName = (String) paramNameEnum.nextElement(); + String paramName = paramNameEnum.nextElement(); parameterMap.put(paramName, config.getInitParameter(paramName)); } } @@ -176,9 +175,9 @@ public abstract class PortletApplicationContextUtils { if (!bf.containsBean(WebApplicationContext.CONTEXT_ATTRIBUTES_BEAN_NAME)) { Map attributeMap = new HashMap(); if (pc != null) { - Enumeration attrNameEnum = pc.getAttributeNames(); + Enumeration attrNameEnum = pc.getAttributeNames(); while (attrNameEnum.hasMoreElements()) { - String attrName = (String) attrNameEnum.nextElement(); + String attrName = attrNameEnum.nextElement(); attributeMap.put(attrName, pc.getAttribute(attrName)); } } @@ -211,15 +210,15 @@ public abstract class PortletApplicationContextUtils { */ public static void initPortletPropertySources(MutablePropertySources propertySources, ServletContext servletContext, PortletContext portletContext, PortletConfig portletConfig) { - Assert.notNull(propertySources, "propertySources must not be null"); + Assert.notNull(propertySources, "propertySources must not be null"); WebApplicationContextUtils.initServletPropertySources(propertySources, servletContext); - if(portletContext != null && propertySources.contains(StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME)) { + if (portletContext != null && propertySources.contains(StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME)) { propertySources.replace(StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME, new PortletContextPropertySource(StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME, portletContext)); } - if(portletConfig != null && propertySources.contains(StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME)) { + if (portletConfig != null && propertySources.contains(StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME)) { propertySources.replace(StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME, new PortletConfigPropertySource(StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME, portletConfig)); }