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.
This commit is contained in:
Oliver Drotbohm
2019-10-30 15:04:12 +01:00
parent 5542474ed2
commit 9e4d490137
2 changed files with 63 additions and 4 deletions

View File

@@ -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<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Sample.class);
PropertyAccessorClassGenerator.generateCustomAccessorClass(entity);
assertThatCode(() -> PropertyAccessorClassGenerator.generateCustomAccessorClass(entity)) //
.doesNotThrowAnyException();
}
static class Sample {}
}