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 5396f59ae..29064788b 100644 --- a/src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java +++ b/src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java @@ -317,13 +317,26 @@ public class ClassGeneratingPropertyAccessorFactory implements PersistentPropert static Class generateCustomAccessorClass(PersistentEntity entity) { String className = generateClassName(entity); - byte[] bytecode = generateBytecode(className.replace('.', '/'), entity); Class type = entity.getType(); + ClassLoader classLoader = type.getClassLoader(); + + if (ClassUtils.isPresent(className, classLoader)) { + + try { + return ClassUtils.forName(className, classLoader); + } catch (Exception o_O) { + throw new IllegalStateException(o_O); + } + } + + byte[] bytecode = generateBytecode(className.replace('.', '/'), entity); try { - return ReflectUtils.defineClass(className, bytecode, type.getClassLoader(), type.getProtectionDomain(), type); - } catch (Exception e) { - throw new IllegalStateException(e); + + return ReflectUtils.defineClass(className, bytecode, classLoader, type.getProtectionDomain(), type); + + } catch (Exception o_O) { + throw new IllegalStateException(o_O); } } diff --git a/src/test/java/org/springframework/data/mapping/model/PropertyAccessorClassGeneratorUnitTests.java b/src/test/java/org/springframework/data/mapping/model/PropertyAccessorClassGeneratorUnitTests.java new file mode 100644 index 000000000..1ccad740b --- /dev/null +++ b/src/test/java/org/springframework/data/mapping/model/PropertyAccessorClassGeneratorUnitTests.java @@ -0,0 +1,46 @@ +/* + * Copyright 2019 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 + * + * https://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.model; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.Test; +import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.mapping.context.SampleMappingContext; +import org.springframework.data.mapping.context.SamplePersistentProperty; +import org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.PropertyAccessorClassGenerator; + +/** + * Unit tests for {@link PropertyAccessorClassGeneratorUnitTests}. + * + * @author Oliver Drotbohm + */ +public class PropertyAccessorClassGeneratorUnitTests { + + @Test // DATACMNS-1602 + public void reusesAlreadyDeclaredClass() { + + SampleMappingContext context = new SampleMappingContext(); + PersistentEntity entity = context.getRequiredPersistentEntity(Sample.class); + + PropertyAccessorClassGenerator.generateCustomAccessorClass(entity); + + assertThatCode(() -> PropertyAccessorClassGenerator.generateCustomAccessorClass(entity)) // + .doesNotThrowAnyException(); + } + + static class Sample {} +}