Skip to content

Commit 02ed5ae

Browse files
committed
remove redundant parse all spel expressions logic
1 parent 988c0d9 commit 02ed5ae

File tree

2 files changed

+24
-51
lines changed

2 files changed

+24
-51
lines changed

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/spel/SpelDefinitionProvider.java

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -96,64 +96,41 @@ public List<LocationLink> getDefinitions(CancelChecker cancelToken, IJavaProject
9696
if (n instanceof StringLiteral) {
9797
StringLiteral valueNode = (StringLiteral) n;
9898
ASTNode parent = ASTUtils.getNearestAnnotationParent(valueNode);
99-
10099
if (parent != null && parent instanceof Annotation) {
100+
List<LocationLink> locationLink = new ArrayList<>();
101101
Annotation a = (Annotation) parent;
102102
IAnnotationBinding binding = a.resolveAnnotationBinding();
103103
if (binding != null && binding.getAnnotationType() != null
104104
&& Annotations.VALUE.equals(binding.getAnnotationType().getQualifiedName())) {
105-
return parseSpelAndFetchLocation(cancelToken, project, cu, offset);
105+
Arrays.stream(spelExtractors).map(e -> {
106+
if (a instanceof SingleMemberAnnotation)
107+
return e.getSpelRegion((SingleMemberAnnotation) a);
108+
else if (a instanceof NormalAnnotation)
109+
return e.getSpelRegion((NormalAnnotation) a);
110+
return Optional.<Snippet>empty();
111+
}).filter(o -> o.isPresent()).map(o -> o.get())
112+
.filter(snippet -> {
113+
int tokenEndIndex = snippet.offset() + snippet.text().length();
114+
return snippet.offset() <= (offset) && (offset) <= tokenEndIndex;
115+
}).forEach(snippet -> {
116+
List<TokenData> beanReferenceTokens = computeTokens(snippet, offset);
117+
if (beanReferenceTokens != null && beanReferenceTokens.size() > 0) {
118+
locationLink.addAll(findLocationLinksForBeanRef(project, offset, beanReferenceTokens));
119+
}
120+
121+
Optional<Tuple2<String, String>> result = parseAndExtractMethodClassPairFromSpel(snippet,
122+
offset);
123+
result.ifPresent(tuple -> {
124+
locationLink.addAll(findLocationLinksForMethodRef(tuple.getT1(), tuple.getT2(), project));
125+
});
126+
});
127+
return locationLink;
106128
}
107129
}
108130
}
109131
return Collections.emptyList();
110132
}
111133

112-
private List<LocationLink> parseSpelAndFetchLocation(CancelChecker cancelToken, IJavaProject project,
113-
CompilationUnit cu, int offset) {
114-
List<LocationLink> locationLink = new ArrayList<>();
115-
cu.accept(new ASTVisitor() {
116-
@Override
117-
public boolean visit(SingleMemberAnnotation node) {
118-
Arrays.stream(spelExtractors).map(e -> e.getSpelRegion(node)).filter(o -> o.isPresent())
119-
.map(o -> o.get()).forEach(snippet -> {
120-
List<TokenData> beanReferenceTokens = computeTokens(snippet, offset);
121-
if (beanReferenceTokens != null && beanReferenceTokens.size() > 0) {
122-
locationLink.addAll(findLocationLinksForBeanRef(project, offset, beanReferenceTokens));
123-
}
124-
125-
Optional<Tuple2<String, String>> result = parseAndExtractMethodClassPairFromSpel(snippet,
126-
offset);
127-
result.ifPresent(tuple -> {
128-
locationLink.addAll(findLocationLinksForMethodRef(tuple.getT1(), tuple.getT2(), project));
129-
});
130-
});
131-
return super.visit(node);
132-
}
133-
134-
@Override
135-
public boolean visit(NormalAnnotation node) {
136-
Arrays.stream(spelExtractors).map(e -> e.getSpelRegion(node)).filter(o -> o.isPresent())
137-
.map(o -> o.get()).forEach(snippet -> {
138-
List<TokenData> beanReferenceTokens = computeTokens(snippet, offset);
139-
if (beanReferenceTokens != null && beanReferenceTokens.size() > 0) {
140-
locationLink.addAll(findLocationLinksForBeanRef(project, offset, beanReferenceTokens));
141-
}
142-
parseAndExtractMethodClassPairFromSpel(snippet, offset);
143-
Optional<Tuple2<String, String>> result = parseAndExtractMethodClassPairFromSpel(snippet,
144-
offset);
145-
result.ifPresent(tuple -> {
146-
locationLink.addAll(findLocationLinksForMethodRef(tuple.getT1(), tuple.getT2(), project));
147-
});
148-
});
149-
150-
return super.visit(node);
151-
}
152-
153-
});
154-
return locationLink;
155-
}
156-
157134
private List<LocationLink> findLocationLinksForBeanRef(IJavaProject project, int offset,
158135
List<TokenData> beanReferenceTokens) {
159136
return beanReferenceTokens.stream().flatMap(t -> findBeansWithName(project, t.text()).stream())
@@ -168,7 +145,6 @@ private List<LocationLink> findLocationLinksForMethodRef(String methodName, Stri
168145
String classFqName = className.substring(2, className.length() - 1);
169146
Optional<URL> sourceUrl = SourceLinks.source(project, classFqName);
170147
if (sourceUrl.isPresent()) {
171-
172148
docUri = sourceUrl.get().toURI();
173149
}
174150
} else if (className.startsWith("@")) {

headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/spel/SpelDefinitionProviderTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,9 @@ public static String concat(String str1, String str2) {
240240
new Range(new Position(7, 23), new Position(7, 37)), new Range(new Position(7, 23), new Position(7, 37)), null);
241241
LocationLink expectedLocation2 = new LocationLink(expectedDefinitionUriSpelClass,
242242
new Range(new Position(37, 22), new Position(37, 28)), new Range(new Position(37, 22), new Position(37, 28)), null);
243-
// LocationLink expectedLocation3 = new LocationLink(expectedDefinitionUriSpelClass,
244-
// new Range(new Position(33, 22), new Position(33, 28)), new Range(new Position(33, 22), new Position(33, 28)), null);
245243

246244
editor.assertLinkTargets("isValidVersion", List.of(expectedLocation1));
247245
editor.assertLinkTargets("concat", List.of(expectedLocation2));
248-
// editor.assertLinkTargets("toUpperCase", List.of(expectedLocation3));
249246
}
250247

251248
}

0 commit comments

Comments
 (0)