3535import org .springframework .ide .vscode .boot .java .annotations .AnnotationHierarchies ;
3636import org .springframework .ide .vscode .boot .java .handlers .CodeLensProvider ;
3737import org .springframework .ide .vscode .boot .java .rewrite .RewriteRefactorings ;
38+ import org .springframework .ide .vscode .boot .java .utils .ASTUtils ;
39+ import org .springframework .ide .vscode .commons .Version ;
3840import org .springframework .ide .vscode .commons .java .IJavaProject ;
41+ import org .springframework .ide .vscode .commons .java .SpringProjectUtil ;
3942import org .springframework .ide .vscode .commons .languageserver .java .JavaProjectFinder ;
4043import org .springframework .ide .vscode .commons .rewrite .config .RecipeScope ;
4144import org .springframework .ide .vscode .commons .rewrite .java .AddAnnotationOverMethod ;
@@ -85,9 +88,19 @@ static boolean isValidMethodBinding(IMethodBinding methodBinding) {
8588 || methodBinding .getDeclaringClass ().getBinaryName () == null ) {
8689 return false ;
8790 }
91+
92+ if (ASTUtils .findInTypeHierarchy (methodBinding .getDeclaringClass (), Set .of (Constants .REPOSITORY_TYPE )) == null ) {
93+ return false ;
94+ }
95+
8896 return true ;
8997 }
9098
99+ static boolean isValidProject (IJavaProject jp ) {
100+ Version version = SpringProjectUtil .getDependencyVersion (jp , "spring-data-commons" );
101+ return version != null && version .compareTo (new Version (4 , 0 , 0 , null )) >= 0 ;
102+ }
103+
91104// static Optional<String> getDataQuery(DataRepositoryAotMetadataService repositoryMetadataService, IJavaProject project, IMethodBinding methodBinding) {
92105// final String repositoryClass = methodBinding.getDeclaringClass().getBinaryName().trim();
93106// final IMethodBinding method = methodBinding.getMethodDeclaration();
@@ -104,21 +117,14 @@ static boolean isValidMethodBinding(IMethodBinding methodBinding) {
104117
105118 static Optional <DataRepositoryAotMetadata > getMetadata (DataRepositoryAotMetadataService dataRepositoryAotMetadataService , IJavaProject project , IMethodBinding methodBinding ) {
106119 final String repositoryClass = methodBinding .getDeclaringClass ().getBinaryName ().trim ();
107-
108- return Optional .ofNullable (dataRepositoryAotMetadataService .getRepositoryMetadata (project , repositoryClass ));
109- }
110-
111- static Optional <IDataRepositoryAotMethodMetadata > getMethodMetadata (DataRepositoryAotMetadataService dataRepositoryAotMetadataService , DataRepositoryAotMetadata metadata , IMethodBinding methodBinding ) {
112- final IMethodBinding method = methodBinding .getMethodDeclaration ();
113-
114- return Optional .ofNullable (metadata .findMethod (method ));
120+ return dataRepositoryAotMetadataService .getRepositoryMetadata (project , repositoryClass );
115121 }
116122
117123 protected void provideCodeLens (CancelChecker cancelToken , MethodDeclaration node , TextDocument document , List <CodeLens > resultAccumulator ) {
118124 cancelToken .checkCanceled ();
119125
120126 IJavaProject project = projectFinder .find (document .getId ()).get ();
121- if (project == null ) {
127+ if (project == null || ! isValidProject ( project ) ) {
122128 return ;
123129 }
124130
@@ -127,13 +133,11 @@ protected void provideCodeLens(CancelChecker cancelToken, MethodDeclaration node
127133 if (isValidMethodBinding (methodBinding )) {
128134 cancelToken .checkCanceled ();
129135
130- getMetadata (repositoryMetadataService , project , methodBinding )
131- .map (metadata -> createCodeLenses (node , document , metadata ))
132- .ifPresent (cls -> cls .forEach (resultAccumulator ::add ));
136+ resultAccumulator .addAll (createCodeLenses (project , node , document ));
133137 }
134138 }
135139
136- private List <CodeLens > createCodeLenses (MethodDeclaration node , TextDocument document , DataRepositoryAotMetadata metadata ) {
140+ private List <CodeLens > createCodeLenses (IJavaProject project , MethodDeclaration node , TextDocument document ) {
137141 List <CodeLens > codeLenses = new ArrayList <>(2 );
138142
139143 try {
@@ -143,49 +147,52 @@ private List<CodeLens> createCodeLenses(MethodDeclaration node, TextDocument doc
143147 Range range = new Range (startPos , endPos );
144148 AnnotationHierarchies hierarchyAnnot = AnnotationHierarchies .get (node );
145149
146- Optional <IDataRepositoryAotMethodMetadata > methodMetadata = getMethodMetadata (repositoryMetadataService , metadata , mb );
147-
148- if (mb != null && hierarchyAnnot != null && methodMetadata .isPresent ()) {
149-
150- boolean isQueryAnnotated = hierarchyAnnot .isAnnotatedWith (mb , Annotations .DATA_JPA_QUERY )
151- || hierarchyAnnot .isAnnotatedWith (mb , Annotations .DATA_MONGODB_QUERY );
152-
153-
154- if (!isQueryAnnotated ) {
155- codeLenses .add (new CodeLens (range , refactorings .createFixCommand (COVERT_TO_QUERY_LABEL , createFixDescriptor (mb , document .getUri (), metadata , methodMetadata .get ())), null ));
156- }
157-
158- Command impl = new Command ("Implementation" , GenAotQueryMethodImplProvider .CMD_NAVIGATE_TO_IMPL , List .of (new GenAotQueryMethodImplProvider .GoToImplParams (
159- document .getId (),
160- mb .getDeclaringClass ().getQualifiedName (),
161- mb .getName (),
162- Arrays .stream (mb .getParameterTypes ()).map (p -> p .getQualifiedName ()).toArray (String []::new ),
163- null
164- )));
165- codeLenses .add (new CodeLens (range , impl , null ));
150+ if (mb != null && hierarchyAnnot != null ) {
166151
167- if (! isQueryAnnotated ) {
168- String queryStatement = methodMetadata . get (). getQueryStatement ();
169- if ( queryStatement != null ) {
170- Command queryTitle = new Command ();
171- queryTitle . setTitle ( queryStatement );
172- codeLenses .add (new CodeLens (range , queryTitle , null ));
152+ getMetadata ( repositoryMetadataService , project , mb ). ifPresent ( metadata -> metadata . findMethod ( mb ). ifPresent ( methodMetadata -> {
153+ boolean isQueryAnnotated = hierarchyAnnot . isAnnotatedWith ( mb , Annotations . DATA_JPA_QUERY )
154+ || hierarchyAnnot . isAnnotatedWith ( mb , Annotations . DATA_MONGODB_QUERY );
155+
156+ if (! isQueryAnnotated ) {
157+ codeLenses .add (new CodeLens (range , refactorings . createFixCommand ( COVERT_TO_QUERY_LABEL , createFixDescriptor ( mb , document . getUri (), metadata . module (), methodMetadata )) , null ));
173158 }
174- }
159+
160+ Command impl = new Command ("Implementation" , GenAotQueryMethodImplProvider .CMD_NAVIGATE_TO_IMPL , List .of (new GenAotQueryMethodImplProvider .GoToImplParams (
161+ document .getId (),
162+ mb .getDeclaringClass ().getQualifiedName (),
163+ mb .getName (),
164+ Arrays .stream (mb .getParameterTypes ()).map (p -> p .getQualifiedName ()).toArray (String []::new ),
165+ null
166+ )));
167+ codeLenses .add (new CodeLens (range , impl , null ));
168+
169+ if (!isQueryAnnotated && methodMetadata != null ) {
170+ String queryStatement = methodMetadata .getQueryStatement ();
171+ if (queryStatement != null ) {
172+ Command queryTitle = new Command ();
173+ queryTitle .setTitle (queryStatement );
174+ codeLenses .add (new CodeLens (range , queryTitle , null ));
175+ }
176+ }
177+ }));
178+
179+ Command refreshCmd = new Command ("Refresh" , DataRepositoryAotMetadataService .CMD_REFESH_METADATA , List .of (project .getElementName ()));
180+ codeLenses .add (new CodeLens (range , refreshCmd , null ));
181+
175182 }
176183 } catch (BadLocationException e ) {
177184 log .error ("bad location while calculating code lens for data repository query method" , e );
178185 }
179186 return codeLenses ;
180187 }
181188
182- static FixDescriptor createFixDescriptor (IMethodBinding mb , String docUri , DataRepositoryAotMetadata metadata , IDataRepositoryAotMethodMetadata methodMetadata ) {
189+ static FixDescriptor createFixDescriptor (IMethodBinding mb , String docUri , DataRepositoryModule module , IDataRepositoryAotMethodMetadata methodMetadata ) {
183190 return new FixDescriptor (AddAnnotationOverMethod .class .getName (), List .of (docUri ), "Turn into `@Query`" )
184191
185192 .withRecipeScope (RecipeScope .FILE )
186193
187194 .withParameters (Map .of (
188- "annotationType" , metadata . module () == DataRepositoryModule .JPA ? Annotations .DATA_JPA_QUERY : Annotations .DATA_MONGODB_QUERY ,
195+ "annotationType" , module == DataRepositoryModule .JPA ? Annotations .DATA_JPA_QUERY : Annotations .DATA_MONGODB_QUERY ,
189196 "method" , "%s %s(%s)" .formatted (mb .getDeclaringClass ().getQualifiedName (), mb .getName (),
190197 Arrays .stream (mb .getParameterTypes ())
191198 .map (pt -> pt .getQualifiedName ())
@@ -203,7 +210,5 @@ private static List<AddAnnotationOverMethod.Attribute> createAttributeList(Map<S
203210
204211 return result ;
205212 }
206-
207-
208213
209214}
0 commit comments