Skip to content

Commit

Permalink
GH-1434: wrapped all remaining calls to getLiteralValue via ASTUtils …
Browse files Browse the repository at this point in the history
…to avoid threading issues

Fixes GH-1434
  • Loading branch information
martinlippert committed Dec 9, 2024
1 parent c4af955 commit 48dbf77
Show file tree
Hide file tree
Showing 21 changed files with 50 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.springframework.ide.vscode.boot.java.handlers.CompletionProvider;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits;
import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal;
Expand Down Expand Up @@ -235,8 +236,7 @@ protected Set<String> alreadyMentionedValues(ASTNode node) {
List<?> expressions = arrayNode.expressions();
for (Object expression : expressions) {
if (expression instanceof StringLiteral) {
StringLiteral stringExr = (StringLiteral) expression;
String value = stringExr.getLiteralValue();
String value = ASTUtils.getLiteralValue((StringLiteral) expression);
result.add(value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public List<LocationLink> getDefinitions(CancelChecker cancelToken, IJavaProject
Annotation a = (Annotation) parent;
IAnnotationBinding binding = a.resolveAnnotationBinding();
if (binding != null && binding.getAnnotationType() != null && Annotations.DEPENDS_ON.equals(binding.getAnnotationType().getQualifiedName())) {
String beanName = valueNode.getLiteralValue();
String beanName = ASTUtils.getLiteralValue(valueNode);

if (beanName != null && beanName.length() > 0) {
return findBeansWithName(project, beanName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public List<LocationLink> getDefinitions(CancelChecker cancelToken, IJavaProject
Annotation a = (Annotation) parent;
IAnnotationBinding binding = a.resolveAnnotationBinding();
if (binding != null && binding.getAnnotationType() != null && Annotations.NAMED_ANNOTATIONS.contains(binding.getAnnotationType().getQualifiedName())) {
String beanName = valueNode.getLiteralValue();
String beanName = ASTUtils.getLiteralValue(valueNode);

if (beanName != null && beanName.length() > 0) {
return findBeansWithName(project, beanName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.handlers.ReferenceProvider;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;

Expand All @@ -50,14 +51,14 @@ public List<? extends Location> provideReferences(CancelChecker cancelToken, IJa
// case: @Value("prefix<*>")
if (node instanceof StringLiteral && node.getParent() instanceof Annotation) {
if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
return provideReferences(project, ((StringLiteral) node).getLiteralValue());
return provideReferences(project, ASTUtils.getLiteralValue((StringLiteral) node));
}
}
// case: @Value(value="prefix<*>")
else if (node instanceof StringLiteral && node.getParent() instanceof MemberValuePair
&& "value".equals(((MemberValuePair)node.getParent()).getName().toString())) {
if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
return provideReferences(project, ((StringLiteral) node).getLiteralValue());
return provideReferences(project, ASTUtils.getLiteralValue((StringLiteral) node));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.handlers.ReferenceProvider;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;

Expand All @@ -48,20 +49,20 @@ public List<? extends Location> provideReferences(CancelChecker cancelToken, IJa
// case: @Value("prefix<*>")
if (node instanceof StringLiteral && node.getParent() instanceof Annotation) {
if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
return provideReferences(project, ((StringLiteral) node).getLiteralValue());
return provideReferences(project, ASTUtils.getLiteralValue((StringLiteral) node));
}
}
// case: @Value(value="prefix<*>")
else if (node instanceof StringLiteral && node.getParent() instanceof MemberValuePair
&& "value".equals(((MemberValuePair)node.getParent()).getName().toString())) {
if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
return provideReferences(project, ((StringLiteral) node).getLiteralValue());
return provideReferences(project, ASTUtils.getLiteralValue((StringLiteral) node));
}
}
// case: @Qualifier({"prefix<*>"})
else if (node instanceof StringLiteral && node.getParent() instanceof ArrayInitializer) {
if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
return provideReferences(project, ((StringLiteral) node).getLiteralValue());
return provideReferences(project, ASTUtils.getLiteralValue((StringLiteral) node));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public List<LocationLink> getDefinitions(CancelChecker cancelToken, IJavaProject
Annotation a = (Annotation) parent;
IAnnotationBinding binding = a.resolveAnnotationBinding();
if (binding != null && binding.getAnnotationType() != null && Annotations.QUALIFIER.equals(binding.getAnnotationType().getQualifiedName())) {
String beanName = valueNode.getLiteralValue();
String beanName = ASTUtils.getLiteralValue(valueNode);

if (beanName != null && beanName.length() > 0) {
return findBeansWithName(project, beanName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.handlers.ReferenceProvider;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;

Expand All @@ -47,14 +48,14 @@ public List<? extends Location> provideReferences(CancelChecker cancelToken, IJa
// case: @Value("prefix<*>")
if (node instanceof StringLiteral && node.getParent() instanceof Annotation) {
if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
return provideReferences(project, ((StringLiteral) node).getLiteralValue());
return provideReferences(project, ASTUtils.getLiteralValue((StringLiteral) node));
}
}
// case: @Value(value="prefix<*>")
else if (node instanceof StringLiteral && node.getParent() instanceof MemberValuePair
&& "value".equals(((MemberValuePair)node.getParent()).getName().toString())) {
if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
return provideReferences(project, ((StringLiteral) node).getLiteralValue());
return provideReferences(project, ASTUtils.getLiteralValue((StringLiteral) node));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public List<LocationLink> getDefinitions(CancelChecker cancelToken, IJavaProject
if (binding != null && binding.getAnnotationType() != null
&& (Annotations.RESOURCE_JAVAX.equals(binding.getAnnotationType().getQualifiedName())
|| Annotations.RESOURCE_JAKARTA.equals(binding.getAnnotationType().getQualifiedName()))) {
String beanName = valueNode.getLiteralValue();
String beanName = ASTUtils.getLiteralValue(valueNode);

if (beanName != null && beanName.length() > 0) {
return findBeansWithName(project, beanName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public List<LocationLink> getDefinitions(CancelChecker cancelToken, IJavaProject
}

private List<LocationLink> getDefinitions(IJavaProject project, StringLiteral valueNode) {
String value = valueNode.getLiteralValue();
String value = ASTUtils.getLiteralValue(valueNode);

if (value != null && value.length() > 0) {
return getDefinitionsForValue(project, valueNode, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private String getPrefixAttributeValue(ASTNode node) {
if (valuePairName != null && "prefix".equals(valuePairName)
&& valuePair.getValue() != null && valuePair.getValue() instanceof StringLiteral) {
StringLiteral prefixLiteral = (StringLiteral) valuePair.getValue();
String valuePairValue = prefixLiteral.getLiteralValue();
String valuePairValue = ASTUtils.getLiteralValue(prefixLiteral);
return valuePairValue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.springframework.ide.vscode.boot.java.IJavaDefinitionProvider;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.commons.java.IClasspathUtil;
import org.springframework.ide.vscode.commons.java.IJavaProject;

Expand All @@ -36,7 +37,7 @@ public List<LocationLink> getDefinitions(CancelChecker cancelToken, IJavaProject
if (n instanceof StringLiteral) {
StringLiteral valueNode = (StringLiteral) n;

String literalValue = valueNode.getLiteralValue();
String literalValue = ASTUtils.getLiteralValue(valueNode);
if (literalValue != null) {
if (literalValue.startsWith("classpath")) {
return getDefinitionForClasspathResource(project, cu, valueNode, literalValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProposal;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeProposal;
import org.springframework.ide.vscode.boot.java.handlers.CompletionProvider;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.commons.java.IClasspathUtil;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits;
Expand Down Expand Up @@ -143,7 +144,8 @@ private void computeProposalsForStringLiteral(IJavaProject project, StringLitera
int startOffset = offset - prefix.length();
int endOffset = offset;

String unfilteredPrefix = node.getLiteralValue().substring(0, offset - (node.getStartPosition() + 1));
String literalValue = ASTUtils.getLiteralValue(node);
String unfilteredPrefix = literalValue.substring(0, offset - (node.getStartPosition() + 1));
addClasspathResourceProposals(project, doc, startOffset, endOffset, unfilteredPrefix, false, completions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.embedded.lang.EmbeddedLangAstUtils;
import org.springframework.ide.vscode.boot.java.embedded.lang.EmbeddedLanguageSnippet;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;

public class JdtCronVisitorUtils {

Expand Down Expand Up @@ -48,7 +49,7 @@ public static EmbeddedLanguageSnippet extractCron(NormalAnnotation node) {
private static boolean isCronExpression(Expression e) {
String value = null;
if (e instanceof StringLiteral sl) {
value = sl.getLiteralValue();
value = ASTUtils.getLiteralValue(sl);
} else if (e instanceof TextBlock tb) {
value = tb.getLiteralValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.springframework.ide.vscode.boot.java.embedded.lang.EmbeddedLanguageSnippet;
import org.springframework.ide.vscode.boot.java.spel.AnnotationParamSpelExtractor;
import org.springframework.ide.vscode.boot.java.spel.SpelSemanticTokens;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.java.SpringProjectUtil;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
Expand Down Expand Up @@ -298,7 +299,7 @@ private String extractPointcutReference(org.eclipse.jdt.core.dom.Expression expr
} else if (expression instanceof SimpleName) {
return ((SimpleName) expression).getIdentifier();
} else if (expression instanceof StringLiteral) {
String literalValue = ((StringLiteral) expression).getLiteralValue();
String literalValue = ASTUtils.getLiteralValue((StringLiteral) expression);
StringBuilder pointcuts = new StringBuilder();
for (Map.Entry<String, String> entry : pointcutMap.entrySet()) {
if (literalValue.contains(entry.getKey())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.jdt.core.dom.StringLiteral;
import org.openrewrite.marker.Range;
import org.springframework.ide.vscode.boot.java.Boot2JavaProblemType;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixRegistry;
import org.springframework.ide.vscode.commons.languageserver.reconcile.IProblemCollector;
Expand Down Expand Up @@ -141,7 +142,7 @@ private static String getAnnotationParameter(Annotation annotation) {
}

if (value instanceof StringLiteral) {
return ((StringLiteral) value).getLiteralValue();
return ASTUtils.getLiteralValue((StringLiteral) value);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Pivotal, Inc.
* Copyright (c) 2018, 2024 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -19,6 +19,7 @@
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.lsp4j.Range;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.commons.util.BadLocationException;
import org.springframework.ide.vscode.commons.util.text.TextDocument;

Expand Down Expand Up @@ -57,7 +58,7 @@ public boolean visit(MethodInvocation node) {
StringLiteral stringLiteral = WebfluxUtils.extractStringLiteralArgument(node);
if (stringLiteral != null) {
Range range = doc.toRange(stringLiteral.getStartPosition(), stringLiteral.getLength());
path.add(new WebfluxRouteElement(stringLiteral.getLiteralValue(), range));
path.add(new WebfluxRouteElement(ASTUtils.getLiteralValue(stringLiteral), range));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018, 2023 Pivotal, Inc.
* Copyright (c) 2018, 2024 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -35,6 +35,7 @@
import org.springframework.ide.vscode.boot.java.handlers.AbstractSymbolProvider;
import org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation;
import org.springframework.ide.vscode.boot.java.handlers.SymbolAddOnInformation;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.boot.java.utils.CachedSymbol;
import org.springframework.ide.vscode.boot.java.utils.SpringIndexerJava.SCAN_PASS;
import org.springframework.ide.vscode.boot.java.utils.SpringIndexerJavaContext;
Expand Down Expand Up @@ -157,7 +158,7 @@ private WebfluxRouteElement[] extractPath(MethodInvocation routerInvocation, Tex
StringLiteral stringLiteral = WebfluxUtils.extractStringLiteralArgument(methodInvocation);
if (stringLiteral != null) {
Range range = doc.toRange(stringLiteral.getStartPosition(), stringLiteral.getLength());
return new WebfluxRouteElement(stringLiteral.getLiteralValue(), range);
return new WebfluxRouteElement(ASTUtils.getLiteralValue(stringLiteral), range);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2023 Pivotal, Inc.
* Copyright (c) 2017, 2024 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -647,7 +647,7 @@ private static AnnotationAttributeValue[] getAnnotationAttributeValues(Expressio
private static AnnotationAttributeValue convertExpressionInto(Expression expression, TextDocument doc) throws BadLocationException {
if (expression instanceof StringLiteral) {
StringLiteral stringLiteral = (StringLiteral) expression;
String literalValue = stringLiteral.getLiteralValue();
String literalValue = ASTUtils.getLiteralValue(stringLiteral);

DocumentRegion region = ASTUtils.nodeRegion(doc, stringLiteral);
Range range = doc.toRange(region);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;

public class PropertyExtractor {

Expand All @@ -40,27 +41,27 @@ public PropertyExtractor() {

Annotations.VALUE, (annotation, memberValuePair, stringLiteral) -> {
if (annotation.isSingleMemberAnnotation()) {
return extractPropertyKey(stringLiteral.getLiteralValue());
return extractPropertyKey(ASTUtils.getLiteralValue(stringLiteral));
} else if (annotation.isNormalAnnotation() && PARAM_VALUE.equals(memberValuePair.getName().getIdentifier())) {
return extractPropertyKey(stringLiteral.getLiteralValue());
return extractPropertyKey(ASTUtils.getLiteralValue(stringLiteral));
}
return null;
},

Annotations.CONDITIONAL_ON_PROPERTY, (annotation, memberValuePair, stringLiteral) -> {
if (annotation.isSingleMemberAnnotation()) {
return stringLiteral.getLiteralValue();
return ASTUtils.getLiteralValue(stringLiteral);
} else if (annotation.isNormalAnnotation()) {
switch (memberValuePair.getName().getIdentifier()) {
case PARAM_VALUE:
return stringLiteral.getLiteralValue();
return ASTUtils.getLiteralValue(stringLiteral);
case PARAM_NAME:
String prefix = extractAnnotationParameter(annotation, PARAM_PREFIX);
String name = stringLiteral.getLiteralValue();
String name = ASTUtils.getLiteralValue(stringLiteral);
return prefix != null && !prefix.isBlank() ? prefix + "." + name : name;
case PARAM_PREFIX:
name = extractAnnotationParameter(annotation, PARAM_NAME);
prefix = stringLiteral.getLiteralValue();
prefix = ASTUtils.getLiteralValue(stringLiteral);
return prefix != null && !prefix.isBlank() ? prefix + "." + name : name;
}
}
Expand Down Expand Up @@ -121,7 +122,7 @@ private static String extractAnnotationParameter(Annotation a, String param) {
}
}
if (value instanceof StringLiteral) {
return ((StringLiteral) value).getLiteralValue();
return ASTUtils.getLiteralValue((StringLiteral) value);
}
return null;
}
Expand Down
Loading

0 comments on commit 48dbf77

Please sign in to comment.