From caa1218d368f156a0d2592e0c78affe195ef6594 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 29 Aug 2013 19:17:59 +0200 Subject: [PATCH] Polishing --- .../format/datetime/joda/JodaTimeContext.java | 10 +-- .../datetime/standard/DateTimeContext.java | 10 +-- .../GenericConversionServiceTests.java | 79 ++++++++++--------- .../web/servlet/support/JstlUtils.java | 4 +- .../web/servlet/support/RequestContext.java | 25 +++--- 5 files changed, 67 insertions(+), 61 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeContext.java b/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeContext.java index e2db64c6bff..e4db4a2557a 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeContext.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeContext.java @@ -38,31 +38,31 @@ public class JodaTimeContext { /** - * Set the user's chronology. + * Set the user's chronology (calendar system). */ public void setChronology(Chronology chronology) { this.chronology = chronology; } /** - * The user's chronology (calendar system), if any. + * Return the user's chronology (calendar system), if any. */ public Chronology getChronology() { return this.chronology; } /** - * Set the user's timezone. + * Set the user's time zone. */ public void setTimeZone(DateTimeZone timeZone) { this.timeZone = timeZone; } /** - * The user's timezone, if any. + * Return the user's time zone, if any. */ public DateTimeZone getTimeZone() { - return timeZone; + return this.timeZone; } diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeContext.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeContext.java index f5b64dcbc15..ec8db53ecc5 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeContext.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeContext.java @@ -37,31 +37,31 @@ public class DateTimeContext { /** - * Set the user's chronology. + * Set the user's chronology (calendar system). */ public void setChronology(Chronology chronology) { this.chronology = chronology; } /** - * The user's chronology (calendar system), if any. + * Return the user's chronology (calendar system), if any. */ public Chronology getChronology() { return this.chronology; } /** - * Set the user's timezone. + * Set the user's time zone. */ public void setTimeZone(ZoneId timeZone) { this.timeZone = timeZone; } /** - * The user's timezone, if any. + * Return the user's time zone, if any. */ public ZoneId getTimeZone() { - return timeZone; + return this.timeZone; } diff --git a/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index c08cdeebb77..07aa882307a 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.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. @@ -49,7 +49,7 @@ import org.springframework.tests.TestGroup; import org.springframework.util.StopWatch; import org.springframework.util.StringUtils; -import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; /** @@ -81,14 +81,14 @@ public class GenericConversionServiceTests { try { assertFalse(conversionService.canConvert(String.class, null)); fail("Should have failed"); - } catch (IllegalArgumentException e) { - + } + catch (IllegalArgumentException ex) { } try { assertFalse(conversionService.canConvert(TypeDescriptor.valueOf(String.class), null)); fail("Should have failed"); - } catch (IllegalArgumentException e) { - + } + catch (IllegalArgumentException ex) { } } @@ -151,8 +151,8 @@ public class GenericConversionServiceTests { } }); fail("Should have failed"); - } catch (IllegalArgumentException e) { - + } + catch (IllegalArgumentException ex) { } } @@ -254,7 +254,8 @@ public class GenericConversionServiceTests { try { conversionService.convert("3,4,5", Integer[].class); fail("should have failed"); - } catch (ConverterNotFoundException e) { + } + catch (ConverterNotFoundException ex) { } } @@ -389,7 +390,7 @@ public class GenericConversionServiceTests { } @Test - public void testConvertUUID() throws Exception { + public void testConvertUUID() { GenericConversionService service = new DefaultConversionService(); UUID uuid = UUID.randomUUID(); String convertToString = service.convert(uuid, String.class); @@ -472,7 +473,7 @@ public class GenericConversionServiceTests { public static Map map; @Test - public void emptyListToArray() throws Exception { + public void emptyListToArray() { conversionService.addConverter(new CollectionToArrayConverter(conversionService)); conversionService.addConverterFactory(new StringToNumberConverterFactory()); List list = new ArrayList(); @@ -483,7 +484,7 @@ public class GenericConversionServiceTests { } @Test - public void emptyListToObject() throws Exception { + public void emptyListToObject() { conversionService.addConverter(new CollectionToObjectConverter(conversionService)); conversionService.addConverterFactory(new StringToNumberConverterFactory()); List list = new ArrayList(); @@ -590,14 +591,14 @@ public class GenericConversionServiceTests { public Collection stringToCollection; @Test - public void testConvertiblePairsInSet() throws Exception { + public void testConvertiblePairsInSet() { Set set = new HashSet(); set.add(new GenericConverter.ConvertiblePair(Number.class, String.class)); assert set.contains(new GenericConverter.ConvertiblePair(Number.class, String.class)); } @Test - public void testConvertiblePairEqualsAndHash() throws Exception { + public void testConvertiblePairEqualsAndHash() { GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class); GenericConverter.ConvertiblePair pairEqual = new GenericConverter.ConvertiblePair(Number.class, String.class); assertEquals(pair, pairEqual); @@ -605,7 +606,7 @@ public class GenericConversionServiceTests { } @Test - public void testConvertiblePairDifferentEqualsAndHash() throws Exception { + public void testConvertiblePairDifferentEqualsAndHash() { GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class); GenericConverter.ConvertiblePair pairOpposite = new GenericConverter.ConvertiblePair(String.class, Number.class); assertFalse(pair.equals(pairOpposite)); @@ -613,7 +614,7 @@ public class GenericConversionServiceTests { } @Test - public void convertPrimitiveArray() throws Exception { + public void convertPrimitiveArray() { GenericConversionService conversionService = new DefaultConversionService(); byte[] byteArray = new byte[] { 1, 2, 3 }; Byte[] converted = conversionService.convert(byteArray, Byte[].class); @@ -625,7 +626,8 @@ public class GenericConversionServiceTests { try { conversionService.canConvert(String.class, null); fail("Did not thow IllegalArgumentException"); - } catch(IllegalArgumentException e) { + } + catch (IllegalArgumentException ex) { } } @@ -634,13 +636,14 @@ public class GenericConversionServiceTests { try { conversionService.canConvert(TypeDescriptor.valueOf(String.class), null); fail("Did not thow IllegalArgumentException"); - } catch(IllegalArgumentException e) { + } + catch(IllegalArgumentException ex) { } } @Test @SuppressWarnings({ "rawtypes" }) - public void convertHashMapValuesToList() throws Exception { + public void convertHashMapValuesToList() { GenericConversionService conversionService = new DefaultConversionService(); Map hashMap = new LinkedHashMap(); hashMap.put("1", 1); @@ -650,7 +653,7 @@ public class GenericConversionServiceTests { } @Test - public void removeConvertible() throws Exception { + public void removeConvertible() { conversionService.addConverter(new ColorConverter()); assertTrue(conversionService.canConvert(String.class, Color.class)); conversionService.removeConvertible(String.class, Color.class); @@ -658,7 +661,7 @@ public class GenericConversionServiceTests { } @Test - public void conditionalConverter() throws Exception { + public void conditionalConverter() { GenericConversionService conversionService = new GenericConversionService(); MyConditionalConverter converter = new MyConditionalConverter(); conversionService.addConverter(new ColorConverter()); @@ -668,7 +671,7 @@ public class GenericConversionServiceTests { } @Test - public void conditionalConverterFactory() throws Exception { + public void conditionalConverterFactory() { GenericConversionService conversionService = new GenericConversionService(); MyConditionalConverterFactory converter = new MyConditionalConverterFactory(); conversionService.addConverter(new ColorConverter()); @@ -679,32 +682,29 @@ public class GenericConversionServiceTests { } @Test - public void shouldNotSuportNullConvertibleTypesFromNonConditionalGenericConverter() - throws Exception { + public void shouldNotSuportNullConvertibleTypesFromNonConditionalGenericConverter() { GenericConversionService conversionService = new GenericConversionService(); GenericConverter converter = new GenericConverter() { - @Override public Set getConvertibleTypes() { return null; } - @Override - public Object convert(Object source, TypeDescriptor sourceType, - TypeDescriptor targetType) { + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { return null; } }; try { conversionService.addConverter(converter); fail("Did not throw"); - } catch (IllegalStateException e) { - assertEquals("Only conditional converters may return null convertible types", e.getMessage()); + } + catch (IllegalStateException ex) { + assertEquals("Only conditional converters may return null convertible types", ex.getMessage()); } } @Test - public void conditionalConversionForAllTypes() throws Exception { + public void conditionalConversionForAllTypes() { GenericConversionService conversionService = new GenericConversionService(); MyConditionalGenericConverter converter = new MyConditionalGenericConverter(); conversionService.addConverter(converter); @@ -717,7 +717,7 @@ public class GenericConversionServiceTests { } @Test - public void convertOptimizeArray() throws Exception { + public void convertOptimizeArray() { // SPR-9566 GenericConversionService conversionService = new DefaultConversionService(); byte[] byteArray = new byte[] { 1, 2, 3 }; @@ -726,7 +726,7 @@ public class GenericConversionServiceTests { } @Test - public void convertCannotOptimizeArray() throws Exception { + public void convertCannotOptimizeArray() { GenericConversionService conversionService = new GenericConversionService(); conversionService.addConverter(new Converter() { @Override @@ -766,6 +766,7 @@ public class GenericConversionServiceTests { conversionService.convert(source, sourceType, targetType); } + @ExampleAnnotation public String annotatedString; @@ -773,8 +774,7 @@ public class GenericConversionServiceTests { public static @interface ExampleAnnotation { } - private static class MyConditionalConverter implements Converter, - ConditionalConverter { + private static class MyConditionalConverter implements Converter, ConditionalConverter { private int matchAttempts = 0; @@ -850,23 +850,26 @@ public class GenericConversionServiceTests { } interface MyEnumInterface { + String getCode(); } public static enum MyEnum implements MyEnumInterface { + A { @Override public String getCode() { return "1"; } - }; + } } - private static class MyEnumInterfaceToStringConverter - implements Converter { + private static class MyEnumInterfaceToStringConverter implements Converter { + @Override public String convert(T source) { return source.getCode(); } } + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/JstlUtils.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/JstlUtils.java index f568f362137..dab87183822 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/JstlUtils.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/JstlUtils.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. @@ -141,6 +141,6 @@ public abstract class JstlUtils { } return RequestContextUtils.getLocale(this.request); } - }; + } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java index cf33995355d..f2edbfedc3e 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java @@ -20,14 +20,12 @@ import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; - import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.jsp.jstl.core.Config; -import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.MessageSource; import org.springframework.context.MessageSourceResolvable; import org.springframework.context.NoSuchMessageException; @@ -90,6 +88,7 @@ public class RequestContext { */ private static final String REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME = "requestDataValueProcessor"; + protected static final boolean jstlPresent = ClassUtils.isPresent("javax.servlet.jsp.jstl.core.Config", RequestContext.class.getClassLoader()); @@ -113,6 +112,7 @@ public class RequestContext { private Map errorsMap; + /** * Create a new RequestContext for the given request, using the request attributes for Errors retrieval.

This * only works with InternalResourceViews, as Errors instances are part of the model and not normally exposed as @@ -181,6 +181,7 @@ public class RequestContext { protected RequestContext() { } + /** * Initialize this context with the given request, using the given model attributes for Errors retrieval. *

Delegates to {@code getFallbackLocale} and {@code getFallbackTheme} for determining the fallback @@ -214,7 +215,8 @@ public class RequestContext { if (localeResolver != null) { // Try LocaleResolver (we're within a DispatcherServlet request). this.locale = localeResolver.resolveLocale(request); - } else { + } + else { // No LocaleResolver available -> try fallback. this.locale = getFallbackLocale(); } @@ -225,13 +227,10 @@ public class RequestContext { this.urlPathHelper = new UrlPathHelper(); - try { + if (this.webApplicationContext.containsBean(REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) { this.requestDataValueProcessor = this.webApplicationContext.getBean( REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class); } - catch (NoSuchBeanDefinitionException ex) { - // Ignored - } } /** @@ -269,6 +268,7 @@ public class RequestContext { return theme; } + /** * Return the underlying HttpServletRequest. Only intended for cooperating classes in this package. */ @@ -702,7 +702,8 @@ public class RequestContext { if (htmlEscape && !(errors instanceof EscapedErrors)) { errors = new EscapedErrors(errors); put = true; - } else if (!htmlEscape && errors instanceof EscapedErrors) { + } + else if (!htmlEscape && errors instanceof EscapedErrors) { errors = ((EscapedErrors) errors).getSource(); put = true; } @@ -720,7 +721,8 @@ public class RequestContext { protected Object getModelObject(String modelName) { if (this.model != null) { return this.model.get(modelName); - } else { + } + else { return this.request.getAttribute(modelName); } } @@ -746,9 +748,10 @@ public class RequestContext { return new BindStatus(this, path, htmlEscape); } + /** - * Inner class that isolates the JSTL dependency. Just called to resolve the fallback locale if the JSTL API is - * present. + * Inner class that isolates the JSTL dependency. + * Just called to resolve the fallback locale if the JSTL API is present. */ private static class JstlLocaleResolver {