Skip to content

Commit a9b92b7

Browse files
committed
Add TypeCollectorFilters to filter Cassandra driver types.
Closes #1606
1 parent 5614e0b commit a9b92b7

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/aot/CassandraManagedTypesBeanRegistrationAotProcessor.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@
1515
*/
1616
package org.springframework.data.cassandra.aot;
1717

18+
import java.lang.reflect.Field;
19+
import java.lang.reflect.Method;
20+
import java.util.function.Predicate;
21+
1822
import org.jspecify.annotations.Nullable;
23+
1924
import org.springframework.data.aot.ManagedTypesBeanRegistrationAotProcessor;
2025
import org.springframework.data.cassandra.CassandraManagedTypes;
26+
import org.springframework.data.util.Predicates;
27+
import org.springframework.data.util.TypeCollector;
28+
import org.springframework.data.util.TypeUtils;
2129
import org.springframework.util.ClassUtils;
2230

2331
/**
@@ -35,4 +43,31 @@ public CassandraManagedTypesBeanRegistrationAotProcessor() {
3543
protected boolean matchesByType(@Nullable Class<?> beanType) {
3644
return beanType != null && ClassUtils.isAssignable(CassandraManagedTypes.class, beanType);
3745
}
46+
47+
/**
48+
* Type filters to exclude Cassandra driver types.
49+
*/
50+
static class CassandraTypeFilters implements TypeCollector.TypeCollectorFilters {
51+
52+
private static final Predicate<Class<?>> CLASS_FILTER = it -> TypeUtils.type(it).isPartOf(
53+
"org.springframework.data.cassandra.core", "org.springframework.data.cassandra.repository",
54+
"org.apache.cassandra", "com.datastax");
55+
56+
@Override
57+
public Predicate<Class<?>> classPredicate() {
58+
return CLASS_FILTER.negate();
59+
}
60+
61+
@Override
62+
public Predicate<Field> fieldPredicate() {
63+
return Predicates.<Field> declaringClass(CLASS_FILTER).negate();
64+
}
65+
66+
@Override
67+
public Predicate<Method> methodPredicate() {
68+
return Predicates.<Method> declaringClass(CLASS_FILTER).negate();
69+
}
70+
71+
}
72+
3873
}

spring-data-cassandra/src/main/resources/META-INF/spring/aot.factories

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ org.springframework.aot.hint.RuntimeHintsRegistrar=\
33

44
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
55
org.springframework.data.cassandra.aot.CassandraManagedTypesBeanRegistrationAotProcessor
6+
org.springframework.data.util.TypeCollector$TypeCollectorFilters=\
7+
org.springframework.data.cassandra.aot.CassandraManagedTypesBeanRegistrationAotProcessor$CassandraTypeFilters
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.cassandra.aot;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.data.util.TypeCollector;
23+
24+
import com.datastax.oss.driver.api.core.CqlIdentifier;
25+
import com.datastax.oss.driver.api.core.data.UdtValue;
26+
27+
/**
28+
* Unit tests for {@link CassandraManagedTypesBeanRegistrationAotProcessor}.
29+
*
30+
* @author Mark Paluch
31+
*/
32+
class CassandraManagedTypesBeanRegistrationAotProcessorTests {
33+
34+
@Test // GH-1606
35+
void shouldFilterUnreachableFieldTypes() {
36+
assertThat(TypeCollector.inspect(CassandraEntity.class).list()).containsOnly(CassandraEntity.class,
37+
Reachable.class);
38+
}
39+
40+
static class Reachable {
41+
42+
}
43+
44+
static class CassandraEntity {
45+
46+
private CqlIdentifier session;
47+
private UdtValue udt;
48+
private Reachable reachable;
49+
50+
public CassandraEntity(CqlIdentifier session, UdtValue udt) {
51+
this.session = session;
52+
this.udt = udt;
53+
}
54+
55+
public void setUdt(UdtValue udt) {
56+
57+
}
58+
59+
}
60+
61+
}

0 commit comments

Comments
 (0)