1414import java .util .List ;
1515import java .util .Map ;
1616import java .util .Set ;
17- import java .util .function .Consumer ;
17+ import java .util .function .BiConsumer ;
1818
1919import org .eclipse .jdt .core .dom .ASTVisitor ;
2020import org .eclipse .jdt .core .dom .Block ;
3535
3636public class WebConfigIndexer {
3737
38+ private static final String CONFIGURE_API_VERSIONING_METHOD = "configureApiVersioning" ;
39+ private static final String CONFIGURE_PATH_MATCHING_METHOD = "configurePathMatch" ;
40+
41+ private static Map <String , MethodInvocationExtractor > methodExtractors = initializeMethodExtractors ();
42+
3843 public static void indexWebConfig (Bean beanDefinition , TypeDeclaration type , SpringIndexerJavaContext context , TextDocument doc ) {
3944 AnnotationHierarchies annotationHierarchies = AnnotationHierarchies .get (type );
4045
@@ -54,106 +59,61 @@ public static void indexWebConfig(Bean beanDefinition, TypeDeclaration type, Spr
5459 throw new RequiredCompleteAstException ();
5560 }
5661
57- Builder builder = new WebConfigIndexElement .Builder ();
62+ MethodDeclaration configureVersioningMethod = findMethod (type , inTypeHierarchy , CONFIGURE_API_VERSIONING_METHOD );
63+ MethodDeclaration configurePathMethod = findMethod (type , inTypeHierarchy , CONFIGURE_PATH_MATCHING_METHOD );
5864
59- MethodDeclaration configureVersioningMethod = findConfigureVersioningMethod (type , inTypeHierarchy );
60- if (configureVersioningMethod != null ) {
61- scanConfigureApiVersioningMethodBody (builder , configureVersioningMethod .getBody (), context , doc );
62- }
65+
66+ if (configureVersioningMethod != null || configurePathMethod != null ) {
67+ Builder builder = new WebConfigIndexElement .Builder ();
68+
69+ if (configureVersioningMethod != null ) scanMethodBody (builder , configureVersioningMethod .getBody (), context , doc );
70+ if (configurePathMethod != null ) scanMethodBody (builder , configurePathMethod .getBody (), context , doc );
6371
64- WebConfigIndexElement webConfigIndexElement = builder .buildFor (beanDefinition .getLocation ());
65- if (webConfigIndexElement != null ) {
66- beanDefinition .addChild (webConfigIndexElement );
72+ WebConfigIndexElement webConfigIndexElement = builder .buildFor (beanDefinition .getLocation ());
73+ if (webConfigIndexElement != null ) {
74+ beanDefinition .addChild (webConfigIndexElement );
75+ }
6776 }
6877
6978 }
7079
71- private static void scanConfigureApiVersioningMethodBody (Builder builder , Block body , SpringIndexerJavaContext context , TextDocument doc ) {
80+ private static void scanMethodBody (Builder builder , Block body , SpringIndexerJavaContext context , TextDocument doc ) {
7281 if (body == null ) {
7382 return ;
7483 }
7584
76- builder .isVersionSupported (true );
77-
78- Map <String , Consumer <MethodInvocation >> apiVersionConfigurerMethods = new HashMap <>();
79- apiVersionConfigurerMethods .put ("addSupportedVersions" , (invocation ) -> {
80-
81- @ SuppressWarnings ("unchecked" )
82- List <Expression > arguments = invocation .arguments ();
83- for (Expression arg : arguments ) {
84- String [] expressionValueAsArray = ASTUtils .getExpressionValueAsArray (arg , (dep ) -> {});
85- for (String supportedVersion : expressionValueAsArray ) {
86- builder .supportedVersion (supportedVersion );
87- }
88- }
89- });
90-
91- apiVersionConfigurerMethods .put ("useRequestHeader" , (invocation ) -> {
92-
93- @ SuppressWarnings ("unchecked" )
94- List <Expression > arguments = invocation .arguments ();
95- if (arguments != null && arguments .size () == 1 ) {
96- String value = ASTUtils .getExpressionValueAsString (arguments .get (0 ), (d ) -> {});
97- if (value != null ) {
98- builder .versionStrategy ("Request Header: " + value );
99- }
100- }
101- });
102-
103- apiVersionConfigurerMethods .put ("usePathSegment" , (invocation ) -> {
104-
105- @ SuppressWarnings ("unchecked" )
106- List <Expression > arguments = invocation .arguments ();
107- if (arguments != null && arguments .size () == 1 ) {
108- String value = ASTUtils .getExpressionValueAsString (arguments .get (0 ), (d ) -> {});
109- if (value != null ) {
110- builder .versionStrategy ("Path Segment: " + value );
111- }
112- }
113- });
114-
115- apiVersionConfigurerMethods .put ("useQueryParam" , (invocation ) -> {
116-
117- @ SuppressWarnings ("unchecked" )
118- List <Expression > arguments = invocation .arguments ();
119- if (arguments != null && arguments .size () == 1 ) {
120- String value = ASTUtils .getExpressionValueAsString (arguments .get (0 ), (d ) -> {});
121- if (value != null ) {
122- builder .versionStrategy ("Query Param: " + value );
123- }
124- }
125- });
126-
12785 body .accept (new ASTVisitor () {
12886
12987 @ Override
13088 public boolean visit (MethodInvocation methodInvocation ) {
13189 String methodName = methodInvocation .getName ().toString ();
132- if (apiVersionConfigurerMethods .containsKey (methodName )) {
90+
91+ MethodInvocationExtractor invocationExtractor = methodExtractors .get (methodName );
92+ if (invocationExtractor != null ) {
13393
13494 IMethodBinding methodBinding = methodInvocation .resolveMethodBinding ();
13595 ITypeBinding declaringClass = methodBinding .getDeclaringClass ();
13696
137- if (declaringClass != null && Annotations . WEB_MVC_API_VERSION_CONFIGURER_INTERFACE .equals (declaringClass .getQualifiedName ())) {
138- apiVersionConfigurerMethods . get ( methodName ). accept ( methodInvocation );
97+ if (declaringClass != null && invocationExtractor . getTargetInvocationType () .equals (declaringClass .getQualifiedName ())) {
98+ invocationExtractor . extractParameters ( methodInvocation , builder );
13999 }
140100 }
141101
142102 return super .visit (methodInvocation );
143103 }
144104 });
145-
146- }
147105
148- private static MethodDeclaration findConfigureVersioningMethod (TypeDeclaration type , ITypeBinding webmvcConfigurerType ) {
106+ }
107+
108+ private static MethodDeclaration findMethod (TypeDeclaration type , ITypeBinding webmvcConfigurerType , String methodName ) {
149109 IMethodBinding [] webConfigurerMethods = webmvcConfigurerType .getDeclaredMethods ();
150110 if (webConfigurerMethods == null ) {
151111 return null ;
152112 }
153113
154114 IMethodBinding configureVersioningMethod = null ;
155115 for (IMethodBinding method : webConfigurerMethods ) {
156- if ("configureApiVersioning" .equals (method .getName ())) {
116+ if (methodName .equals (method .getName ())) {
157117 configureVersioningMethod = method ;
158118 }
159119 }
@@ -174,5 +134,85 @@ private static MethodDeclaration findConfigureVersioningMethod(TypeDeclaration t
174134
175135 return null ;
176136 }
137+
138+ private static Map <String , MethodInvocationExtractor > initializeMethodExtractors () {
139+ Map <String , MethodInvocationExtractor > result = new HashMap <>();
140+
141+ result .put ("addSupportedVersions" , new MultipleArgumentsExtractor (Annotations .WEB_MVC_API_VERSION_CONFIGURER_INTERFACE , (expression , webconfigBuilder ) -> {
142+ String [] expressionValueAsArray = ASTUtils .getExpressionValueAsArray (expression , (dep ) -> {});
143+ for (String supportedVersion : expressionValueAsArray ) {
144+ webconfigBuilder .supportedVersion (supportedVersion );
145+ }
146+ }));
147+
148+ result .put ("useRequestHeader" , new SingleArgumentExtractor (Annotations .WEB_MVC_API_VERSION_CONFIGURER_INTERFACE , 0 , (expression , webconfigBuilder ) -> {
149+ String value = ASTUtils .getExpressionValueAsString (expression , (d ) -> {});
150+ if (value != null ) {
151+ webconfigBuilder .versionStrategy ("Request Header: " + value );
152+ }
153+ }));
154+
155+ result .put ("usePathSegment" , new SingleArgumentExtractor (Annotations .WEB_MVC_API_VERSION_CONFIGURER_INTERFACE , 0 , (expression , webconfigBuilder ) -> {
156+ String value = ASTUtils .getExpressionValueAsString (expression , (d ) -> {});
157+ if (value != null ) {
158+ webconfigBuilder .versionStrategy ("Path Segment: " + value );
159+ }
160+ }));
161+
162+ result .put ("useQueryParam" , new SingleArgumentExtractor (Annotations .WEB_MVC_API_VERSION_CONFIGURER_INTERFACE , 0 , (expression , webconfigBuilder ) -> {
163+ String value = ASTUtils .getExpressionValueAsString (expression , (d ) -> {});
164+ if (value != null ) {
165+ webconfigBuilder .versionStrategy ("Query Param: " + value );
166+ }
167+ }));
168+
169+ result .put ("addPathPrefix" , new SingleArgumentExtractor (Annotations .WEB_MVC_PATH_MATCH_CONFIGURER_INTERFACE , 0 , (expression , webconfigBuilder ) -> {
170+ String value = ASTUtils .getExpressionValueAsString (expression , (d ) -> {});
171+ if (value != null ) {
172+ webconfigBuilder .pathPrefix ("Path Prefix: " + value );
173+ }
174+ }));
175+
176+
177+ return result ;
178+ }
179+
180+ interface MethodInvocationExtractor {
181+ String getTargetInvocationType ();
182+ void extractParameters (MethodInvocation methodInvocation , WebConfigIndexElement .Builder builder );
183+ }
177184
185+ record SingleArgumentExtractor (String invocationTargetType , int argumentNo , BiConsumer <Expression , WebConfigIndexElement .Builder > consumer ) implements MethodInvocationExtractor {
186+
187+ @ Override
188+ public String getTargetInvocationType () {
189+ return invocationTargetType ;
190+ }
191+
192+ public void extractParameters (MethodInvocation methodInvocation , WebConfigIndexElement .Builder builder ) {
193+ @ SuppressWarnings ("unchecked" )
194+ List <Expression > arguments = methodInvocation .arguments ();
195+ Expression expression = arguments .get (argumentNo );
196+ consumer .accept (expression , builder );
197+ }
198+
199+ }
200+
201+ record MultipleArgumentsExtractor (String invocationTargetType , BiConsumer <Expression , WebConfigIndexElement .Builder > consumer ) implements MethodInvocationExtractor {
202+
203+ @ Override
204+ public String getTargetInvocationType () {
205+ return invocationTargetType ;
206+ }
207+
208+ public void extractParameters (MethodInvocation methodInvocation , WebConfigIndexElement .Builder builder ) {
209+ @ SuppressWarnings ("unchecked" )
210+ List <Expression > arguments = methodInvocation .arguments ();
211+ for (Expression expression : arguments ) {
212+ consumer .accept (expression , builder );
213+ }
214+ }
215+
216+ }
217+
178218}
0 commit comments