From ca6b3f0bd5148873122f555493ddb38b9b6dd179 Mon Sep 17 00:00:00 2001 From: wuwen Date: Tue, 17 May 2016 18:27:21 +0800 Subject: [PATCH] fix StringIndexOutOfBoundsException, Should not throw exception on invalid property --- .../beans/AbstractNestablePropertyAccessor.java | 4 +++- .../springframework/beans/BeanWrapperTests.java | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java b/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java index df8931c46112..0d5764f9422b 100644 --- a/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java @@ -943,7 +943,9 @@ private PropertyTokenHolder getPropertyNameTokens(String propertyName) { } String key = propertyName.substring(keyStart + PROPERTY_KEY_PREFIX.length(), keyEnd); if ((key.startsWith("'") && key.endsWith("'")) || (key.startsWith("\"") && key.endsWith("\""))) { - key = key.substring(1, key.length() - 1); + if (key.length() > 1) { + key = key.substring(1, key.length() - 1); + } } keys.add(key); searchIndex = keyEnd + PROPERTY_KEY_SUFFIX.length(); diff --git a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java index 9d79f7f7ac7e..5038a2ba145e 100644 --- a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java @@ -202,6 +202,22 @@ public void getPropertyWithOptionalAndAutoGrow() { assertEquals("x", accessor.getPropertyValue("object.name")); } + @Test + public void checkNotWritablePropertyIsInvalidCharacter() { + TestBean target = new TestBean(); + try { + BeanWrapper accessor = createAccessor(target); + accessor.setPropertyValue("[']", "foobar"); + fail("Should throw exception on invalid property"); + } + catch (NotWritablePropertyException ex) { + assertNull(ex.getPossibleMatches()); + } + catch (RuntimeException re) { + fail("Should not throw runtime exception on invalid property"); + } + } + @SuppressWarnings("unused") private interface AliasedProperty {