From 8220832c4e8424b42a0e6d86529cffd3f42ea636 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 27 May 2014 18:29:51 +0200 Subject: [PATCH] Polishing --- .../format/annotation/NumberFormat.java | 27 +++++++------ .../GsonBase64ByteArrayJsonTypeAdapter.java | 20 +++++----- .../json/GsonHttpMessageConverter.java | 40 ++++++++----------- 3 files changed, 40 insertions(+), 47 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/format/annotation/NumberFormat.java b/spring-context/src/main/java/org/springframework/format/annotation/NumberFormat.java index e4f1076ea7..0eea0622ac 100644 --- a/spring-context/src/main/java/org/springframework/format/annotation/NumberFormat.java +++ b/spring-context/src/main/java/org/springframework/format/annotation/NumberFormat.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -25,13 +25,14 @@ import java.lang.annotation.Target; * Declares that a field should be formatted as a number. * Supports formatting by style or custom pattern string. * Can be applied to any JDK {@code java.lang.Number} type. - *

- * For style-based formatting, set the {@link #style()} attribute to be the desired {@link Style}. + * + *

For style-based formatting, set the {@link #style()} attribute to be the desired {@link Style}. * For custom formatting, set the {@link #pattern()} attribute to be the number pattern, such as {@code #, ###.##}. - *

- * Each attribute is mutually exclusive, so only set one attribute per annotation instance (the one most convenient one for your formatting needs). - * When the pattern attribute is specified, it takes precedence over the style attribute. - * When no annotation attributes are specified, the default format applied is style-based with a style of {@link Style#NUMBER}. + * + *

Each attribute is mutually exclusive, so only set one attribute per annotation instance + * (the one most convenient one for your formatting needs). When the pattern attribute is specified, + * it takes precedence over the style attribute. When no annotation attributes are specified, + * the default format applied is style-based with a style of {@link Style#NUMBER}. * * @author Keith Donald * @since 3.0 @@ -43,23 +44,23 @@ public @interface NumberFormat { /** * The style pattern to use to format the field. - * Defaults to {@link Style#NUMBER} for general-purpose number formatter. - * Set this attribute when you wish to format your field in accordance with a common style other than the default style. + *

Defaults to {@link Style#NUMBER} for general-purpose number formatter. + * Set this attribute when you wish to format your field in accordance with a + * common style other than the default style. */ Style style() default Style.NUMBER; /** * The custom pattern to use to format the field. - * Defaults to empty String, indicating no custom pattern String has been specified. - * Set this attribute when you wish to format your field in accordance with a custom number pattern not represented by a style. + *

Defaults to empty String, indicating no custom pattern String has been specified. + * Set this attribute when you wish to format your field in accordance with a + * custom number pattern not represented by a style. */ String pattern() default ""; /** * Common number format styles. - * @author Keith Donald - * @since 3.0 */ public enum Style { diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/GsonBase64ByteArrayJsonTypeAdapter.java b/spring-web/src/main/java/org/springframework/http/converter/json/GsonBase64ByteArrayJsonTypeAdapter.java index 54212f480a..d3c342797b 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/GsonBase64ByteArrayJsonTypeAdapter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/GsonBase64ByteArrayJsonTypeAdapter.java @@ -19,32 +19,30 @@ package org.springframework.http.converter.json; import java.lang.reflect.Type; import java.nio.charset.Charset; -import org.apache.commons.codec.binary.Base64; - import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.TypeAdapter; +import org.apache.commons.codec.binary.Base64; /** - * Custom Gson {@link TypeAdapter} for serialization or deserialization of - * {@code byte[]}. By default Gson converts byte arrays to JSON arrays instead - * of a Base64 encoded string. Use this type adapter with - * {@link org.springframework.http.converter.json.GsonHttpMessageConverter - * GsonHttpMessageConverter} to read and write Base64 encoded byte arrays. + * Custom Gson {@link TypeAdapter} for serialization and deserialization + * of {@code byte[]} values to/from Base64-encoded Strings. + * + *

By default, Gson converts byte arrays to JSON arrays. This type adapter + * needs to be specifically registered to read/write Base64-encoded byte arrays. * * @author Roy Clarkson * @since 4.1 * @see GsonBuilder#registerTypeHierarchyAdapter(Class, Object) */ -final class GsonBase64ByteArrayJsonTypeAdapter implements JsonSerializer, JsonDeserializer { +class GsonBase64ByteArrayJsonTypeAdapter implements JsonSerializer, JsonDeserializer { - public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); + private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private final Base64 base64 = new Base64(); @@ -56,7 +54,7 @@ final class GsonBase64ByteArrayJsonTypeAdapter implements JsonSerializer } @Override - public byte[] deserialize(JsonElement json, Type type, JsonDeserializationContext cxt) throws JsonParseException { + public byte[] deserialize(JsonElement json, Type type, JsonDeserializationContext cxt) { return this.base64.decode(json.getAsString().getBytes(DEFAULT_CHARSET)); } diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java index 27677f009e..7a68e3ddf9 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java @@ -23,6 +23,11 @@ import java.io.Reader; import java.lang.reflect.Type; import java.nio.charset.Charset; +import com.google.gson.Gson; +import com.google.gson.JsonIOException; +import com.google.gson.JsonParseException; +import com.google.gson.reflect.TypeToken; + import org.springframework.http.HttpHeaders; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; @@ -33,28 +38,21 @@ import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.util.Assert; -import com.google.gson.Gson; -import com.google.gson.JsonIOException; -import com.google.gson.JsonParseException; -import com.google.gson.reflect.TypeToken; - /** - * Implementation of {@link org.springframework.http.converter.HttpMessageConverter - * HttpMessageConverter} that can read and write JSON using the Google Gson library's {@link Gson} - * class. + * Implementation of {@link org.springframework.http.converter.HttpMessageConverter} + * that can read and write JSON using the + * Google Gson library's + * {@link Gson} class. * - *

This converter can be used to bind to typed beans or untyped - * {@link java.util.HashMap HashMap} instances. - * - *

By default this converter supports {@code application/json} and - * {@code application/*+json} but {@link #setSupportedMediaTypes - * supportedMediaTypes} can be used to change that. + *

This converter can be used to bind to typed beans or untyped {@code HashMap}s. + * By default, it supports {@code application/json} and {@code application/*+json}. * *

Tested against Gson 2.2; compatible with Gson 2.0 and higher. * * @author Roy Clarkson * @since 4.1 + * @see #setGson + * @see #setSupportedMediaTypes */ public class GsonHttpMessageConverter extends AbstractHttpMessageConverter implements GenericHttpMessageConverter { @@ -75,10 +73,10 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverterSetting a custom-configured {@code Gson} is one way to take further * control of the JSON serialization process. */ @@ -96,7 +94,6 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverterPrefixing the JSON string in this manner is used to help prevent JSON * Hijacking. The prefix renders the string syntactically invalid as a script * so that it cannot be hijacked. This prefix does not affect the evaluation * of JSON, but if JSON validation is performed on the string, the prefix * would need to be ignored. - * * @see #setJsonPrefix */ public void setPrefixJson(boolean prefixJson) { @@ -159,16 +154,15 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverterThe default implementation returns {@code TypeToken.get(type)}, but * this can be overridden in subclasses to allow for custom generic * collection handling. For instance: *
 	 * protected TypeToken getTypeToken(Type type) {
 	 *   if (type instanceof Class && List.class.isAssignableFrom((Class) type)) {
-	 *     return new TypeToken>() {
-	 *     };
-	 *   } else {
+	 *     return new TypeToken>() {};
+	 *   }
+	 *   else {
 	 *     return super.getTypeToken(type);
 	 *   }
 	 * }