15
15
import java .util .List ;
16
16
import java .util .Map ;
17
17
import java .util .Optional ;
18
+ import java .util .Set ;
18
19
import java .util .stream .Collectors ;
19
20
20
21
import org .apache .commons .text .StringEscapeUtils ;
@@ -80,18 +81,30 @@ static boolean isValidMethodBinding(IMethodBinding methodBinding) {
80
81
return true ;
81
82
}
82
83
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 ) {
84
99
final String repositoryClass = methodBinding .getDeclaringClass ().getBinaryName ().trim ();
85
- final IMethodBinding method = methodBinding .getMethodDeclaration ();
86
100
87
- DataRepositoryAotMetadata metadata = repositoryMetadataService .getRepositoryMetadata (project , repositoryClass );
101
+ return Optional .ofNullable (dataRepositoryAotMetadataService .getRepositoryMetadata (project , repositoryClass ));
102
+ }
88
103
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 ();
94
106
107
+ return Optional .ofNullable (dataRepositoryAotMetadataService .findMethod (metadata , method ));
95
108
}
96
109
97
110
protected void provideCodeLens (CancelChecker cancelToken , MethodDeclaration node , TextDocument document , List <CodeLens > resultAccumulator ) {
@@ -106,13 +119,14 @@ protected void provideCodeLens(CancelChecker cancelToken, MethodDeclaration node
106
119
107
120
if (isValidMethodBinding (methodBinding )) {
108
121
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 ))
111
125
.ifPresent (cls -> cls .forEach (resultAccumulator ::add ));
112
126
}
113
127
}
114
128
115
- private List <CodeLens > createCodeLenses (MethodDeclaration node , TextDocument document , String queryStatement ) {
129
+ private List <CodeLens > createCodeLenses (MethodDeclaration node , TextDocument document , DataRepositoryAotMetadata metadata ) {
116
130
List <CodeLens > codeLenses = new ArrayList <>(2 );
117
131
118
132
try {
@@ -122,13 +136,16 @@ private List<CodeLens> createCodeLenses(MethodDeclaration node, TextDocument doc
122
136
Range range = new Range (startPos , endPos );
123
137
AnnotationHierarchies hierarchyAnnot = AnnotationHierarchies .get (node );
124
138
125
- if (mb != null && hierarchyAnnot != null ) {
139
+ Optional <DataRepositoryAotMetadataMethod > methodMetadata = getMethodMetadata (repositoryMetadataService , metadata , mb );
140
+
141
+ if (mb != null && hierarchyAnnot != null && methodMetadata .isPresent ()) {
126
142
127
143
boolean isQueryAnnotated = hierarchyAnnot .isAnnotatedWith (mb , Annotations .DATA_JPA_QUERY )
128
144
|| hierarchyAnnot .isAnnotatedWith (mb , Annotations .DATA_MONGODB_QUERY );
145
+
129
146
130
147
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 ));
132
149
}
133
150
134
151
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
142
159
143
160
if (!isQueryAnnotated ) {
144
161
Command queryTitle = new Command ();
145
- queryTitle .setTitle (queryStatement );
162
+ queryTitle .setTitle (methodMetadata . get (). getQueryStatement ( metadata ) );
146
163
codeLenses .add (new CodeLens (range , queryTitle , null ));
147
164
}
148
165
}
@@ -152,15 +169,31 @@ private List<CodeLens> createCodeLenses(MethodDeclaration node, TextDocument doc
152
169
return codeLenses ;
153
170
}
154
171
155
- static FixDescriptor createFixDescriptor (IMethodBinding mb , String docUri , String queryStatement ) {
172
+ static FixDescriptor createFixDescriptor (IMethodBinding mb , String docUri , DataRepositoryAotMetadata metadata , DataRepositoryAotMetadataMethod methodMetadata ) {
156
173
return new FixDescriptor (AddAnnotationOverMethod .class .getName (), List .of (docUri ), "Turn into `@Query`" )
174
+
157
175
.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 ))));
164
184
}
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
+
165
198
166
199
}
0 commit comments