-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1305: find all references for property keys now supported on Condi…
…tionalOnProperty annotation
- Loading branch information
1 parent
790629e
commit 4258612
Showing
6 changed files
with
284 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...erver/src/main/java/org/springframework/ide/vscode/boot/java/value/PropertyExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Broadcom | ||
* 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 | ||
* https://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Broadcom - initial API and implementation | ||
*******************************************************************************/ | ||
package org.springframework.ide.vscode.boot.java.value; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.eclipse.jdt.core.dom.ASTNode; | ||
import org.eclipse.jdt.core.dom.Annotation; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.dom.Expression; | ||
import org.eclipse.jdt.core.dom.IAnnotationBinding; | ||
import org.eclipse.jdt.core.dom.MemberValuePair; | ||
import org.eclipse.jdt.core.dom.NormalAnnotation; | ||
import org.eclipse.jdt.core.dom.SingleMemberAnnotation; | ||
import org.eclipse.jdt.core.dom.StringLiteral; | ||
import org.eclipse.lsp4j.LocationLink; | ||
import org.springframework.ide.vscode.boot.java.Annotations; | ||
import org.springframework.ide.vscode.commons.java.IJavaProject; | ||
|
||
public class PropertyExtractor { | ||
|
||
private static final String PARAM_VALUE = "value"; | ||
private static final String PARAM_NAME = "name"; | ||
private static final String PARAM_PREFIX = "prefix"; | ||
|
||
private static interface PropertyKeyExtractor { | ||
String extract(Annotation annotation, MemberValuePair memberValuePair, StringLiteral stringLiteral); | ||
} | ||
|
||
private final Map<String, PropertyKeyExtractor> propertyKeyExtractors; | ||
|
||
public PropertyExtractor() { | ||
propertyKeyExtractors = Map.of( | ||
|
||
Annotations.VALUE, (annotation, memberValuePair, stringLiteral) -> { | ||
if (annotation.isSingleMemberAnnotation()) { | ||
return extractPropertyKey(stringLiteral.getLiteralValue()); | ||
} else if (annotation.isNormalAnnotation() && PARAM_VALUE.equals(memberValuePair.getName().getIdentifier())) { | ||
return extractPropertyKey(stringLiteral.getLiteralValue()); | ||
} | ||
return null; | ||
}, | ||
|
||
Annotations.CONDITIONAL_ON_PROPERTY, (annotation, memberValuePair, stringLiteral) -> { | ||
if (annotation.isSingleMemberAnnotation()) { | ||
return stringLiteral.getLiteralValue(); | ||
} else if (annotation.isNormalAnnotation()) { | ||
switch (memberValuePair.getName().getIdentifier()) { | ||
case PARAM_VALUE: | ||
return stringLiteral.getLiteralValue(); | ||
case PARAM_NAME: | ||
String prefix = extractAnnotationParameter(annotation, PARAM_PREFIX); | ||
String name = stringLiteral.getLiteralValue(); | ||
return prefix != null && !prefix.isBlank() ? prefix + "." + name : name; | ||
} | ||
} | ||
return null; | ||
} | ||
); | ||
} | ||
|
||
public String extractPropertyKey(StringLiteral valueNode) { | ||
|
||
ASTNode parent = valueNode.getParent(); | ||
|
||
if (parent instanceof Annotation) { | ||
|
||
Annotation a = (Annotation) parent; | ||
IAnnotationBinding binding = a.resolveAnnotationBinding(); | ||
if (binding != null && binding.getAnnotationType() != null) { | ||
PropertyKeyExtractor propertyExtractor = propertyKeyExtractors.get(binding.getAnnotationType().getQualifiedName()); | ||
if (propertyExtractor != null) { | ||
return propertyExtractor.extract(a, null, valueNode); | ||
} | ||
} | ||
|
||
} else if (parent instanceof MemberValuePair && parent.getParent() instanceof Annotation) { | ||
|
||
MemberValuePair pair = (MemberValuePair) parent; | ||
Annotation a = (Annotation) parent.getParent(); | ||
IAnnotationBinding binding = a.resolveAnnotationBinding(); | ||
if (binding != null && binding.getAnnotationType() != null) { | ||
PropertyKeyExtractor propertyExtractor = propertyKeyExtractors.get(binding.getAnnotationType().getQualifiedName()); | ||
if (propertyExtractor != null) { | ||
return propertyExtractor.extract(a, pair, valueNode); | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private String extractPropertyKey(String s) { | ||
if (s.length() > 3 && (s.startsWith("${") || s.startsWith("#{")) && s.endsWith("}")) { | ||
return s.substring(2, s.length() - 1); | ||
} | ||
return null; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static String extractAnnotationParameter(Annotation a, String param) { | ||
Expression value = null; | ||
if (a.isSingleMemberAnnotation() && PARAM_VALUE.equals(param)) { | ||
value = ((SingleMemberAnnotation) a).getValue(); | ||
} else if (a.isNormalAnnotation()) { | ||
for (MemberValuePair pair : (List<MemberValuePair>) ((NormalAnnotation) a).values()) { | ||
if (param.equals(pair.getName().getIdentifier())) { | ||
value = pair.getValue(); | ||
break; | ||
} | ||
} | ||
} | ||
if (value instanceof StringLiteral) { | ||
return ((StringLiteral) value).getLiteralValue(); | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.