diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/util/ReflectionHelper.java b/core-common/src/main/java/org/glassfish/jersey/internal/util/ReflectionHelper.java index 9aeb5d19ef..7e6041455d 100644 --- a/core-common/src/main/java/org/glassfish/jersey/internal/util/ReflectionHelper.java +++ b/core-common/src/main/java/org/glassfish/jersey/internal/util/ReflectionHelper.java @@ -963,9 +963,13 @@ public static String getPropertyName(final Method method) { final int offset = methodName.startsWith("is") ? 2 : 3; final char[] chars = methodName.toCharArray(); - chars[offset] = Character.toLowerCase(chars[offset]); - return new String(chars, offset, chars.length - offset); + if (offset < chars.length) { + chars[offset] = Character.toLowerCase(chars[offset]); + return new String(chars, offset, chars.length - offset); + } else { + return ""; + } } /** diff --git a/core-common/src/test/java/org/glassfish/jersey/internal/util/ReflectionHelperTest.java b/core-common/src/test/java/org/glassfish/jersey/internal/util/ReflectionHelperTest.java index d0fcec0d9e..ab03e7d978 100644 --- a/core-common/src/test/java/org/glassfish/jersey/internal/util/ReflectionHelperTest.java +++ b/core-common/src/test/java/org/glassfish/jersey/internal/util/ReflectionHelperTest.java @@ -108,7 +108,7 @@ public void securityManagerSetContextClassLoaderInDoPrivileged() throws Exceptio AccessController.doPrivileged(ReflectionHelper.setContextClassLoaderPA(loader)); fail("It should not be possible to set context class loader even from privileged block via Jersey ReflectionHelper " - + "utility"); + + "utility"); } public static class FromStringClass { @@ -186,4 +186,28 @@ public void testGetFromStringStringMethodNegative() throws Exception { assertThat("Invalid valueOf method found.", methodPA.run(), nullValue()); } + + @Test + public void testForPropertyNameNormalGetter() throws Exception { + class TestGet { + public int getProp() { + return 1; + } + } + Method method = TestGet.class.getMethod("getProp"); + final String propertyName = ReflectionHelper.getPropertyName(method); + assertThat("getPropertyName should extract the correct property", propertyName, is("prop")); + } + + @Test + public void testForPropertyNameOfGetMethod() throws Exception { + class TestGet { + public int get() { + return 0; + } + } + Method method = TestGet.class.getMethod("get"); + String propertyName = ReflectionHelper.getPropertyName(method); + assertThat("get method should not have a property name", propertyName, is("")); + } }