提交 1e9ab530 编写于 作者: J Juergen Hoeller

CharacterEditor uses Character.valueOf(char) and throws a more descriptive exception

Issue: SPR-12386
上级 33d85d2a
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -57,11 +57,10 @@ public class CharacterEditor extends PropertyEditorSupport { ...@@ -57,11 +57,10 @@ public class CharacterEditor extends PropertyEditorSupport {
/** /**
* Create a new CharacterEditor instance. * Create a new CharacterEditor instance.
* <p>The "allowEmpty" parameter controls whether an empty String is * <p>The "allowEmpty" parameter controls whether an empty String is to be
* to be allowed in parsing, i.e. be interpreted as the {@code null} * allowed in parsing, i.e. be interpreted as the {@code null} value when
* value when {@link #setAsText(String) text is being converted}. If * {@link #setAsText(String) text is being converted}. If {@code false},
* {@code false}, an {@link IllegalArgumentException} will be thrown * an {@link IllegalArgumentException} will be thrown at that time.
* at that time.
* @param allowEmpty if empty strings are to be allowed * @param allowEmpty if empty strings are to be allowed
*/ */
public CharacterEditor(boolean allowEmpty) { public CharacterEditor(boolean allowEmpty) {
...@@ -81,12 +80,12 @@ public class CharacterEditor extends PropertyEditorSupport { ...@@ -81,12 +80,12 @@ public class CharacterEditor extends PropertyEditorSupport {
else if (isUnicodeCharacterSequence(text)) { else if (isUnicodeCharacterSequence(text)) {
setAsUnicode(text); setAsUnicode(text);
} }
else if (text.length() != 1) { else if (text.length() == 1) {
throw new IllegalArgumentException("String [" + text + "] with length " + setValue(Character.valueOf(text.charAt(0)));
text.length() + " cannot be converted to char type");
} }
else { else {
setValue(new Character(text.charAt(0))); throw new IllegalArgumentException("String [" + text + "] with length " +
text.length() + " cannot be converted to char type: neither Unicode nor single character");
} }
} }
...@@ -103,7 +102,7 @@ public class CharacterEditor extends PropertyEditorSupport { ...@@ -103,7 +102,7 @@ public class CharacterEditor extends PropertyEditorSupport {
private void setAsUnicode(String text) { private void setAsUnicode(String text) {
int code = Integer.parseInt(text.substring(UNICODE_PREFIX.length()), 16); int code = Integer.parseInt(text.substring(UNICODE_PREFIX.length()), 16);
setValue(new Character((char) code)); setValue(Character.valueOf((char) code));
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册