Skip to content

Commit 094304b

Browse files
committed
GH-1494: include prefix in config property names
Fixes GH-1494
1 parent 1b30b47 commit 094304b

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Collection;
1515
import java.util.HashSet;
1616
import java.util.List;
17+
import java.util.Optional;
1718
import java.util.Set;
1819
import java.util.stream.Collectors;
1920
import java.util.stream.Stream;
@@ -41,6 +42,7 @@
4142
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
4243
import org.springframework.ide.vscode.boot.java.utils.CachedSymbol;
4344
import org.springframework.ide.vscode.boot.java.utils.SpringIndexerJavaContext;
45+
import org.springframework.ide.vscode.commons.protocol.spring.AnnotationAttributeValue;
4446
import org.springframework.ide.vscode.commons.protocol.spring.AnnotationMetadata;
4547
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
4648
import org.springframework.ide.vscode.commons.protocol.spring.InjectionPoint;
@@ -84,7 +86,7 @@ protected void createSymbolForType(AbstractTypeDeclaration type, Annotation node
8486
if (!isComponentAnnotated) {
8587
String beanName = BeanUtils.getBeanNameFromType(type.getName().getFullyQualifiedName());
8688

87-
Location location = new Location(doc.getUri(), doc.toRange(type.getStartPosition(), type.getLength()));
89+
Location location = new Location(doc.getUri(), doc.toRange(node.getStartPosition(), node.getLength()));
8890

8991
WorkspaceSymbol symbol = new WorkspaceSymbol(
9092
ComponentSymbolProvider.beanLabel("+", annotationTypeName, metaAnnotationNames, beanName, typeBinding.getName()), SymbolKind.Interface,
@@ -107,7 +109,7 @@ protected void createSymbolForType(AbstractTypeDeclaration type, Annotation node
107109
.toArray(AnnotationMetadata[]::new);
108110

109111
Bean beanDefinition = new Bean(beanName, typeBinding.getQualifiedName(), location, injectionPoints, supertypes, annotations, isConfiguration, symbol.getName());
110-
112+
111113
indexConfigurationProperties(beanDefinition, type, context, doc);
112114

113115
context.getGeneratedSymbols().add(new CachedSymbol(context.getDocURI(), context.getLastModified(), symbol));
@@ -116,15 +118,38 @@ protected void createSymbolForType(AbstractTypeDeclaration type, Annotation node
116118
}
117119

118120
public static void indexConfigurationProperties(Bean beanDefinition, AbstractTypeDeclaration abstractType, SpringIndexerJavaContext context, TextDocument doc) {
121+
Optional<String> prefixValue = Arrays.stream(beanDefinition.getAnnotations())
122+
.filter(annotation -> Annotations.CONFIGURATION_PROPERTIES.equals(annotation.getAnnotationType()))
123+
.flatMap(annotation -> Arrays.stream(getPrefix(annotation)))
124+
.map(attribute -> attribute.getName())
125+
.findFirst();
126+
127+
String prefix = "";
128+
if (prefixValue.isPresent()) {
129+
prefix = prefixValue.get() + ".";
130+
}
131+
119132
if (abstractType instanceof TypeDeclaration type) {
120-
indexConfigurationPropertiesForType(beanDefinition, type, context, doc);
133+
indexConfigurationPropertiesForType(beanDefinition, type, context, doc, prefix);
121134
}
122135
else if (abstractType instanceof RecordDeclaration record) {
123-
indexConfigurationPropertiesForRecord(beanDefinition, record, context, doc);
136+
indexConfigurationPropertiesForRecord(beanDefinition, record, context, doc, prefix);
137+
}
138+
}
139+
140+
private static AnnotationAttributeValue[] getPrefix(AnnotationMetadata annotation) {
141+
if (annotation.getAttributes().containsKey("prefix")) {
142+
return annotation.getAttributes().get("prefix");
143+
}
144+
else if (annotation.getAttributes().containsKey("value")) {
145+
return annotation.getAttributes().get("value");
146+
}
147+
else {
148+
return new AnnotationAttributeValue[0];
124149
}
125150
}
126151

127-
public static void indexConfigurationPropertiesForType(Bean beanDefinition, TypeDeclaration type, SpringIndexerJavaContext context, TextDocument doc) {
152+
public static void indexConfigurationPropertiesForType(Bean beanDefinition, TypeDeclaration type, SpringIndexerJavaContext context, TextDocument doc, String prefix) {
128153

129154
FieldDeclaration[] fields = type.getFields();
130155
if (fields != null) {
@@ -143,7 +168,7 @@ public static void indexConfigurationPropertiesForType(Bean beanDefinition, Type
143168

144169
DocumentRegion nodeRegion = ASTUtils.nodeRegion(doc, field);
145170
Range range = doc.toRange(nodeRegion);
146-
ConfigPropertyIndexElement configPropElement = new ConfigPropertyIndexElement(name.getFullyQualifiedName(), fieldType.resolveBinding().getQualifiedName(), range);
171+
ConfigPropertyIndexElement configPropElement = new ConfigPropertyIndexElement(prefix + name.getFullyQualifiedName(), fieldType.resolveBinding().getQualifiedName(), range);
147172

148173
beanDefinition.addChild(configPropElement);
149174
}
@@ -157,7 +182,7 @@ public static void indexConfigurationPropertiesForType(Bean beanDefinition, Type
157182

158183
}
159184

160-
public static void indexConfigurationPropertiesForRecord(Bean beanDefinition, RecordDeclaration record, SpringIndexerJavaContext context, TextDocument doc) {
185+
public static void indexConfigurationPropertiesForRecord(Bean beanDefinition, RecordDeclaration record, SpringIndexerJavaContext context, TextDocument doc, String prefix) {
161186

162187
@SuppressWarnings("unchecked")
163188
List<SingleVariableDeclaration> fields = record.recordComponents();
@@ -173,7 +198,7 @@ public static void indexConfigurationPropertiesForRecord(Bean beanDefinition, Re
173198

174199
DocumentRegion nodeRegion = ASTUtils.nodeRegion(doc, field);
175200
Range range = doc.toRange(nodeRegion);
176-
ConfigPropertyIndexElement configPropElement = new ConfigPropertyIndexElement(name.getFullyQualifiedName(), fieldType.resolveBinding().getQualifiedName(), range);
201+
ConfigPropertyIndexElement configPropElement = new ConfigPropertyIndexElement(prefix + name.getFullyQualifiedName(), fieldType.resolveBinding().getQualifiedName(), range);
177202

178203
beanDefinition.addChild(configPropElement);
179204
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void testSimpleConfigPropertiesClass() throws Exception {
8080
assertEquals(1, children.size());
8181

8282
ConfigPropertyIndexElement configPropElement = (ConfigPropertyIndexElement) children.get(0);
83-
assertEquals("simpleConfigProp", configPropElement.getName());
83+
assertEquals("com.example.config.prefix.simple.simpleConfigProp", configPropElement.getName());
8484
assertEquals("java.lang.String", configPropElement.getType());
8585
}
8686

@@ -98,7 +98,7 @@ void testSimpleConfigPropertiesClassWithAdditionalConfigurationAnnotation() thro
9898
assertEquals(1, children.size());
9999

100100
ConfigPropertyIndexElement configPropElement = (ConfigPropertyIndexElement) children.get(0);
101-
assertEquals("simpleConfigProp", configPropElement.getName());
101+
assertEquals("com.example.config.prefix.simple2.simpleConfigProp", configPropElement.getName());
102102
assertEquals("java.lang.String", configPropElement.getType());
103103
}
104104

@@ -116,11 +116,11 @@ void testSimpleConfigPropertiesRecord() throws Exception {
116116
assertEquals(2, children.size());
117117

118118
ConfigPropertyIndexElement configPropElement1 = (ConfigPropertyIndexElement) children.get(0);
119-
assertEquals("name", configPropElement1.getName());
119+
assertEquals("com.example.config.record.prefix.name", configPropElement1.getName());
120120
assertEquals("java.lang.String", configPropElement1.getType());
121121

122122
ConfigPropertyIndexElement configPropElement2 = (ConfigPropertyIndexElement) children.get(1);
123-
assertEquals("duration", configPropElement2.getName());
123+
assertEquals("com.example.config.record.prefix.duration", configPropElement2.getName());
124124
assertEquals("int", configPropElement2.getType());
125125
}
126126

@@ -138,11 +138,11 @@ void testSimpleConfigPropertiesRecordAndConfigurationAnnotation() throws Excepti
138138
assertEquals(2, children.size());
139139

140140
ConfigPropertyIndexElement configPropElement1 = (ConfigPropertyIndexElement) children.get(0);
141-
assertEquals("name", configPropElement1.getName());
141+
assertEquals("com.example.config.record.prefix2.name", configPropElement1.getName());
142142
assertEquals("java.lang.String", configPropElement1.getType());
143143

144144
ConfigPropertyIndexElement configPropElement2 = (ConfigPropertyIndexElement) children.get(1);
145-
assertEquals("duration", configPropElement2.getName());
145+
assertEquals("com.example.config.record.prefix2.duration", configPropElement2.getName());
146146
assertEquals("int", configPropElement2.getType());
147147

148148
}

headless-services/spring-boot-language-server/src/test/resources/test-projects/test-configuration-properties-indexing/src/main/java/com/example/configproperties/ConfigurationPropertiesExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.springframework.boot.context.properties.ConfigurationProperties;
44

5-
@ConfigurationProperties(prefix = "com.example.config.prefix.simple")
5+
@ConfigurationProperties("com.example.config.prefix.simple")
66
public class ConfigurationPropertiesExample {
77

88
private String simpleConfigProp = "default config value";

headless-services/spring-boot-language-server/src/test/resources/test-projects/test-configuration-properties-indexing/src/main/java/com/example/configproperties/ConfigurationPropertiesExampleWithConfigurationAnnotation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.springframework.context.annotation.Configuration;
55

66
@Configuration
7-
@ConfigurationProperties(prefix = "com.example.config.prefix.simple2")
7+
@ConfigurationProperties(value = "com.example.config.prefix.simple2")
88
public class ConfigurationPropertiesExampleWithConfigurationAnnotation {
99

1010
private String simpleConfigProp = "default config value";

0 commit comments

Comments
 (0)