Skip to content

Commit 8b4d601

Browse files
committed
Add TypeCollectorFilters to filter $$_hibernate fields and methods.
Closes #4014
1 parent 811266f commit 8b4d601

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2025 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.jpa.repository.aot;
17+
18+
import java.lang.reflect.Field;
19+
import java.lang.reflect.Member;
20+
import java.lang.reflect.Method;
21+
import java.util.function.Predicate;
22+
23+
import org.springframework.data.util.Predicates;
24+
import org.springframework.data.util.TypeCollector;
25+
import org.springframework.data.util.TypeUtils;
26+
27+
/**
28+
* {@link TypeCollector} predicates to exclude JPA provider types.
29+
*
30+
* @author Mark Paluch
31+
* @since 4.0
32+
*/
33+
class JpaTypeFilters implements TypeCollector.TypeCollectorFilters {
34+
35+
/**
36+
* Match for bytecode-enhanced members.
37+
*/
38+
private static final Predicate<Member> IS_HIBERNATE_MEMBER = member -> member.getName().startsWith("$$_hibernate");
39+
40+
private static final Predicate<Class<?>> CLASS_FILTER = it -> TypeUtils.type(it).isPartOf("org.hibernate",
41+
"org.eclipse.persistence", "jakarta.persistence");
42+
43+
@Override
44+
public Predicate<Class<?>> classPredicate() {
45+
return CLASS_FILTER.negate();
46+
}
47+
48+
@Override
49+
public Predicate<Field> fieldPredicate() {
50+
return Predicates.<Field> declaringClass(CLASS_FILTER).or(IS_HIBERNATE_MEMBER).negate();
51+
}
52+
53+
@Override
54+
public Predicate<Method> methodPredicate() {
55+
return Predicates.<Method> declaringClass(CLASS_FILTER).or(IS_HIBERNATE_MEMBER).negate();
56+
}
57+
58+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
org.springframework.aot.hint.RuntimeHintsRegistrar=\
22
org.springframework.data.jpa.repository.aot.JpaRuntimeHints
3+
4+
org.springframework.data.util.TypeCollector$TypeCollectorFilters=\
5+
org.springframework.data.jpa.repository.aot.JpaTypeFilters
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2025 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.jpa.repository.aot;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import jakarta.persistence.LockOption;
21+
22+
import org.eclipse.persistence.sessions.DatabaseSession;
23+
import org.hibernate.Session;
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.data.util.TypeCollector;
27+
28+
/**
29+
* Unit tests for {@link JpaTypeFilters}.
30+
*
31+
* @author Mark Paluch
32+
*/
33+
class JpaTypeFiltersUnitTests {
34+
35+
@Test // GH-4014
36+
void shouldFilterUnreachableField() {
37+
assertThat(TypeCollector.inspect(EnhancedEntity.class).list()).containsOnly(EnhancedEntity.class, Reachable.class);
38+
}
39+
40+
static class Unreachable {
41+
42+
}
43+
44+
static class Reachable {
45+
46+
}
47+
48+
static class EnhancedEntity {
49+
50+
private Unreachable $$_hibernate_field;
51+
private Reachable reachable;
52+
private Session session;
53+
private DatabaseSession databaseSession;
54+
private LockOption lockOption;
55+
56+
public EnhancedEntity(Session session, LockOption lockOption) {
57+
this.session = session;
58+
this.lockOption = lockOption;
59+
}
60+
61+
public void setSession(Session session) {
62+
63+
}
64+
}
65+
66+
}

0 commit comments

Comments
 (0)