GH-1536: include signature information in query method index nodes

This commit is contained in:
Martin Lippert
2025-04-10 10:05:15 +02:00
parent 1ce3f1b90d
commit 8cf3836ed7
6 changed files with 74 additions and 11 deletions

View File

@@ -22,6 +22,7 @@ import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.NormalAnnotation;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Range;
@@ -108,14 +109,15 @@ public class DataRepositorySymbolProvider implements SymbolProvider {
if (nameNode != null) {
String methodName = nameNode.getFullyQualifiedName();
DocumentRegion nodeRegion = ASTUtils.nodeRegion(doc, method);
DocumentRegion nodeRegion = ASTUtils.nodeRegion(doc, nameNode);
try {
Range range = doc.toRange(nodeRegion);
if (methodName != null) {
String queryString = identifyQueryString(method, annotationHierarchies);
beanDefinition.addChild(new QueryMethodIndexElement(methodName, queryString, range));
String methodSignature = identifyMethodSignature(method);
beanDefinition.addChild(new QueryMethodIndexElement(methodSignature, queryString, range));
}
} catch (BadLocationException e) {
@@ -125,6 +127,36 @@ public class DataRepositorySymbolProvider implements SymbolProvider {
}
}
private String identifyMethodSignature(MethodDeclaration method) {
StringBuilder result = new StringBuilder();
// method name
String name = method.getName().getFullyQualifiedName();
result.append(name);
// params
result.append("(");
@SuppressWarnings("unchecked")
List<SingleVariableDeclaration> parameters = method.parameters();
String[] paramNames = new String[parameters.size()];
for (int i = 0; i < parameters.size(); i++) {
ITypeBinding type = parameters.get(i).getType().resolveBinding();
paramNames[i] = type.getName();
}
result.append(String.join(", ", paramNames));
result.append(") : ");
// return type
ITypeBinding returnType = method.getReturnType2().resolveBinding();
String returnTypeName = returnType.getName();
result.append(returnTypeName);
return result.toString();
}
private List<MethodDeclaration> identifyQueryMethods(TypeDeclaration type, AnnotationHierarchies annotationHierarchies) {
List<MethodDeclaration> result = new ArrayList<>();

View File

@@ -92,7 +92,7 @@ public class SpringIndexerJava implements SpringIndexer {
// whenever the implementation of the indexer changes in a way that the stored data in the cache is no longer valid,
// we need to change the generation - this will result in a re-indexing due to no up-to-date cache data being found
private static final String GENERATION = "GEN-19";
private static final String GENERATION = "GEN-20";
private static final String INDEX_FILES_TASK_ID = "index-java-source-files-task-";
private static final String SYMBOL_KEY = "symbols";

View File

@@ -84,7 +84,7 @@ public class DataRepositoryIndexElementsTest {
assertEquals("org.test.CustomerRepository", repoBean[0].getType());
Bean[] matchingBeans = springIndex.getMatchingBeans("test-spring-data-symbols", "org.springframework.data.repository.CrudRepository");
assertEquals(4, matchingBeans.length);
assertEquals(5, matchingBeans.length);
ArrayUtils.contains(matchingBeans, repoBean[0]);
}
@@ -100,7 +100,22 @@ public class DataRepositoryIndexElementsTest {
assertEquals(1, queryMethods.size());
QueryMethodIndexElement queryMethod = (QueryMethodIndexElement) queryMethods.get(0);
assertEquals("findByLastName", queryMethod.getMethodName());
assertEquals("findByLastName(String) : List<Customer>", queryMethod.getMethodName());
}
@Test
void testSimpleQueryMethodElementsWithTwoParams() throws Exception {
String docUri = directory.toPath().resolve("src/main/java/org/test/CustomerRepositoryWithTwoParamsMethod.java").toUri().toString();
DocumentElement document = springIndex.getDocument(docUri);
List<SpringIndexElement> children = document.getChildren();
Bean repositoryElement = (Bean) children.get(0);
List<SpringIndexElement> queryMethods = repositoryElement.getChildren();
assertEquals(1, queryMethods.size());
QueryMethodIndexElement queryMethod = (QueryMethodIndexElement) queryMethods.get(0);
assertEquals("findByLastNameAndStatus(String, Status) : List<Customer>", queryMethod.getMethodName());
}
@Test
@@ -115,7 +130,7 @@ public class DataRepositoryIndexElementsTest {
assertEquals(1, queryMethods.size());
QueryMethodIndexElement queryMethod = (QueryMethodIndexElement) queryMethods.get(0);
assertEquals("findPetTypes", queryMethod.getMethodName());
assertEquals("findPetTypes() : List<Object>", queryMethod.getMethodName());
assertEquals("SELECT ptype FROM PetType ptype ORDER BY ptype.name", queryMethod.getQueryString());
}
@@ -140,11 +155,11 @@ public class DataRepositoryIndexElementsTest {
assertEquals(2, queryMethods.size());
QueryMethodIndexElement queryMethod = (QueryMethodIndexElement) queryMethods.get(0);
assertEquals("findConcretePetTypes", queryMethod.getMethodName());
assertEquals("findConcretePetTypes() : List<Object>", queryMethod.getMethodName());
assertEquals("CONCRETE REPO SELECT STATEMENT", queryMethod.getQueryString());
QueryMethodIndexElement parentQueryMethod = (QueryMethodIndexElement) queryMethods.get(1);
assertEquals("findParentPetTypes", parentQueryMethod.getMethodName());
assertEquals("findParentPetTypes() : List<Object>", parentQueryMethod.getMethodName());
assertEquals("PARENT REPO INTERFACE QUERY STATEMENT", parentQueryMethod.getQueryString());
}

View File

@@ -92,7 +92,7 @@ public class DataRepositorySymbolProviderTest {
DocumentSymbol documentSymbol = symbols.get(0);
List<DocumentSymbol> children = documentSymbol.getChildren();
DocumentSymbol childSymbol = children.get(0);
assertEquals("findByLastName", childSymbol.getName());
assertEquals("findByLastName(String) : List<Customer>", childSymbol.getName());
assertEquals(1, children.size());
}
@@ -107,7 +107,7 @@ public class DataRepositorySymbolProviderTest {
DocumentSymbol documentSymbol = symbols.get(0);
List<DocumentSymbol> children = documentSymbol.getChildren();
DocumentSymbol queryMethodSymbol = children.get(0);
assertEquals("findPetTypes", queryMethodSymbol.getName());
assertEquals("findPetTypes() : List<Object>", queryMethodSymbol.getName());
assertEquals(1, children.size());
List<DocumentSymbol> queryChildren = queryMethodSymbol.getChildren();

View File

@@ -0,0 +1,11 @@
package org.test;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface CustomerRepositoryWithTwoParamsMethod extends CrudRepository<Customer, Long> {
List<Customer> findByLastNameAndStatus(String lastName, Status status);
}