Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 319e101

Browse files
committed
Refine TypeModelProcessor class filtering and error handling
Closes gh-1376
1 parent 656ba12 commit 319e101

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

samples/data-mongodb/src/main/java/com/example/data/mongo/Order.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@
2525
import org.springframework.data.annotation.LastModifiedBy;
2626
import org.springframework.data.annotation.LastModifiedDate;
2727
import org.springframework.data.annotation.PersistenceConstructor;
28+
import org.springframework.data.domain.Sort;
29+
import org.springframework.data.mongodb.core.index.Index;
30+
import org.springframework.data.mongodb.core.index.IndexDefinition;
31+
import org.springframework.data.mongodb.core.index.IndexResolver;
2832
import org.springframework.data.mongodb.core.index.Indexed;
33+
import org.springframework.data.mongodb.core.index.PartialIndexFilter;
2934
import org.springframework.data.mongodb.core.mapping.DBRef;
3035
import org.springframework.data.mongodb.core.mapping.Document;
3136
import org.springframework.data.mongodb.core.mapping.DocumentReference;
3237
import org.springframework.data.mongodb.core.mapping.Field;
38+
import org.springframework.data.mongodb.core.query.Criteria;
3339

3440
@Document
3541
public class Order {
@@ -189,4 +195,15 @@ public String toString() {
189195
", reduction=" + reduction +
190196
'}';
191197
}
198+
199+
// Reproducer for https://github.com/spring-projects-experimental/spring-native/issues/1376
200+
public static List<IndexDefinition> getIndexes(IndexResolver indexResolver) {
201+
List<IndexDefinition> ret = new ArrayList<>();
202+
indexResolver.resolveIndexFor(Order.class).forEach(ret::add);
203+
ret.add(new Index()
204+
.unique()
205+
.on("createdBy", Sort.Direction.ASC)
206+
.partial(PartialIndexFilter.of(Criteria.where("createdBy").exists(true))));
207+
return ret;
208+
}
192209
}

spring-aot/src/main/java/org/springframework/data/TypeModelProcessor.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,22 @@
3535
import java.util.function.Predicate;
3636
import java.util.stream.Collectors;
3737

38+
import org.apache.commons.logging.Log;
39+
import org.apache.commons.logging.LogFactory;
40+
3841
import org.springframework.core.ResolvableType;
3942
import org.springframework.util.ReflectionUtils;
4043

4144
/**
4245
* @author Christoph Strobl
46+
* @author Sebastien Deleuze
4347
*/
4448
public class TypeModelProcessor {
4549

46-
static final Set<String> EXCLUDED_DOMAINS = new HashSet<>(Arrays.asList("sun.", "jdk.", "reactor.", "kotlinx.", "kotlin."));
50+
private static Log logger = LogFactory.getLog(TypeModelProcessor.class);
51+
52+
static final Set<String> EXCLUDED_DOMAINS = new HashSet<>(Arrays.asList("sun.", "jdk.", "reactor.", "kotlinx.", "kotlin.",
53+
"org.springframework.core.", "org.springframework.data.", "org.springframework.boot."));
4754

4855
private Predicate<Class<?>> typeFilter = (type) -> EXCLUDED_DOMAINS.stream().noneMatch(it -> {
4956
if (type.getPackageName().startsWith("java.")) {
@@ -151,17 +158,21 @@ Set<Type> visitMethodsOfType(ResolvableType type, TypeModel result) {
151158
return Collections.emptySet();
152159
}
153160
Set<Type> discoveredTypes = new LinkedHashSet<>();
154-
ReflectionUtils.doWithLocalMethods(type.toClass(), method -> {
155-
if (!methodFilter.test(method)) {
156-
return;
157-
}
158-
result.addMethod(method);
159-
for (Class<?> signatureType : TypeUtils.resolveTypesInSignature(type.toClass(), method)) {
160-
if (typeFilter.test(signatureType)) {
161-
discoveredTypes.add(signatureType);
161+
try {
162+
ReflectionUtils.doWithLocalMethods(type.toClass(), method -> {
163+
if (!methodFilter.test(method)) {
164+
return;
162165
}
163-
}
164-
});
166+
result.addMethod(method);
167+
for (Class<?> signatureType : TypeUtils.resolveTypesInSignature(type.toClass(), method)) {
168+
if (typeFilter.test(signatureType)) {
169+
discoveredTypes.add(signatureType);
170+
}
171+
}
172+
});
173+
} catch (Exception ex) {
174+
logger.warn(ex);
175+
}
165176
return new HashSet<>(discoveredTypes);
166177
}
167178

0 commit comments

Comments
 (0)