90
90
import java .util .HashMap ;
91
91
import java .util .HashSet ;
92
92
import java .util .Iterator ;
93
+ import java .util .LinkedHashMap ;
93
94
import java .util .List ;
94
95
import java .util .ListIterator ;
95
96
import java .util .Map ;
@@ -111,11 +112,11 @@ public class RepositoryTypeElementVisitor implements TypeElementVisitor<Reposito
111
112
public static final String SPRING_REPO = "org.springframework.data.repository.Repository" ;
112
113
public static final String JAKARTA_DATA_REPO = "jakarta.data.repository.DataRepository" ;
113
114
private static final boolean IS_DOCUMENT_ANNOTATION_PROCESSOR = ClassUtils .isPresent ("io.micronaut.data.document.processor.mapper.MappedEntityMapper" , RepositoryTypeElementVisitor .class .getClassLoader ());
115
+ private static final Map <String , String > COMMON_TYPE_ROLES ;
114
116
115
117
private ClassElement currentClass ;
116
118
private ClassElement currentRepository ;
117
119
private QueryBuilder queryEncoder ;
118
- private final Map <String , String > typeRoles = new HashMap <>();
119
120
private final List <MethodMatcher > methodsMatchers ;
120
121
private boolean failing = false ;
121
122
private final Set <String > visitedRepositories = new HashSet <>();
@@ -131,28 +132,36 @@ public class RepositoryTypeElementVisitor implements TypeElementVisitor<Reposito
131
132
methodsMatchers = matcherList ;
132
133
}
133
134
135
+ static {
136
+ Map <String , String > roles = new LinkedHashMap <>();
137
+ roles .put (Pageable .class .getName (), TypeRole .PAGEABLE );
138
+ roles .put (Sort .class .getName (), TypeRole .SORT );
139
+ roles .put (CursoredPage .class .getName (), TypeRole .CURSORED_PAGE );
140
+ roles .put (Page .class .getName (), TypeRole .PAGE );
141
+ roles .put (Slice .class .getName (), TypeRole .SLICE );
142
+ roles .put (Limit .class .getName (), TypeRole .LIMIT );
143
+ // Spring Data
144
+ roles .put ("org.springframework.data.domain.Pageable" , TypeRole .PAGEABLE );
145
+ roles .put ("org.springframework.data.domain.Page" , TypeRole .PAGE );
146
+ roles .put ("org.springframework.data.domain.Slice" , TypeRole .SLICE );
147
+ roles .put ("org.springframework.data.domain.Sort" , TypeRole .SORT );
148
+ // Jakarta Data
149
+ roles .put ("jakarta.data.page.Page" , TypeRole .PAGE );
150
+ roles .put ("jakarta.data.page.CursoredPage" , TypeRole .CURSORED_PAGE );
151
+ roles .put ("jakarta.data.page.PageRequest" , TypeRole .PAGEABLE );
152
+ roles .put ("jakarta.data.Order" , TypeRole .SORT );
153
+ roles .put ("jakarta.data.Sort" , TypeRole .SORT );
154
+ roles .put ("jakarta.data.Limit" , TypeRole .LIMIT );
155
+ COMMON_TYPE_ROLES = Collections .unmodifiableMap (roles );
156
+ }
157
+
158
+ private Map <String , String > currentClassTypeRoles ;
159
+
134
160
/**
135
161
* Default constructor.
136
162
*/
137
163
public RepositoryTypeElementVisitor () {
138
- typeRoles .put (Pageable .class .getName (), TypeRole .PAGEABLE );
139
- typeRoles .put (Sort .class .getName (), TypeRole .SORT );
140
- typeRoles .put (CursoredPage .class .getName (), TypeRole .CURSORED_PAGE );
141
- typeRoles .put (Page .class .getName (), TypeRole .PAGE );
142
- typeRoles .put (Slice .class .getName (), TypeRole .SLICE );
143
- typeRoles .put (Limit .class .getName (), TypeRole .LIMIT );
144
- // Spring Data
145
- typeRoles .put ("org.springframework.data.domain.Pageable" , TypeRole .PAGEABLE );
146
- typeRoles .put ("org.springframework.data.domain.Page" , TypeRole .PAGE );
147
- typeRoles .put ("org.springframework.data.domain.Slice" , TypeRole .SLICE );
148
- typeRoles .put ("org.springframework.data.domain.Sort" , TypeRole .SORT );
149
- // Jakarta Data
150
- typeRoles .put ("jakarta.data.page.Page" , TypeRole .PAGE );
151
- typeRoles .put ("jakarta.data.page.CursoredPage" , TypeRole .CURSORED_PAGE );
152
- typeRoles .put ("jakarta.data.page.PageRequest" , TypeRole .PAGEABLE );
153
- typeRoles .put ("jakarta.data.Order" , TypeRole .SORT );
154
- typeRoles .put ("jakarta.data.Sort" , TypeRole .SORT );
155
- typeRoles .put ("jakarta.data.Limit" , TypeRole .LIMIT );
164
+
156
165
}
157
166
158
167
@ NonNull
@@ -181,6 +190,7 @@ private Map<ClassElement, FindInterceptorDef> createFindInterceptors(ClassElemen
181
190
182
191
@ Override
183
192
public void visitClass (ClassElement element , VisitorContext context ) {
193
+ currentClassTypeRoles = new LinkedHashMap <>(COMMON_TYPE_ROLES );
184
194
String interfaceName = element .getName ();
185
195
if (failing ) {
186
196
return ;
@@ -245,7 +255,7 @@ public SourcePersistentEntity apply(String entitySimpleName) {
245
255
AnnotationClassValue cv = parameterRole .get ("type" , AnnotationClassValue .class ).orElse (null );
246
256
if (StringUtils .isNotEmpty (role ) && cv != null ) {
247
257
context .getClassElement (cv .getName ()).ifPresent (ce ->
248
- typeRoles .put (ce .getName (), role )
258
+ currentClassTypeRoles .put (ce .getName (), role )
249
259
);
250
260
}
251
261
}
@@ -283,7 +293,7 @@ public void visitMethod(MethodElement element, VisitorContext context) {
283
293
currentRepository ,
284
294
context ,
285
295
element ,
286
- typeRoles ,
296
+ currentClassTypeRoles ,
287
297
genericReturnType ,
288
298
parameters ,
289
299
findInterceptors );
@@ -301,7 +311,7 @@ public void visitMethod(MethodElement element, VisitorContext context) {
301
311
genericReturnType ,
302
312
element ,
303
313
parametersInRole ,
304
- typeRoles ,
314
+ currentClassTypeRoles ,
305
315
parameters ,
306
316
entityResolver ,
307
317
findInterceptors ,
@@ -340,10 +350,10 @@ public void visitMethod(MethodElement element, VisitorContext context) {
340
350
}
341
351
342
352
private Map <Element , String > getParametersInRole (ParameterElement [] parameters ) {
343
- Map <Element , String > parametersInRole = new HashMap <>(2 );
353
+ Map <Element , String > parametersInRole = new LinkedHashMap <>(2 );
344
354
for (ParameterElement parameter : parameters ) {
345
355
ClassElement type = parameter .getType ();
346
- this . typeRoles .entrySet ().stream ().filter (entry -> {
356
+ currentClassTypeRoles .entrySet ().stream ().filter (entry -> {
347
357
String roleType = entry .getKey ();
348
358
return type .isAssignable (roleType );
349
359
}
@@ -358,7 +368,7 @@ private List<ParameterElement> getParametersNotInRole(ParameterElement[] paramet
358
368
List <ParameterElement > parametersNotInRole = new ArrayList <>();
359
369
for (ParameterElement parameter : parameters ) {
360
370
ClassElement type = parameter .getType ();
361
- if (this . typeRoles .keySet ().stream ().noneMatch (type ::isAssignable )) {
371
+ if (currentClassTypeRoles .keySet ().stream ().noneMatch (type ::isAssignable )) {
362
372
parametersNotInRole .add (parameter );
363
373
}
364
374
}
@@ -488,7 +498,7 @@ private void processMethodInfo(MethodMatchContext methodMatchContext, MethodMatc
488
498
}
489
499
490
500
ClassElement returnType = method .getReturnType ().getType ();
491
- for (Map .Entry <String , String > stringStringEntry : typeRoles .entrySet ()) {
501
+ for (Map .Entry <String , String > stringStringEntry : currentClassTypeRoles .entrySet ()) {
492
502
if (returnType .isAssignable (stringStringEntry .getKey ())) {
493
503
annotationBuilder .member (DataMethodQuery .META_MEMBER_RETURN_TYPE_ROLE , stringStringEntry .getValue ());
494
504
break ;
0 commit comments