diff --git a/src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java b/src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java index 800e7e5db..225e47e12 100644 --- a/src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java +++ b/src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java @@ -110,7 +110,7 @@ public class ClassGeneratingPropertyAccessorFactory implements PersistentPropert return false; } - if (entity.getType().getClassLoader() == null || entity.getType().getPackage().getName().startsWith("java")) { + if (!isTypeInjectable(entity)) { return false; } @@ -143,6 +143,13 @@ public class ClassGeneratingPropertyAccessorFactory implements PersistentPropert return hashCodes.size() == propertyCount.get(); } + private static boolean isTypeInjectable(PersistentEntity entity) { + + Class type = entity.getType(); + return type.getClassLoader() != null + && (type.getPackage() == null || !type.getPackage().getName().startsWith("java")); + } + /** * @param entity must not be {@literal null}. * @return diff --git a/src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryTests.java b/src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryTests.java index d6e32ac34..9191ef148 100644 --- a/src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryTests.java +++ b/src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryTests.java @@ -13,13 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.mapping.model; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.*; +import static org.junit.Assume.assumeTrue; import java.lang.reflect.Constructor; import java.util.ArrayList; @@ -38,6 +38,7 @@ import org.springframework.data.mapping.context.SampleMappingContext; import org.springframework.data.mapping.context.SamplePersistentProperty; import org.springframework.data.mapping.model.subpackage.TypeInOtherPackage; import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.util.StringUtils; /** * Unit tests for {@link ClassGeneratingPropertyAccessorFactory} @@ -63,7 +64,8 @@ public class ClassGeneratingPropertyAccessorFactoryTests { } @Parameters(name = "{3}") - public static List parameters() { + @SuppressWarnings("unchecked") + public static List parameters() throws ReflectiveOperationException { List parameters = new ArrayList(); List propertyNames = Arrays.asList("privateField", "packageDefaultField", "protectedField", "publicField", @@ -80,6 +82,11 @@ public class ClassGeneratingPropertyAccessorFactoryTests { ClassGeneratingPropertyAccessorPublicType.class)); parameters.addAll(parameters(new SubtypeOfTypeInOtherPackage(), propertyNames, SubtypeOfTypeInOtherPackage.class)); + Class defaultPackageClass = (Class) Class.forName("TypeInDefaultPackage"); + + parameters + .add(new Object[] { defaultPackageClass.newInstance(), "", defaultPackageClass, "Class in default package" }); + return parameters; } @@ -95,9 +102,16 @@ public class ClassGeneratingPropertyAccessorFactoryTests { return parameters; } + @Test // DATACMNS-1201 + public void shouldSupportGeneratedPropertyAccessors() { + assertThat(factory.isSupported(mappingContext.getPersistentEntity(bean.getClass())), is(true)); + } + @Test // DATACMNS-809 public void shouldSetAndGetProperty() throws Exception { + assumeTrue(StringUtils.hasText(propertyName)); + PersistentProperty property = getProperty(bean, propertyName); PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);