From cc0ab54c38e90ddfbcf4d2aa23993270cb8e56b9 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 25 Sep 2017 08:58:00 +0200 Subject: [PATCH] DATACMNS-1171 - Do not create PersistentEntity for unsupported Kotlin classes. We now reject unsupported Kotlin classes from the MappingContext by default by not creating persistent entities. Original pull request: #245. --- .../context/AbstractMappingContext.java | 15 ++++++++--- .../AbstractMappingContextUnitTests.java | 11 ++++++++ .../data/mapping/context/SimpleDataClass.kt | 22 ++++++++++++++++ .../context/TypeCreatingSyntheticClass.kt | 25 +++++++++++++++++++ 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/test/kotlin/org/springframework/data/mapping/context/SimpleDataClass.kt create mode 100644 src/test/kotlin/org/springframework/data/mapping/context/TypeCreatingSyntheticClass.kt diff --git a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index c1c83b9ac..8092e1130 100644 --- a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -471,15 +471,22 @@ public abstract class AbstractMappingContext * * @param type will never be {@literal null}. * @return */ protected boolean shouldCreatePersistentEntityFor(TypeInformation type) { - return !simpleTypeHolder.isSimpleType(type.getType()); + + if (simpleTypeHolder.isSimpleType(type.getType())) { + return false; + } + + return !org.springframework.data.util.ReflectionUtils.isKotlinClass(type.getType()) + || org.springframework.data.util.ReflectionUtils.isSupportedKotlinClass(type.getType()); } /** diff --git a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java index b89c762ac..1b27db175 100755 --- a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java @@ -45,6 +45,7 @@ import org.springframework.util.StringUtils; * * @author Oliver Gierke * @author Thomas Darimont + * @author Mark Paluch */ public class AbstractMappingContextUnitTests { @@ -210,6 +211,16 @@ public class AbstractMappingContextUnitTests { assertHasEntityFor(TreeMap.class, context, false); } + @Test // DATACMNS-1171 + public void shouldCreateEntityForKotlinDataClass() { + assertThat(context.getPersistentEntity(SimpleDataClass.class)).isNotNull(); + } + + @Test // DATACMNS-1171 + public void shouldNotCreateEntityForSyntheticKotlinClass() { + assertThat(context.getPersistentEntity(TypeCreatingSyntheticClass.class)).isNotNull(); + } + @Test // DATACMNS-695 public void persistentPropertyPathTraversesGenericTypesCorrectly() { assertThat(context.getPersistentPropertyPath("field.wrapped.field", Outer.class)).hasSize(3); diff --git a/src/test/kotlin/org/springframework/data/mapping/context/SimpleDataClass.kt b/src/test/kotlin/org/springframework/data/mapping/context/SimpleDataClass.kt new file mode 100644 index 000000000..b8e7ece3d --- /dev/null +++ b/src/test/kotlin/org/springframework/data/mapping/context/SimpleDataClass.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mapping.context + +/** + * @author Mark Paluch + */ +data class SimpleDataClass(val firstname: String) { +} \ No newline at end of file diff --git a/src/test/kotlin/org/springframework/data/mapping/context/TypeCreatingSyntheticClass.kt b/src/test/kotlin/org/springframework/data/mapping/context/TypeCreatingSyntheticClass.kt new file mode 100644 index 000000000..23e219349 --- /dev/null +++ b/src/test/kotlin/org/springframework/data/mapping/context/TypeCreatingSyntheticClass.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mapping.context + +/** + * @author Mark Paluch + */ +class TypeCreatingSyntheticClass { +} + +fun foobar(args: Array) { +} \ No newline at end of file