resolve constants for expressions when extracting annotation attribute values

This commit is contained in:
Martin Lippert
2024-11-15 13:13:03 +01:00
parent 1e9e893994
commit 5cded42633
7 changed files with 65 additions and 77 deletions

View File

@@ -669,7 +669,9 @@ public class ASTUtils {
Range range = doc.toRange(region);
Location location = new Location(doc.getUri(), range);
return new AnnotationAttributeValue(expression.toString(), location);
Object constantExpressionValue = expression.resolveConstantExpressionValue();
return new AnnotationAttributeValue(constantExpressionValue != null ? constantExpressionValue.toString() : expression.toString(), location);
}
}
@@ -698,7 +700,7 @@ public class ASTUtils {
ITypeBinding klass = varBinding.getDeclaringClass();
if (klass != null) {
result.dereferencedType= klass;
result.dereferencedType = klass;
}
}

View File

@@ -230,7 +230,6 @@ public class SpringMetamodelIndexerBeansTest {
assertEquals(1, point2Annotations[1].getAttributes().get("value").length);
assertEquals("setter-injection-qualifier-on-param", point2Annotations[1].getAttributes().get("value")[0].getName());
assertEquals(new Location(docUri, new Range(new Position(26, 33), new Position(26, 70))), point2Annotations[1].getAttributes().get("value")[0].getLocation());
}
@Test

View File

@@ -132,10 +132,13 @@ public class ValueCompletionTest {
@BeforeEach
public void setup() throws Exception {
harness.intialize(null);
prepareDefaultIndexData();
indexHarness.data("spring.prop1", "java.lang.String", null, null);
indexHarness.data("data.prop2", "java.lang.String", null, null);
indexHarness.data("else.prop3", "java.lang.String", null, null);
}
@Test
@Test
void testPrefixIdentification() {
ValueCompletionProcessor processor = new ValueCompletionProcessor(projectFinder, null, null);
@@ -579,12 +582,6 @@ public class ValueCompletionTest {
}
private void prepareDefaultIndexData() {
indexHarness.data("spring.prop1", "java.lang.String", null, null);
indexHarness.data("data.prop2", "java.lang.String", null, null);
indexHarness.data("else.prop3", "java.lang.String", null, null);
}
private void prepareCase(String selectedAnnotation, String annotationStatementBeforeTest) throws Exception {
InputStream resource = this.getClass().getResourceAsStream("/test-projects/test-annotations/src/main/java/org/test/TestValueCompletion.java");
String content = IOUtils.toString(resource, Charset.defaultCharset());

View File

@@ -21,6 +21,8 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.WorkspaceFolder;
import org.junit.jupiter.api.BeforeEach;
@@ -86,11 +88,7 @@ public class ValuePropertyReferenceFinderTest {
Location location = locations.get(0);
URI docURI = file.toUri();
assertEquals(docURI.toString(), location.getUri());
assertEquals(0, location.getRange().getStart().getLine());
assertEquals(0, location.getRange().getStart().getCharacter());
assertEquals(0, location.getRange().getEnd().getLine());
assertEquals(13, location.getRange().getEnd().getCharacter());
assertEquals(new Location(docURI.toString(), new Range(new Position(0, 0), new Position(0, 13))), location);
}
@Test
@@ -105,11 +103,7 @@ public class ValuePropertyReferenceFinderTest {
Location location = locations.get(0);
URI docURI = file.toUri();
assertEquals(docURI.toString(), location.getUri());
assertEquals(3, location.getRange().getStart().getLine());
assertEquals(2, location.getRange().getStart().getCharacter());
assertEquals(3, location.getRange().getEnd().getLine());
assertEquals(10, location.getRange().getEnd().getCharacter());
assertEquals(new Location(docURI.toString(), new Range(new Position(3, 2), new Position(3, 10))), location);
}
@Test
@@ -123,35 +117,19 @@ public class ValuePropertyReferenceFinderTest {
Path file1 = resourceDir.resolve("mixed-multiple-files/application-dev.properties");
Location location = getLocation(locations, file1.toUri());
assertNotNull(location);
assertEquals(1, location.getRange().getStart().getLine());
assertEquals(0, location.getRange().getStart().getCharacter());
assertEquals(1, location.getRange().getEnd().getLine());
assertEquals(10, location.getRange().getEnd().getCharacter());
assertEquals(new Location(file1.toUri().toString(), new Range(new Position(1, 0), new Position(1, 10))), location);
Path file2 = resourceDir.resolve("mixed-multiple-files/application.yml");
location = getLocation(locations, file2.toUri());
assertNotNull(location);
assertEquals(3, location.getRange().getStart().getLine());
assertEquals(2, location.getRange().getStart().getCharacter());
assertEquals(3, location.getRange().getEnd().getLine());
assertEquals(6, location.getRange().getEnd().getCharacter());
assertEquals(new Location(file2.toUri().toString(), new Range(new Position(3, 2), new Position(3, 6))), location);
Path file3 = resourceDir.resolve("another-prop-folder/application.properties");
location = getLocation(locations, file3.toUri());
assertNotNull(location);
assertEquals(1, location.getRange().getStart().getLine());
assertEquals(0, location.getRange().getStart().getCharacter());
assertEquals(1, location.getRange().getEnd().getLine());
assertEquals(10, location.getRange().getEnd().getCharacter());
assertEquals(new Location(file3.toUri().toString(), new Range(new Position(1, 0), new Position(1, 10))), location);
Path file4 = resourceDir.resolve("another-prop-folder/prod-application.properties");
location = getLocation(locations, file4.toUri());
assertNotNull(location);
assertEquals(1, location.getRange().getStart().getLine());
assertEquals(0, location.getRange().getStart().getCharacter());
assertEquals(1, location.getRange().getEnd().getLine());
assertEquals(10, location.getRange().getEnd().getCharacter());
assertEquals(new Location(file4.toUri().toString(), new Range(new Position(1, 0), new Position(1, 10))), location);
}
private Location getLocation(List<? extends Location> locations, URI docURI) {
@@ -193,10 +171,7 @@ public class ValuePropertyReferenceFinderTest {
Location location = references.get(0);
assertEquals(directory.toPath().resolve("src/main/java/application.properties").toUri().toString(), location.getUri());
assertEquals(0, location.getRange().getStart().getLine());
assertEquals(0, location.getRange().getStart().getCharacter());
assertEquals(0, location.getRange().getEnd().getLine());
assertEquals(7, location.getRange().getEnd().getCharacter());
assertEquals(new Location(location.getUri(), new Range(new Position(0, 0), new Position(0, 7))), location);
}
@Test
@@ -228,10 +203,7 @@ public class ValuePropertyReferenceFinderTest {
Location location = references.get(0);
assertEquals(directory.toPath().resolve("src/main/java/application.properties").toUri().toString(), location.getUri());
assertEquals(0, location.getRange().getStart().getLine());
assertEquals(0, location.getRange().getStart().getCharacter());
assertEquals(0, location.getRange().getEnd().getLine());
assertEquals(7, location.getRange().getEnd().getCharacter());
assertEquals(new Location(location.getUri(), new Range(new Position(0, 0), new Position(0, 7))), location);
}
@Test
@@ -241,47 +213,35 @@ public class ValuePropertyReferenceFinderTest {
List<? extends Location> locations = provider.findReferencesToPropertyKey("my.prop2");
assertNotNull(locations);
assertEquals(5, locations.size());
assertEquals(7, locations.size());
URI propertiesFile = directory.toPath().resolve("src/main/java/application.properties").toUri();
Location location = getLocation(locations, propertiesFile);
assertNotNull(location);
assertEquals(1, location.getRange().getStart().getLine());
assertEquals(0, location.getRange().getStart().getCharacter());
assertEquals(1, location.getRange().getEnd().getLine());
assertEquals(8, location.getRange().getEnd().getCharacter());
assertEquals(new Location(propertiesFile.toString(), new Range(new Position(1, 0), new Position(1, 8))), location);
URI javaFile = directory.toPath().resolve("src/main/java/org/test/properties/PropertyUsageWithValue.java").toUri();
location = getLocation(locations, javaFile);
assertNotNull(location);
assertEquals(8, location.getRange().getStart().getLine());
assertEquals(8, location.getRange().getStart().getCharacter());
assertEquals(8, location.getRange().getEnd().getLine());
assertEquals(21, location.getRange().getEnd().getCharacter());
assertEquals(new Location(javaFile.toString(), new Range(new Position(8, 8), new Position(8, 21))), location);
javaFile = directory.toPath().resolve("src/main/java/org/test/properties/PropertyUsageWithConditional.java").toUri();
location = getLocation(locations, javaFile);
assertNotNull(location);
assertEquals(6, location.getRange().getStart().getLine());
assertEquals(23, location.getRange().getStart().getCharacter());
assertEquals(6, location.getRange().getEnd().getLine());
assertEquals(33, location.getRange().getEnd().getCharacter());
assertEquals(new Location(javaFile.toString(), new Range(new Position(6, 23), new Position(6, 33))), location);
javaFile = directory.toPath().resolve("src/main/java/org/test/properties/PropertyUsageWithConditionalAndArray.java").toUri();
location = getLocation(locations, javaFile);
assertNotNull(location);
assertEquals(6, location.getRange().getStart().getLine());
assertEquals(31, location.getRange().getStart().getCharacter());
assertEquals(6, location.getRange().getEnd().getLine());
assertEquals(41, location.getRange().getEnd().getCharacter());
assertEquals(new Location(javaFile.toString(), new Range(new Position(6, 31), new Position(6, 41))), location);
javaFile = directory.toPath().resolve("src/main/java/org/test/properties/PropertyUsageWithConditionalWithArrayAndPrefix.java").toUri();
location = getLocation(locations, javaFile);
assertNotNull(location);
assertEquals(6, location.getRange().getStart().getLine());
assertEquals(46, location.getRange().getStart().getCharacter());
assertEquals(6, location.getRange().getEnd().getLine());
assertEquals(53, location.getRange().getEnd().getCharacter());
}
assertEquals(new Location(javaFile.toString(), new Range(new Position(6, 46), new Position(6, 53))), location);
javaFile = directory.toPath().resolve("src/main/java/org/test/properties/PropertyUsageWithConstantInConditionalAnnotation.java").toUri();
location = getLocation(locations, javaFile);
assertEquals(new Location(javaFile.toString(), new Range(new Position(6, 23), new Position(6, 52))), location);
javaFile = directory.toPath().resolve("src/main/java/org/test/properties/PropertyUsageWithConstantInValueAnnotation.java").toUri();
location = getLocation(locations, javaFile);
assertEquals(new Location(javaFile.toString(), new Range(new Position(8, 8), new Position(8, 41))), location);
}
}

View File

@@ -0,0 +1,8 @@
package org.test.properties;
public class Constants {
public static final String PURE_PROPERTY_VALUE = "my.prop2";
public static final String EMBEDDED_PROPERTY_VALUE = "${my.prop2}";
}

View File

@@ -0,0 +1,10 @@
package org.test.properties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(Constants.PURE_PROPERTY_VALUE)
public class PropertyUsageWithConstantInConditionalAnnotation {
}

View File

@@ -0,0 +1,12 @@
package org.test.properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class PropertyUsageWithConstantInValueAnnotation {
@Value(Constants.EMBEDDED_PROPERTY_VALUE)
private String someProp;
}