From f532404521bf9faba89a095ba8e83320526f4142 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 31 Jul 2012 15:41:56 +0200 Subject: [PATCH] DATACMNS-206 - Guard against PropertyDescriptor being null. Added guards to PropertyDescriptor access to prevent NullPointerExceptions in cases it is not given in the first place. Polished JavaDoc for test case. --- .../model/AbstractPersistentProperty.java | 8 ++++++ .../AbstractPersistentPropertyUnitTests.java | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index 18ad2687d..dccb18f3e 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -148,6 +148,10 @@ public abstract class AbstractPersistentProperty

*/ public Method getGetter() { + if (propertyDescriptor == null) { + return null; + } + Method getter = propertyDescriptor.getReadMethod(); if (getter == null) { @@ -163,6 +167,10 @@ public abstract class AbstractPersistentProperty

*/ public Method getSetter() { + if (propertyDescriptor == null) { + return null; + } + Method setter = propertyDescriptor.getWriteMethod(); if (setter == null) { diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java index c21c5e04e..317f789ae 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -125,6 +125,9 @@ public class AbstractPersistentPropertyUnitTests { assertThat(property.isTransient(), is(false)); } + /** + * @see DATACMNS-206 + */ @Test public void findsSimpleGettersAndASetters() { @@ -136,6 +139,9 @@ public class AbstractPersistentPropertyUnitTests { assertThat(property.getSetter(), is(notNullValue())); } + /** + * @see DATACMNS-206 + */ @Test public void doesNotUseInvalidGettersAndASetters() { @@ -147,6 +153,9 @@ public class AbstractPersistentPropertyUnitTests { assertThat(property.getSetter(), is(nullValue())); } + /** + * @see DATACMNS-206 + */ @Test public void usesCustomGetter() { @@ -158,6 +167,9 @@ public class AbstractPersistentPropertyUnitTests { assertThat(property.getSetter(), is(nullValue())); } + /** + * @see DATACMNS-206 + */ @Test public void usesCustomSetter() { @@ -169,6 +181,20 @@ public class AbstractPersistentPropertyUnitTests { assertThat(property.getSetter(), is(notNullValue())); } + /** + * @see DATACMNS-206 + */ + @Test + public void returnsNullGetterAndSetterIfNoPropertyDescriptorGiven() { + + Field field = ReflectionUtils.findField(AccessorTestClass.class, "id"); + PersistentProperty property = new SamplePersistentProperty(field, null, entity, + typeHolder); + + assertThat(property.getGetter(), is(nullValue())); + assertThat(property.getSetter(), is(nullValue())); + } + private static PropertyDescriptor getPropertyDescriptor(Class type, String propertyName) { try {