Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2026-present 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.context;

import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

/**
* Cold property filtering. A fresh fork keeps Kotlin metadata uninitialized for each measurement.
*
* @author Greg Taube
*/
@State(Scope.Thread)
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 0)
@Measurement(iterations = 1)
@Fork(10)
public class ColdKotlinPropertyFilterBenchmarks {

private Field[] fields;

@Setup
public void setUp() {
fields = SimpleDataClass.class.getDeclaredFields();
}

@Benchmark
public int inspectOrdinaryProperties() {

int included = 0;
for (Field field : fields) {
if (AbstractMappingContext.PersistentPropertyFilter.INSTANCE.matches(field)) {
included++;
}
}
return included;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
* @author Mikael Klamra
* @author Christoph Strobl
* @author Kamil Krzywański
* @author Greg Taube
*/
public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?, P>, P extends PersistentProperty<P>>
implements MappingContext<E, P>, ApplicationEventPublisherAware, ApplicationContextAware, BeanFactoryAware,
Expand Down Expand Up @@ -770,7 +771,7 @@ enum PersistentPropertyFilter implements FieldFilter {
matches.add(new PropertyMatch("this\\$.*", null));
matches.add(new PropertyMatch("\\$\\$_.*", null));
matches.add(new PropertyMatch("metaClass", "groovy.lang.MetaClass"));
matches.add(new KotlinDataClassPropertyMatch(".*\\$delegate", null));
matches.add(new KotlinDataClassPropertyMatch());

UNMAPPED_PROPERTIES = matches;
}
Expand Down Expand Up @@ -880,37 +881,27 @@ public boolean matches(String name, Class<?> type) {
}

/**
* Value object extension to {@link PropertyMatch} that matches for fields only for Kotlin data classes.
* Matches delegate fields and properties declared by Kotlin data classes.
*
* @author Mark Paluch
* @since 3.3.2
*/
static class KotlinDataClassPropertyMatch extends PropertyMatch {

public KotlinDataClassPropertyMatch(@Nullable String namePattern, @Nullable String typeName) {
super(namePattern, typeName);
public KotlinDataClassPropertyMatch() {
super(".*\\$delegate", null);
}

@Override
public boolean matches(Field field) {

if (!KotlinReflectionUtils.isDataClass(field.getDeclaringClass())) {
return false;
}

return super.matches(field);
return field.getName().endsWith("$delegate") && KotlinReflectionUtils.isDataClass(field.getDeclaringClass())
&& super.matches(field);
}

@Override
public boolean matches(Property property) {

Field field = property.getField().orElse(null);

if (field == null || !KotlinReflectionUtils.isDataClass(field.getDeclaringClass())) {
return false;
}

return super.matches(property);
return property.getField().filter(this::matches).isPresent();
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2026-present 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.context;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.lang.reflect.Field;
import java.util.Optional;

import org.junit.jupiter.api.Test;

import org.springframework.data.core.TypeInformation;
import org.springframework.data.mapping.context.AbstractMappingContext.PersistentPropertyFilter.KotlinDataClassPropertyMatch;
import org.springframework.data.mapping.model.DataClassWithLazy;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.util.KotlinReflectionUtils;

/**
* Unit tests for {@link KotlinDataClassPropertyMatch}.
*
* @author Greg Taube
*/
class KotlinDataClassPropertyMatchUnitTests {

private final KotlinDataClassPropertyMatch matcher = new KotlinDataClassPropertyMatch();

@Test // GH-3567
void skipsKotlinReflectionForNonMatchingField() throws Exception {

Field field = DataClassWithLazy.class.getDeclaredField("amount");

try (var reflection = mockStatic(KotlinReflectionUtils.class)) {
assertThat(matcher.matches(field)).isFalse();
reflection.verifyNoInteractions();
}
}

@Test // GH-3567
void skipsKotlinReflectionForNonMatchingProperty() throws Exception {

Property property = property(DataClassWithLazy.class.getDeclaredField("amount"));

try (var reflection = mockStatic(KotlinReflectionUtils.class)) {
assertThat(matcher.matches(property)).isFalse();
reflection.verifyNoInteractions();
}
}

@Test // GH-3567
void matchesDelegateFieldOnKotlinDataClass() throws Exception {
assertThat(matcher.matches(DataClassWithLazy.class.getDeclaredField("foo$delegate"))).isTrue();
}

@Test // GH-3567
void matchesDelegatePropertyOnKotlinDataClass() throws Exception {
assertThat(matcher.matches(property(DataClassWithLazy.class.getDeclaredField("foo$delegate")))).isTrue();
}

@Test // GH-3567
void doesNotMatchDelegateFieldOnJavaClass() throws Exception {
assertThat(matcher.matches(JavaSample.class.getDeclaredField("foo$delegate"))).isFalse();
}

@Test // GH-3567
void doesNotMatchDelegatePropertyOnJavaClass() throws Exception {
assertThat(matcher.matches(property(JavaSample.class.getDeclaredField("foo$delegate")))).isFalse();
}

@Test // GH-3567
void doesNotMatchPropertyWithoutField() {

Property property = mock(Property.class);
when(property.getField()).thenReturn(Optional.empty());

try (var reflection = mockStatic(KotlinReflectionUtils.class)) {
assertThat(matcher.matches(property)).isFalse();
reflection.verifyNoInteractions();
}
}

@Test // GH-3567
void doesNotMatchDelegateFieldOnRegularKotlinClass() throws Exception {
assertThat(matcher.matches(RegularClassWithLazy.class.getDeclaredField("foo$delegate"))).isFalse();
}

@Test // GH-3567
void doesNotMatchDelegatePropertyOnRegularKotlinClass() throws Exception {
assertThat(matcher.matches(property(RegularClassWithLazy.class.getDeclaredField("foo$delegate")))).isFalse();
}

private static Property property(Field field) {
return Property.of(TypeInformation.of(field.getDeclaringClass()), field);
}

static class JavaSample {
Object foo$delegate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ package org.springframework.data.mapping.context

/**
* @author Mark Paluch
* @author Greg Taube
*/
data class SimpleDataClass(val firstname: String, val lastname: String)

class RegularClassWithLazy {
val foo by lazy { 123 }
}
Loading