From 9e4d4901370de034f1df25d65a07c94a1c97c0d8 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Wed, 30 Oct 2019 15:04:12 +0100 Subject: [PATCH] DATACMNS-1602 - Avoid attempts to recreate the same property accessor class. PropertyAccessorClassGenerator now tries to look up the class to be generated first to potentially reuse an already existing one and avoid the recreation and registration of a same class which would trigger a Linkage error as a classloader cannot hold two classes with the same name. The root of the problem is in the fact that the accessor instances are held in per instance caches in EntityInstantiators, so that multiple of those might try to create the same accessor class for a given domain type. --- ...lassGeneratingPropertyAccessorFactory.java | 21 +++++++-- ...opertyAccessorClassGeneratorUnitTests.java | 46 +++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/springframework/data/mapping/model/PropertyAccessorClassGeneratorUnitTests.java 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 {} +}