Skip to content

Commit 9e4d490

Browse files
committed
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.
1 parent 5542474 commit 9e4d490

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,26 @@ static class PropertyAccessorClassGenerator {
317317
static Class<?> generateCustomAccessorClass(PersistentEntity<?, ?> entity) {
318318

319319
String className = generateClassName(entity);
320-
byte[] bytecode = generateBytecode(className.replace('.', '/'), entity);
321320
Class<?> type = entity.getType();
321+
ClassLoader classLoader = type.getClassLoader();
322+
323+
if (ClassUtils.isPresent(className, classLoader)) {
324+
325+
try {
326+
return ClassUtils.forName(className, classLoader);
327+
} catch (Exception o_O) {
328+
throw new IllegalStateException(o_O);
329+
}
330+
}
331+
332+
byte[] bytecode = generateBytecode(className.replace('.', '/'), entity);
322333

323334
try {
324-
return ReflectUtils.defineClass(className, bytecode, type.getClassLoader(), type.getProtectionDomain(), type);
325-
} catch (Exception e) {
326-
throw new IllegalStateException(e);
335+
336+
return ReflectUtils.defineClass(className, bytecode, classLoader, type.getProtectionDomain(), type);
337+
338+
} catch (Exception o_O) {
339+
throw new IllegalStateException(o_O);
327340
}
328341
}
329342

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mapping.model;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.Test;
21+
import org.springframework.data.mapping.PersistentEntity;
22+
import org.springframework.data.mapping.context.SampleMappingContext;
23+
import org.springframework.data.mapping.context.SamplePersistentProperty;
24+
import org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.PropertyAccessorClassGenerator;
25+
26+
/**
27+
* Unit tests for {@link PropertyAccessorClassGeneratorUnitTests}.
28+
*
29+
* @author Oliver Drotbohm
30+
*/
31+
public class PropertyAccessorClassGeneratorUnitTests {
32+
33+
@Test // DATACMNS-1602
34+
public void reusesAlreadyDeclaredClass() {
35+
36+
SampleMappingContext context = new SampleMappingContext();
37+
PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Sample.class);
38+
39+
PropertyAccessorClassGenerator.generateCustomAccessorClass(entity);
40+
41+
assertThatCode(() -> PropertyAccessorClassGenerator.generateCustomAccessorClass(entity)) //
42+
.doesNotThrowAnyException();
43+
}
44+
45+
static class Sample {}
46+
}

0 commit comments

Comments
 (0)