1818import java .util .Optional ;
1919
2020import org .eclipse .jdt .core .dom .ASTNode ;
21- import org .eclipse .jdt .core .dom .Annotation ;
2221import org .eclipse .jdt .core .dom .CompilationUnit ;
23- import org .eclipse .jdt .core .dom .Expression ;
24- import org .eclipse .jdt .core .dom .IAnnotationBinding ;
25- import org .eclipse .jdt .core .dom .MemberValuePair ;
26- import org .eclipse .jdt .core .dom .NormalAnnotation ;
27- import org .eclipse .jdt .core .dom .SingleMemberAnnotation ;
2822import org .eclipse .jdt .core .dom .StringLiteral ;
2923import org .eclipse .lsp4j .Location ;
3024import org .eclipse .lsp4j .LocationLink ;
3428import org .eclipse .lsp4j .jsonrpc .CancelChecker ;
3529import org .slf4j .Logger ;
3630import org .slf4j .LoggerFactory ;
37- import org .springframework .ide .vscode .boot .java .Annotations ;
3831import org .springframework .ide .vscode .boot .java .IJavaDefinitionProvider ;
3932import org .springframework .ide .vscode .boot .properties .BootPropertiesLanguageServerComponents ;
4033import org .springframework .ide .vscode .commons .java .IClasspathUtil ;
4740public class ValueDefinitionProvider implements IJavaDefinitionProvider {
4841
4942 private static final Logger log = LoggerFactory .getLogger (ValueDefinitionProvider .class );
43+ private final PropertyExtractor propertyExtractor ;
5044
51- private static final String PARAM_VALUE = "value" ;
52- private static final String PARAM_NAME = "name" ;
53- private static final String PARAM_PREFIX = "prefix" ;
45+ public ValueDefinitionProvider () {
46+ this . propertyExtractor = new PropertyExtractor () ;
47+ }
5448
55- private Map <String , PropertyKeyExtractor > annotationToPropertyKeyExtractor = Map .of (
56- Annotations .VALUE , (annotation , memberValuePair , stringLiteral ) -> {
57- if (annotation .isSingleMemberAnnotation ()) {
58- return extractPropertyKey (stringLiteral .getLiteralValue ());
59- } else if (annotation .isNormalAnnotation () && PARAM_VALUE .equals (memberValuePair .getName ().getIdentifier ())) {
60- return extractPropertyKey (stringLiteral .getLiteralValue ());
61- }
62- return null ;
63- },
64- Annotations .CONDITIONAL_ON_PROPERTY , (annotation , memberValuePair , stringLiteral ) -> {
65- if (annotation .isSingleMemberAnnotation ()) {
66- return stringLiteral .getLiteralValue ();
67- } else if (annotation .isNormalAnnotation ()) {
68- switch (memberValuePair .getName ().getIdentifier ()) {
69- case PARAM_VALUE :
70- return stringLiteral .getLiteralValue ();
71- case PARAM_NAME :
72- String prefix = extractAnnotationParameter (annotation , PARAM_PREFIX );
73- String name = stringLiteral .getLiteralValue ();
74- return prefix != null && !prefix .isBlank () ? prefix + "." + name : name ;
75- }
76- }
77- return null ;
78- }
79- );
80-
8149 @ Override
8250 public List <LocationLink > getDefinitions (CancelChecker cancelToken , IJavaProject project ,
8351 TextDocumentIdentifier docId , CompilationUnit cu , ASTNode n , int offset ) {
@@ -99,30 +67,7 @@ public List<LocationLink> getDefinitions(CancelChecker cancelToken, IJavaProject
9967 }
10068
10169 private List <LocationLink > getDefinitionForProperty (IJavaProject project , CompilationUnit cu , StringLiteral valueNode ) {
102- String propertyKey = null ;
103-
104- ASTNode parent = valueNode .getParent ();
105- if (parent instanceof Annotation ) {
106- Annotation a = (Annotation ) parent ;
107- IAnnotationBinding binding = a .resolveAnnotationBinding ();
108- if (binding != null && binding .getAnnotationType () != null ) {
109- PropertyKeyExtractor propertyExtractor = annotationToPropertyKeyExtractor .get (binding .getAnnotationType ().getQualifiedName ());
110- if (propertyExtractor != null ) {
111- propertyKey = propertyExtractor .extract (a , null , valueNode );
112- }
113- }
114- } else if (parent instanceof MemberValuePair
115- && parent .getParent () instanceof Annotation ) {
116- MemberValuePair pair = (MemberValuePair ) parent ;
117- Annotation a = (Annotation ) parent .getParent ();
118- IAnnotationBinding binding = a .resolveAnnotationBinding ();
119- if (binding != null && binding .getAnnotationType () != null ) {
120- PropertyKeyExtractor propertyExtractor = annotationToPropertyKeyExtractor .get (binding .getAnnotationType ().getQualifiedName ());
121- if (propertyExtractor != null ) {
122- propertyKey = propertyExtractor .extract (a , pair , valueNode );
123- }
124- }
125- }
70+ String propertyKey = propertyExtractor .extractPropertyKey (valueNode );
12671
12772 if (propertyKey != null ) {
12873 Builder <LocationLink > builder = ImmutableList .builder ();
@@ -217,36 +162,6 @@ private List<Location> findValueReferences(IJavaProject project, String property
217162 return links .build ();
218163 }
219164
220- @ SuppressWarnings ("unchecked" )
221- private static String extractAnnotationParameter (Annotation a , String param ) {
222- Expression value = null ;
223- if (a .isSingleMemberAnnotation () && PARAM_VALUE .equals (param )) {
224- value = ((SingleMemberAnnotation ) a ).getValue ();
225- } else if (a .isNormalAnnotation ()) {
226- for (MemberValuePair pair : (List <MemberValuePair >) ((NormalAnnotation ) a ).values ()) {
227- if (param .equals (pair .getName ().getIdentifier ())) {
228- value = pair .getValue ();
229- break ;
230- }
231- }
232- }
233- if (value instanceof StringLiteral ) {
234- return ((StringLiteral ) value ).getLiteralValue ();
235- }
236- return null ;
237- }
238-
239- private static String extractPropertyKey (String s ) {
240- if (s .length () > 3 && (s .startsWith ("${" ) || s .startsWith ("#{" )) && s .endsWith ("}" )) {
241- return s .substring (2 , s .length () - 1 );
242- }
243- return null ;
244- }
245-
246- private interface PropertyKeyExtractor {
247- String extract (Annotation annotation , MemberValuePair memberValuePair , StringLiteral stringLiteral );
248- }
249-
250165 private List <LocationLink > getDefinitionForClasspathResource (IJavaProject project , CompilationUnit cu , StringLiteral valueNode , String literalValue ) {
251166 literalValue = literalValue .substring ("classpath:" .length ());
252167
0 commit comments