Skip to content

Commit 6a34458

Browse files
jiafu1115ericbottard
authored andcommitted
Improve hasJacksonAnnotations performance with early return
Signed-off-by: stroller <[email protected]>
1 parent 348085a commit 6a34458

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

spring-ai-model/src/main/java/org/springframework/ai/aot/AiRuntimeHints.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @author Josh Long
4242
* @author Christian Tzolov
4343
* @author Mark Pollack
44+
* @author Fu Jian
4445
*/
4546
public abstract class AiRuntimeHints {
4647

@@ -100,12 +101,11 @@ public static Set<TypeReference> findClassesInPackage(String packageName, TypeFi
100101
}
101102

102103
private static boolean hasJacksonAnnotations(Class<?> type) {
103-
var hasAnnotation = false;
104104
var annotationsToFind = Set.of(JsonProperty.class, JsonInclude.class);
105105
for (var annotationToFind : annotationsToFind) {
106106

107107
if (type.isAnnotationPresent(annotationToFind)) {
108-
hasAnnotation = true;
108+
return true;
109109
}
110110

111111
var executables = new HashSet<Executable>();
@@ -114,35 +114,33 @@ private static boolean hasJacksonAnnotations(Class<?> type) {
114114
executables.addAll(Set.of(type.getDeclaredConstructors()));
115115

116116
for (var executable : executables) {
117-
//
118117
if (executable.isAnnotationPresent(annotationToFind)) {
119-
hasAnnotation = true;
118+
return true;
120119
}
121120

122-
///
123121
for (var p : executable.getParameters()) {
124122
if (p.isAnnotationPresent(annotationToFind)) {
125-
hasAnnotation = true;
123+
return true;
126124
}
127125
}
128126
}
129127

130128
if (type.getRecordComponents() != null) {
131129
for (var r : type.getRecordComponents()) {
132130
if (r.isAnnotationPresent(annotationToFind)) {
133-
hasAnnotation = true;
131+
return true;
134132
}
135133
}
136134
}
137135

138136
for (var f : type.getFields()) {
139137
if (f.isAnnotationPresent(annotationToFind)) {
140-
hasAnnotation = true;
138+
return true;
141139
}
142140
}
143141
}
144142

145-
return hasAnnotation;
143+
return false;
146144
}
147145

148146
private static Set<Class<?>> discoverJacksonAnnotatedTypesFromRootType(Class<?> type) {

0 commit comments

Comments
 (0)