Skip to content

Commit edb6608

Browse files
committed
Polishing
1 parent 866c784 commit edb6608

File tree

6 files changed

+80
-53
lines changed

6 files changed

+80
-53
lines changed

spring-context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -306,6 +306,28 @@ public Date convert(MyDate source) {
306306
TypeDescriptor.valueOf(String.class));
307307
}
308308

309+
@Test
310+
public void testRegisterDefaultValueViaFormatter() {
311+
registerDefaultValue(Date.class, new Date());
312+
}
313+
314+
private <T> void registerDefaultValue(Class<T> clazz, final T defaultValue) {
315+
formattingService.addFormatterForFieldType(clazz, new Formatter<T>() {
316+
@Override
317+
public T parse(String text, Locale locale) throws ParseException {
318+
return defaultValue;
319+
}
320+
@Override
321+
public String print(T t, Locale locale) {
322+
return defaultValue.toString();
323+
}
324+
@Override
325+
public String toString() {
326+
return defaultValue.toString();
327+
}
328+
});
329+
}
330+
309331

310332
public static class ValueBean {
311333

spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,8 +59,8 @@ public abstract class SpringFactoriesLoader {
5959

6060

6161
/**
62-
* Loads the factory implementations of the given type from the default location, using
63-
* the given class loader.
62+
* Load the factory implementations of the given type from the default location,
63+
* using the given class loader.
6464
* <p>The returned factories are ordered in accordance with the {@link OrderComparator}.
6565
* @param factoryClass the interface or abstract class representing the factory
6666
* @param classLoader the ClassLoader to use for loading (can be {@code null} to use the default)
@@ -82,7 +82,7 @@ public static <T> List<T> loadFactories(Class<T> factoryClass, ClassLoader class
8282
return result;
8383
}
8484

85-
private static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
85+
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
8686
String factoryClassName = factoryClass.getName();
8787
try {
8888
List<String> result = new ArrayList<String>();

spring-core/src/main/java/org/springframework/util/SystemPropertyUtils.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,14 +16,12 @@
1616

1717
package org.springframework.util;
1818

19-
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
20-
2119
/**
2220
* Helper class for resolving placeholders in texts. Usually applied to file paths.
2321
*
24-
* <p>A text may contain {@code ${...}} placeholders, to be resolved as system properties: e.g.
25-
* {@code ${user.dir}}. Default values can be supplied using the ":" separator between key
26-
* and value.
22+
* <p>A text may contain {@code ${...}} placeholders, to be resolved as system properties:
23+
* e.g. {@code ${user.dir}}. Default values can be supplied using the ":" separator
24+
* between key and value.
2725
*
2826
* @author Juergen Hoeller
2927
* @author Rob Harrop
@@ -53,7 +51,8 @@ public abstract class SystemPropertyUtils {
5351

5452

5553
/**
56-
* Resolve ${...} placeholders in the given text, replacing them with corresponding system property values.
54+
* Resolve {@code ${...}} placeholders in the given text, replacing them with
55+
* corresponding system property values.
5756
* @param text the String to resolve
5857
* @return the resolved String
5958
* @see #PLACEHOLDER_PREFIX
@@ -65,23 +64,28 @@ public static String resolvePlaceholders(String text) {
6564
}
6665

6766
/**
68-
* Resolve ${...} placeholders in the given text, replacing them with corresponding system property values.
69-
* Unresolvable placeholders with no default value are ignored and passed through unchanged if the
70-
* flag is set to true.
67+
* Resolve {@code ${...}} placeholders in the given text, replacing them with
68+
* corresponding system property values. Unresolvable placeholders with no default
69+
* value are ignored and passed through unchanged if the flag is set to {@code true}.
7170
* @param text the String to resolve
72-
* @param ignoreUnresolvablePlaceholders flag to determine is unresolved placeholders are ignored
71+
* @param ignoreUnresolvablePlaceholders whether unresolved placeholders are to be ignored
7372
* @return the resolved String
7473
* @see #PLACEHOLDER_PREFIX
7574
* @see #PLACEHOLDER_SUFFIX
76-
* @throws IllegalArgumentException if there is an unresolvable placeholder and the flag is false
75+
* @throws IllegalArgumentException if there is an unresolvable placeholder
76+
* and the "ignoreUnresolvablePlaceholders" flag is {@code false}
7777
*/
7878
public static String resolvePlaceholders(String text, boolean ignoreUnresolvablePlaceholders) {
7979
PropertyPlaceholderHelper helper = (ignoreUnresolvablePlaceholders ? nonStrictHelper : strictHelper);
8080
return helper.replacePlaceholders(text, new SystemPropertyPlaceholderResolver(text));
8181
}
8282

8383

84-
private static class SystemPropertyPlaceholderResolver implements PlaceholderResolver {
84+
/**
85+
* PlaceholderResolver implementation that resolves against system properties
86+
* and system environment variables.
87+
*/
88+
private static class SystemPropertyPlaceholderResolver implements PropertyPlaceholderHelper.PlaceholderResolver {
8589

8690
private final String text;
8791

spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,21 @@
5353
*/
5454
public class ReflectiveMethodResolver implements MethodResolver {
5555

56-
private Map<Class<?>, MethodFilter> filters = null;
57-
5856
// Using distance will ensure a more accurate match is discovered,
5957
// more closely following the Java rules.
60-
private boolean useDistance = false;
58+
private final boolean useDistance;
59+
60+
private Map<Class<?>, MethodFilter> filters;
6161

6262

6363
public ReflectiveMethodResolver() {
64+
this.useDistance = false;
6465
}
6566

6667
/**
6768
* This constructors allows the ReflectiveMethodResolver to be configured such that it will
6869
* use a distance computation to check which is the better of two close matches (when there
69-
* are multiple matches). Using the distance computation is intended to ensure matches
70+
* are multiple matches). Using the distance computation is intended to ensure matches
7071
* are more closely representative of what a Java compiler would do when taking into
7172
* account boxing/unboxing and whether the method candidates are declared to handle a
7273
* supertype of the type (of the argument) being passed in.
@@ -77,6 +78,19 @@ public ReflectiveMethodResolver(boolean useDistance) {
7778
}
7879

7980

81+
public void registerMethodFilter(Class<?> type, MethodFilter filter) {
82+
if (this.filters == null) {
83+
this.filters = new HashMap<Class<?>, MethodFilter>();
84+
}
85+
if (filter != null) {
86+
this.filters.put(type, filter);
87+
}
88+
else {
89+
this.filters.remove(type);
90+
}
91+
}
92+
93+
8094
/**
8195
* Locate a method on a type. There are three kinds of match that might occur:
8296
* <ol>
@@ -187,18 +201,6 @@ else if (matchRequiringConversion != null) {
187201
}
188202
}
189203

190-
public void registerMethodFilter(Class<?> type, MethodFilter filter) {
191-
if (this.filters == null) {
192-
this.filters = new HashMap<Class<?>, MethodFilter>();
193-
}
194-
if (filter == null) {
195-
this.filters.remove(type);
196-
}
197-
else {
198-
this.filters.put(type,filter);
199-
}
200-
}
201-
202204
private Method[] getMethods(Class<?> type, Object targetObject) {
203205
if (targetObject instanceof Class) {
204206
Set<Method> methods = new HashSet<Method>();

spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@
4545
import org.springframework.web.context.request.WebRequest;
4646

4747
/**
48-
* Convenience methods for retrieving the root
49-
* {@link org.springframework.web.context.WebApplicationContext} for a given
50-
* {@code ServletContext}. This is useful for programmatically accessing a
51-
* Spring application context from within custom web views or MVC actions.
48+
* Convenience methods for retrieving the root {@link WebApplicationContext} for
49+
* a given {@link ServletContext}. This is useful for programmatically accessing
50+
* a Spring application context from within custom web views or MVC actions.
5251
*
5352
* <p>Note that there are more convenient ways of accessing the root context for
5453
* many web frameworks, either part of Spring or available as an external library.
@@ -232,6 +231,7 @@ public static void registerEnvironmentBeans(
232231
*/
233232
public static void initServletPropertySources(
234233
MutablePropertySources propertySources, ServletContext servletContext) {
234+
235235
initServletPropertySources(propertySources, servletContext, null);
236236
}
237237

spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/context/PortletApplicationContextUtils.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,6 @@
2121
import java.util.Enumeration;
2222
import java.util.HashMap;
2323
import java.util.Map;
24-
2524
import javax.portlet.PortletConfig;
2625
import javax.portlet.PortletContext;
2726
import javax.portlet.PortletRequest;
@@ -42,9 +41,9 @@
4241
import org.springframework.web.context.support.WebApplicationContextUtils;
4342

4443
/**
45-
* Convenience methods for retrieving the root WebApplicationContext for a given
46-
* PortletContext. This is e.g. useful for accessing a Spring context from
47-
* within custom Portlet implementations.
44+
* Convenience methods for retrieving the root {@link WebApplicationContext} for
45+
* a given {@link PortletContext}. This is useful for programmatically accessing
46+
* a Spring application context from within custom Portlet implementations.
4847
*
4948
* @author Juergen Hoeller
5049
* @author John A. Lewis
@@ -98,7 +97,7 @@ public static ApplicationContext getWebApplicationContext(PortletContext pc) {
9897
* @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
9998
*/
10099
public static ApplicationContext getRequiredWebApplicationContext(PortletContext pc)
101-
throws IllegalStateException {
100+
throws IllegalStateException {
102101

103102
ApplicationContext wac = getWebApplicationContext(pc);
104103
if (wac == null) {
@@ -156,16 +155,16 @@ static void registerEnvironmentBeans(
156155
if (!bf.containsBean(WebApplicationContext.CONTEXT_PARAMETERS_BEAN_NAME)) {
157156
Map<String, String> parameterMap = new HashMap<String, String>();
158157
if (pc != null) {
159-
Enumeration paramNameEnum = pc.getInitParameterNames();
158+
Enumeration<String> paramNameEnum = pc.getInitParameterNames();
160159
while (paramNameEnum.hasMoreElements()) {
161-
String paramName = (String) paramNameEnum.nextElement();
160+
String paramName = paramNameEnum.nextElement();
162161
parameterMap.put(paramName, pc.getInitParameter(paramName));
163162
}
164163
}
165164
if (config != null) {
166-
Enumeration paramNameEnum = config.getInitParameterNames();
165+
Enumeration<String> paramNameEnum = config.getInitParameterNames();
167166
while (paramNameEnum.hasMoreElements()) {
168-
String paramName = (String) paramNameEnum.nextElement();
167+
String paramName = paramNameEnum.nextElement();
169168
parameterMap.put(paramName, config.getInitParameter(paramName));
170169
}
171170
}
@@ -176,9 +175,9 @@ static void registerEnvironmentBeans(
176175
if (!bf.containsBean(WebApplicationContext.CONTEXT_ATTRIBUTES_BEAN_NAME)) {
177176
Map<String, Object> attributeMap = new HashMap<String, Object>();
178177
if (pc != null) {
179-
Enumeration attrNameEnum = pc.getAttributeNames();
178+
Enumeration<String> attrNameEnum = pc.getAttributeNames();
180179
while (attrNameEnum.hasMoreElements()) {
181-
String attrName = (String) attrNameEnum.nextElement();
180+
String attrName = attrNameEnum.nextElement();
182181
attributeMap.put(attrName, pc.getAttribute(attrName));
183182
}
184183
}
@@ -211,15 +210,15 @@ static void registerEnvironmentBeans(
211210
*/
212211
public static void initPortletPropertySources(MutablePropertySources propertySources, ServletContext servletContext,
213212
PortletContext portletContext, PortletConfig portletConfig) {
214-
Assert.notNull(propertySources, "propertySources must not be null");
215213

214+
Assert.notNull(propertySources, "propertySources must not be null");
216215
WebApplicationContextUtils.initServletPropertySources(propertySources, servletContext);
217216

218-
if(portletContext != null && propertySources.contains(StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME)) {
217+
if (portletContext != null && propertySources.contains(StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME)) {
219218
propertySources.replace(StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME,
220219
new PortletContextPropertySource(StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME, portletContext));
221220
}
222-
if(portletConfig != null && propertySources.contains(StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME)) {
221+
if (portletConfig != null && propertySources.contains(StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME)) {
223222
propertySources.replace(StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME,
224223
new PortletConfigPropertySource(StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME, portletConfig));
225224
}

0 commit comments

Comments
 (0)