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 658142a1a..e55fd8901 100644 --- a/src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java +++ b/src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java @@ -133,7 +133,10 @@ public class ClassGeneratingPropertyAccessorFactory implements PersistentPropert } private static boolean isTypeInjectable(PersistentEntity entity) { - return entity.getType().getClassLoader() != null && !entity.getType().getPackage().getName().startsWith("java"); + + Class type = entity.getType(); + return type.getClassLoader() != null + && (type.getPackage() == null || !type.getPackage().getName().startsWith("java")); } private boolean hasUniquePropertyHashCodes(PersistentEntity entity) { 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 8945cbeda..f21025560 100755 --- a/src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryTests.java +++ b/src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryTests.java @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.mapping.model; import static org.assertj.core.api.Assertions.*; +import static org.junit.Assume.*; import java.lang.reflect.Constructor; import java.util.ArrayList; @@ -36,6 +36,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} @@ -61,7 +62,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", @@ -79,6 +81,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; } @@ -94,9 +101,16 @@ public class ClassGeneratingPropertyAccessorFactoryTests { return parameters; } + @Test // DATACMNS-1201 + public void shouldSupportGeneratedPropertyAccessors() { + assertThat(factory.isSupported(mappingContext.getRequiredPersistentEntity(bean.getClass()))).isTrue(); + } + @Test // DATACMNS-809 public void shouldSetAndGetProperty() throws Exception { + assumeTrue(StringUtils.hasText(propertyName)); + assertThat(getProperty(bean, propertyName)).satisfies(property -> { PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);