1515import java .util .List ;
1616import java .util .Map ;
1717import java .util .Optional ;
18+ import java .util .Set ;
1819import java .util .stream .Collectors ;
1920
2021import org .apache .commons .text .StringEscapeUtils ;
@@ -80,18 +81,30 @@ static boolean isValidMethodBinding(IMethodBinding methodBinding) {
8081 return true ;
8182 }
8283
83- static Optional <String > getDataQuery (DataRepositoryAotMetadataService repositoryMetadataService , IJavaProject project , IMethodBinding methodBinding ) {
84+ // static Optional<String> getDataQuery(DataRepositoryAotMetadataService repositoryMetadataService, IJavaProject project, IMethodBinding methodBinding) {
85+ // final String repositoryClass = methodBinding.getDeclaringClass().getBinaryName().trim();
86+ // final IMethodBinding method = methodBinding.getMethodDeclaration();
87+ //
88+ // DataRepositoryAotMetadata metadata = repositoryMetadataService.getRepositoryMetadata(project, repositoryClass);
89+ //
90+ // if (metadata != null) {
91+ // return Optional.ofNullable(repositoryMetadataService.getQueryStatement(metadata, method));
92+ // }
93+ //
94+ // return Optional.empty();
95+ //
96+ // }
97+
98+ static Optional <DataRepositoryAotMetadata > getMetadata (DataRepositoryAotMetadataService dataRepositoryAotMetadataService , IJavaProject project , IMethodBinding methodBinding ) {
8499 final String repositoryClass = methodBinding .getDeclaringClass ().getBinaryName ().trim ();
85- final IMethodBinding method = methodBinding .getMethodDeclaration ();
86100
87- DataRepositoryAotMetadata metadata = repositoryMetadataService .getRepositoryMetadata (project , repositoryClass );
101+ return Optional .ofNullable (dataRepositoryAotMetadataService .getRepositoryMetadata (project , repositoryClass ));
102+ }
88103
89- if (metadata != null ) {
90- return Optional .ofNullable (repositoryMetadataService .getQueryStatement (metadata , method ));
91- }
92-
93- return Optional .empty ();
104+ static Optional <DataRepositoryAotMetadataMethod > getMethodMetadata (DataRepositoryAotMetadataService dataRepositoryAotMetadataService , DataRepositoryAotMetadata metadata , IMethodBinding methodBinding ) {
105+ final IMethodBinding method = methodBinding .getMethodDeclaration ();
94106
107+ return Optional .ofNullable (dataRepositoryAotMetadataService .findMethod (metadata , method ));
95108 }
96109
97110 protected void provideCodeLens (CancelChecker cancelToken , MethodDeclaration node , TextDocument document , List <CodeLens > resultAccumulator ) {
@@ -106,13 +119,14 @@ protected void provideCodeLens(CancelChecker cancelToken, MethodDeclaration node
106119
107120 if (isValidMethodBinding (methodBinding )) {
108121 cancelToken .checkCanceled ();
109- getDataQuery (repositoryMetadataService , project , methodBinding )
110- .map (queryStatement -> createCodeLenses (node , document , queryStatement ))
122+
123+ getMetadata (repositoryMetadataService , project , methodBinding )
124+ .map (metadata -> createCodeLenses (node , document , metadata ))
111125 .ifPresent (cls -> cls .forEach (resultAccumulator ::add ));
112126 }
113127 }
114128
115- private List <CodeLens > createCodeLenses (MethodDeclaration node , TextDocument document , String queryStatement ) {
129+ private List <CodeLens > createCodeLenses (MethodDeclaration node , TextDocument document , DataRepositoryAotMetadata metadata ) {
116130 List <CodeLens > codeLenses = new ArrayList <>(2 );
117131
118132 try {
@@ -122,13 +136,16 @@ private List<CodeLens> createCodeLenses(MethodDeclaration node, TextDocument doc
122136 Range range = new Range (startPos , endPos );
123137 AnnotationHierarchies hierarchyAnnot = AnnotationHierarchies .get (node );
124138
125- if (mb != null && hierarchyAnnot != null ) {
139+ Optional <DataRepositoryAotMetadataMethod > methodMetadata = getMethodMetadata (repositoryMetadataService , metadata , mb );
140+
141+ if (mb != null && hierarchyAnnot != null && methodMetadata .isPresent ()) {
126142
127143 boolean isQueryAnnotated = hierarchyAnnot .isAnnotatedWith (mb , Annotations .DATA_JPA_QUERY )
128144 || hierarchyAnnot .isAnnotatedWith (mb , Annotations .DATA_MONGODB_QUERY );
145+
129146
130147 if (!isQueryAnnotated ) {
131- codeLenses .add (new CodeLens (range , refactorings .createFixCommand (COVERT_TO_QUERY_LABEL , createFixDescriptor (mb , document .getUri (), queryStatement )), null ));
148+ codeLenses .add (new CodeLens (range , refactorings .createFixCommand (COVERT_TO_QUERY_LABEL , createFixDescriptor (mb , document .getUri (), metadata , methodMetadata . get () )), null ));
132149 }
133150
134151 Command impl = new Command ("Implementation" , GenAotQueryMethodImplProvider .CMD_NAVIGATE_TO_IMPL , List .of (new GenAotQueryMethodImplProvider .GoToImplParams (
@@ -142,7 +159,7 @@ private List<CodeLens> createCodeLenses(MethodDeclaration node, TextDocument doc
142159
143160 if (!isQueryAnnotated ) {
144161 Command queryTitle = new Command ();
145- queryTitle .setTitle (queryStatement );
162+ queryTitle .setTitle (methodMetadata . get (). getQueryStatement ( metadata ) );
146163 codeLenses .add (new CodeLens (range , queryTitle , null ));
147164 }
148165 }
@@ -152,15 +169,31 @@ private List<CodeLens> createCodeLenses(MethodDeclaration node, TextDocument doc
152169 return codeLenses ;
153170 }
154171
155- static FixDescriptor createFixDescriptor (IMethodBinding mb , String docUri , String queryStatement ) {
172+ static FixDescriptor createFixDescriptor (IMethodBinding mb , String docUri , DataRepositoryAotMetadata metadata , DataRepositoryAotMetadataMethod methodMetadata ) {
156173 return new FixDescriptor (AddAnnotationOverMethod .class .getName (), List .of (docUri ), "Turn into `@Query`" )
174+
157175 .withRecipeScope (RecipeScope .FILE )
158- .withParameters (Map .of ("annotationType" , Annotations .DATA_JPA_QUERY , "method" ,
159- "%s %s(%s)" .formatted (mb .getDeclaringClass ().getQualifiedName (), mb .getName (),
160- Arrays .stream (mb .getParameterTypes ()).map (pt -> pt .getQualifiedName ())
161- .collect (Collectors .joining (", " ))),
162- "attributes" , List .of (new AddAnnotationOverMethod .Attribute ("value" ,
163- "\" %s\" " .formatted (StringEscapeUtils .escapeJava (queryStatement ))))));
176+
177+ .withParameters (Map .of (
178+ "annotationType" , metadata .isJPA () ? Annotations .DATA_JPA_QUERY : Annotations .DATA_MONGODB_QUERY ,
179+ "method" , "%s %s(%s)" .formatted (mb .getDeclaringClass ().getQualifiedName (), mb .getName (),
180+ Arrays .stream (mb .getParameterTypes ())
181+ .map (pt -> pt .getQualifiedName ())
182+ .collect (Collectors .joining (", " ))),
183+ "attributes" , createAttributeList (methodMetadata .getAttributesMap (metadata ))));
164184 }
185+
186+ private static List <AddAnnotationOverMethod .Attribute > createAttributeList (Map <String , String > attributes ) {
187+ List <AddAnnotationOverMethod .Attribute > result = new ArrayList <>();
188+
189+ Set <String > keys = attributes .keySet ();
190+ for (String key : keys ) {
191+ result .add (new AddAnnotationOverMethod .Attribute (key , "\" %s\" " .formatted (StringEscapeUtils .escapeJava (attributes .get (key )))));
192+ }
193+
194+ return result ;
195+ }
196+
197+
165198
166199}
0 commit comments