Skip to content

Commit 5cded42

Browse files
committed
resolve constants for expressions when extracting annotation attribute values
1 parent 1e9e893 commit 5cded42

File tree

7 files changed

+65
-77
lines changed

7 files changed

+65
-77
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,9 @@ else if (expression instanceof TypeLiteral) {
669669
Range range = doc.toRange(region);
670670
Location location = new Location(doc.getUri(), range);
671671

672-
return new AnnotationAttributeValue(expression.toString(), location);
672+
Object constantExpressionValue = expression.resolveConstantExpressionValue();
673+
674+
return new AnnotationAttributeValue(constantExpressionValue != null ? constantExpressionValue.toString() : expression.toString(), location);
673675
}
674676
}
675677

@@ -698,7 +700,7 @@ public static MemberValuePairAndType getValuesFromValuePair(IMemberValuePairBind
698700

699701
ITypeBinding klass = varBinding.getDeclaringClass();
700702
if (klass != null) {
701-
result.dereferencedType= klass;
703+
result.dereferencedType = klass;
702704
}
703705

704706
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ void testSetterInjectionPointsFromConstructor() {
230230
assertEquals(1, point2Annotations[1].getAttributes().get("value").length);
231231
assertEquals("setter-injection-qualifier-on-param", point2Annotations[1].getAttributes().get("value")[0].getName());
232232
assertEquals(new Location(docUri, new Range(new Position(26, 33), new Position(26, 70))), point2Annotations[1].getAttributes().get("value")[0].getLocation());
233-
234233
}
235234

236235
@Test

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,13 @@ public Collection<? extends IJavaProject> all() {
132132
@BeforeEach
133133
public void setup() throws Exception {
134134
harness.intialize(null);
135-
prepareDefaultIndexData();
135+
136+
indexHarness.data("spring.prop1", "java.lang.String", null, null);
137+
indexHarness.data("data.prop2", "java.lang.String", null, null);
138+
indexHarness.data("else.prop3", "java.lang.String", null, null);
136139
}
137-
138-
@Test
140+
141+
@Test
139142
void testPrefixIdentification() {
140143
ValueCompletionProcessor processor = new ValueCompletionProcessor(projectFinder, null, null);
141144

@@ -579,12 +582,6 @@ void adHoc() throws Exception {
579582
}
580583

581584

582-
private void prepareDefaultIndexData() {
583-
indexHarness.data("spring.prop1", "java.lang.String", null, null);
584-
indexHarness.data("data.prop2", "java.lang.String", null, null);
585-
indexHarness.data("else.prop3", "java.lang.String", null, null);
586-
}
587-
588585
private void prepareCase(String selectedAnnotation, String annotationStatementBeforeTest) throws Exception {
589586
InputStream resource = this.getClass().getResourceAsStream("/test-projects/test-annotations/src/main/java/org/test/TestValueCompletion.java");
590587
String content = IOUtils.toString(resource, Charset.defaultCharset());

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

Lines changed: 25 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.concurrent.TimeUnit;
2222

2323
import org.eclipse.lsp4j.Location;
24+
import org.eclipse.lsp4j.Position;
25+
import org.eclipse.lsp4j.Range;
2426
import org.eclipse.lsp4j.TextDocumentIdentifier;
2527
import org.eclipse.lsp4j.WorkspaceFolder;
2628
import org.junit.jupiter.api.BeforeEach;
@@ -86,11 +88,7 @@ void testFindReferenceAtBeginningPropFile() throws Exception {
8688
Location location = locations.get(0);
8789

8890
URI docURI = file.toUri();
89-
assertEquals(docURI.toString(), location.getUri());
90-
assertEquals(0, location.getRange().getStart().getLine());
91-
assertEquals(0, location.getRange().getStart().getCharacter());
92-
assertEquals(0, location.getRange().getEnd().getLine());
93-
assertEquals(13, location.getRange().getEnd().getCharacter());
91+
assertEquals(new Location(docURI.toString(), new Range(new Position(0, 0), new Position(0, 13))), location);
9492
}
9593

9694
@Test
@@ -105,11 +103,7 @@ void testFindReferenceAtBeginningYMLFile() throws Exception {
105103
Location location = locations.get(0);
106104

107105
URI docURI = file.toUri();
108-
assertEquals(docURI.toString(), location.getUri());
109-
assertEquals(3, location.getRange().getStart().getLine());
110-
assertEquals(2, location.getRange().getStart().getCharacter());
111-
assertEquals(3, location.getRange().getEnd().getLine());
112-
assertEquals(10, location.getRange().getEnd().getCharacter());
106+
assertEquals(new Location(docURI.toString(), new Range(new Position(3, 2), new Position(3, 10))), location);
113107
}
114108

115109
@Test
@@ -123,35 +117,19 @@ void testFindReferenceWithinMultipleFiles() throws Exception {
123117

124118
Path file1 = resourceDir.resolve("mixed-multiple-files/application-dev.properties");
125119
Location location = getLocation(locations, file1.toUri());
126-
assertNotNull(location);
127-
assertEquals(1, location.getRange().getStart().getLine());
128-
assertEquals(0, location.getRange().getStart().getCharacter());
129-
assertEquals(1, location.getRange().getEnd().getLine());
130-
assertEquals(10, location.getRange().getEnd().getCharacter());
120+
assertEquals(new Location(file1.toUri().toString(), new Range(new Position(1, 0), new Position(1, 10))), location);
131121

132122
Path file2 = resourceDir.resolve("mixed-multiple-files/application.yml");
133123
location = getLocation(locations, file2.toUri());
134-
assertNotNull(location);
135-
assertEquals(3, location.getRange().getStart().getLine());
136-
assertEquals(2, location.getRange().getStart().getCharacter());
137-
assertEquals(3, location.getRange().getEnd().getLine());
138-
assertEquals(6, location.getRange().getEnd().getCharacter());
124+
assertEquals(new Location(file2.toUri().toString(), new Range(new Position(3, 2), new Position(3, 6))), location);
139125

140126
Path file3 = resourceDir.resolve("another-prop-folder/application.properties");
141127
location = getLocation(locations, file3.toUri());
142-
assertNotNull(location);
143-
assertEquals(1, location.getRange().getStart().getLine());
144-
assertEquals(0, location.getRange().getStart().getCharacter());
145-
assertEquals(1, location.getRange().getEnd().getLine());
146-
assertEquals(10, location.getRange().getEnd().getCharacter());
128+
assertEquals(new Location(file3.toUri().toString(), new Range(new Position(1, 0), new Position(1, 10))), location);
147129

148130
Path file4 = resourceDir.resolve("another-prop-folder/prod-application.properties");
149131
location = getLocation(locations, file4.toUri());
150-
assertNotNull(location);
151-
assertEquals(1, location.getRange().getStart().getLine());
152-
assertEquals(0, location.getRange().getStart().getCharacter());
153-
assertEquals(1, location.getRange().getEnd().getLine());
154-
assertEquals(10, location.getRange().getEnd().getCharacter());
132+
assertEquals(new Location(file4.toUri().toString(), new Range(new Position(1, 0), new Position(1, 10))), location);
155133
}
156134

157135
private Location getLocation(List<? extends Location> locations, URI docURI) {
@@ -193,10 +171,7 @@ public class TestDependsOnClass {
193171

194172
Location location = references.get(0);
195173
assertEquals(directory.toPath().resolve("src/main/java/application.properties").toUri().toString(), location.getUri());
196-
assertEquals(0, location.getRange().getStart().getLine());
197-
assertEquals(0, location.getRange().getStart().getCharacter());
198-
assertEquals(0, location.getRange().getEnd().getLine());
199-
assertEquals(7, location.getRange().getEnd().getCharacter());
174+
assertEquals(new Location(location.getUri(), new Range(new Position(0, 0), new Position(0, 7))), location);
200175
}
201176

202177
@Test
@@ -228,10 +203,7 @@ public class TestDependsOnClass {
228203

229204
Location location = references.get(0);
230205
assertEquals(directory.toPath().resolve("src/main/java/application.properties").toUri().toString(), location.getUri());
231-
assertEquals(0, location.getRange().getStart().getLine());
232-
assertEquals(0, location.getRange().getStart().getCharacter());
233-
assertEquals(0, location.getRange().getEnd().getLine());
234-
assertEquals(7, location.getRange().getEnd().getCharacter());
206+
assertEquals(new Location(location.getUri(), new Range(new Position(0, 0), new Position(0, 7))), location);
235207
}
236208

237209
@Test
@@ -241,47 +213,35 @@ void testFindReferenceForPropertiesUsedInAnnotations() throws Exception {
241213
List<? extends Location> locations = provider.findReferencesToPropertyKey("my.prop2");
242214

243215
assertNotNull(locations);
244-
assertEquals(5, locations.size());
216+
assertEquals(7, locations.size());
245217

246218
URI propertiesFile = directory.toPath().resolve("src/main/java/application.properties").toUri();
247219
Location location = getLocation(locations, propertiesFile);
248-
assertNotNull(location);
249-
assertEquals(1, location.getRange().getStart().getLine());
250-
assertEquals(0, location.getRange().getStart().getCharacter());
251-
assertEquals(1, location.getRange().getEnd().getLine());
252-
assertEquals(8, location.getRange().getEnd().getCharacter());
220+
assertEquals(new Location(propertiesFile.toString(), new Range(new Position(1, 0), new Position(1, 8))), location);
253221

254222
URI javaFile = directory.toPath().resolve("src/main/java/org/test/properties/PropertyUsageWithValue.java").toUri();
255223
location = getLocation(locations, javaFile);
256-
assertNotNull(location);
257-
assertEquals(8, location.getRange().getStart().getLine());
258-
assertEquals(8, location.getRange().getStart().getCharacter());
259-
assertEquals(8, location.getRange().getEnd().getLine());
260-
assertEquals(21, location.getRange().getEnd().getCharacter());
224+
assertEquals(new Location(javaFile.toString(), new Range(new Position(8, 8), new Position(8, 21))), location);
261225

262226
javaFile = directory.toPath().resolve("src/main/java/org/test/properties/PropertyUsageWithConditional.java").toUri();
263227
location = getLocation(locations, javaFile);
264-
assertNotNull(location);
265-
assertEquals(6, location.getRange().getStart().getLine());
266-
assertEquals(23, location.getRange().getStart().getCharacter());
267-
assertEquals(6, location.getRange().getEnd().getLine());
268-
assertEquals(33, location.getRange().getEnd().getCharacter());
228+
assertEquals(new Location(javaFile.toString(), new Range(new Position(6, 23), new Position(6, 33))), location);
269229

270230
javaFile = directory.toPath().resolve("src/main/java/org/test/properties/PropertyUsageWithConditionalAndArray.java").toUri();
271231
location = getLocation(locations, javaFile);
272-
assertNotNull(location);
273-
assertEquals(6, location.getRange().getStart().getLine());
274-
assertEquals(31, location.getRange().getStart().getCharacter());
275-
assertEquals(6, location.getRange().getEnd().getLine());
276-
assertEquals(41, location.getRange().getEnd().getCharacter());
232+
assertEquals(new Location(javaFile.toString(), new Range(new Position(6, 31), new Position(6, 41))), location);
277233

278234
javaFile = directory.toPath().resolve("src/main/java/org/test/properties/PropertyUsageWithConditionalWithArrayAndPrefix.java").toUri();
279235
location = getLocation(locations, javaFile);
280-
assertNotNull(location);
281-
assertEquals(6, location.getRange().getStart().getLine());
282-
assertEquals(46, location.getRange().getStart().getCharacter());
283-
assertEquals(6, location.getRange().getEnd().getLine());
284-
assertEquals(53, location.getRange().getEnd().getCharacter());
285-
}
236+
assertEquals(new Location(javaFile.toString(), new Range(new Position(6, 46), new Position(6, 53))), location);
237+
238+
javaFile = directory.toPath().resolve("src/main/java/org/test/properties/PropertyUsageWithConstantInConditionalAnnotation.java").toUri();
239+
location = getLocation(locations, javaFile);
240+
assertEquals(new Location(javaFile.toString(), new Range(new Position(6, 23), new Position(6, 52))), location);
241+
242+
javaFile = directory.toPath().resolve("src/main/java/org/test/properties/PropertyUsageWithConstantInValueAnnotation.java").toUri();
243+
location = getLocation(locations, javaFile);
244+
assertEquals(new Location(javaFile.toString(), new Range(new Position(8, 8), new Position(8, 41))), location);
245+
}
286246

287247
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.test.properties;
2+
3+
public class Constants {
4+
5+
public static final String PURE_PROPERTY_VALUE = "my.prop2";
6+
public static final String EMBEDDED_PROPERTY_VALUE = "${my.prop2}";
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.test.properties;
2+
3+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@ConditionalOnProperty(Constants.PURE_PROPERTY_VALUE)
8+
public class PropertyUsageWithConstantInConditionalAnnotation {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.test.properties;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class PropertyUsageWithConstantInValueAnnotation {
8+
9+
@Value(Constants.EMBEDDED_PROPERTY_VALUE)
10+
private String someProp;
11+
12+
}

0 commit comments

Comments
 (0)