Skip to content

Commit 9217198

Browse files
committed
Introduce first-class type-safe property path references.
We now support type-safe property paths and property references: Java variants: PropertyPath.from("name", Person.class) // existing String-based API PropertyPath.of(Person::getName) // type-safe property reference expression PropertyPath.from("address.country", Person.class) // existing nested path API PropertyPath.of(Person::getAddress).then(Address::getCountry) // type-safe composed path expression PropertyReference.of(Secret::getSecret) Kotlin variants: PropertyReference.of(Secret::secret) PropertyPath.of(Person::address / Address::city) allowing type-safe usage through e.g.: Sort.by(Person::getFirstName, Person::getLastName) Closes #3400
1 parent fa9449a commit 9217198

39 files changed

+4779
-88
lines changed

pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,28 @@
388388
</resources>
389389
</build>
390390
</profile>
391+
<profile>
392+
<id>jmh</id>
393+
<dependencies>
394+
<dependency>
395+
<groupId>org.openjdk.jmh</groupId>
396+
<artifactId>jmh-core</artifactId>
397+
<scope>test</scope>
398+
<version>${jmh}</version>
399+
</dependency>
400+
<dependency>
401+
<groupId>org.openjdk.jmh</groupId>
402+
<artifactId>jmh-generator-annprocess</artifactId>
403+
<scope>test</scope>
404+
</dependency>
405+
</dependencies>
406+
<repositories>
407+
<repository>
408+
<id>jitpack.io</id>
409+
<url>https://jitpack.io</url>
410+
</repository>
411+
</repositories>
412+
</profile>
391413
</profiles>
392414

393415
<repositories>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2025-present 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;
17+
18+
import java.util.concurrent.TimeUnit;
19+
20+
import org.openjdk.jmh.annotations.BenchmarkMode;
21+
import org.openjdk.jmh.annotations.Fork;
22+
import org.openjdk.jmh.annotations.Measurement;
23+
import org.openjdk.jmh.annotations.Mode;
24+
import org.openjdk.jmh.annotations.OutputTimeUnit;
25+
import org.openjdk.jmh.annotations.Warmup;
26+
27+
/**
28+
* Global benchmark settings.
29+
*
30+
* @author Mark Paluch
31+
*/
32+
@Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
33+
@Measurement(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
34+
@Fork(value = 1, warmups = 0)
35+
@BenchmarkMode(Mode.Throughput)
36+
@OutputTimeUnit(TimeUnit.SECONDS)
37+
public abstract class BenchmarkSettings {
38+
39+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2025-present 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.core;
17+
18+
import org.junit.platform.commons.annotation.Testable;
19+
import org.openjdk.jmh.annotations.Benchmark;
20+
21+
import org.springframework.data.BenchmarkSettings;
22+
23+
/**
24+
* Benchmarks for {@link SerializableLambdaReader}.
25+
*
26+
* @author Mark Paluch
27+
*/
28+
@Testable
29+
public class SerializableLambdaReaderBenchmarks extends BenchmarkSettings {
30+
31+
private static final SerializableLambdaReader reader = new SerializableLambdaReader(PropertyReference.class);
32+
33+
@Benchmark
34+
public Object benchmarkMethodReference() {
35+
36+
PropertyReference<Person, String> methodReference = Person::firstName;
37+
return reader.read(methodReference);
38+
}
39+
40+
@Benchmark
41+
public Object benchmarkLambda() {
42+
43+
PropertyReference<Person, String> methodReference = person -> person.firstName();
44+
return reader.read(methodReference);
45+
}
46+
47+
record Person(String firstName, String lastName) {
48+
49+
}
50+
51+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2025-present 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.core;
17+
18+
import org.junit.platform.commons.annotation.Testable;
19+
import org.openjdk.jmh.annotations.Benchmark;
20+
import org.springframework.data.BenchmarkSettings;
21+
22+
/**
23+
* Benchmarks for {@link TypedPropertyPath}.
24+
*
25+
* @author Mark Paluch
26+
*/
27+
@Testable
28+
public class TypedPropertyPathBenchmarks extends BenchmarkSettings {
29+
30+
@Benchmark
31+
public Object benchmarkMethodReference() {
32+
return TypedPropertyPath.path(Person::firstName);
33+
}
34+
35+
@Benchmark
36+
public Object benchmarkComposedMethodReference() {
37+
return TypedPropertyPath.path(Person::address).then(Address::city);
38+
}
39+
40+
@Benchmark
41+
public TypedPropertyPath<Person, String> benchmarkLambda() {
42+
return TypedPropertyPath.path(person -> person.firstName());
43+
}
44+
45+
@Benchmark
46+
public TypedPropertyPath<Person, String> benchmarkComposedLambda() {
47+
return TypedPropertyPath.path((Person person) -> person.address()).then(address -> address.city());
48+
}
49+
50+
@Benchmark
51+
public Object dotPath() {
52+
return TypedPropertyPath.path(Person::firstName).toDotPath();
53+
}
54+
55+
@Benchmark
56+
public Object composedDotPath() {
57+
return TypedPropertyPath.path(Person::address).then(Address::city).toDotPath();
58+
}
59+
60+
record Person(String firstName, String lastName, Address address) {
61+
62+
}
63+
64+
record Address(String city) {
65+
66+
}
67+
68+
}

src/main/antora/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
** xref:repositories/projections.adoc[]
1818
* xref:query-by-example.adoc[]
1919
* xref:value-expressions.adoc[]
20+
* xref:property-paths.adoc[]
2021
* xref:auditing.adoc[]
2122
* xref:custom-conversions.adoc[]
2223
* xref:entity-callbacks.adoc[]

0 commit comments

Comments
 (0)