Skip to content

Commit 90dce92

Browse files
Constant referenced in annotation is not recognized as a constant value (eclipse-jdt#4840)
* Fixes eclipse-jdt#4835
1 parent 7f2f38b commit 90dce92

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ public FieldBinding getField(char[] fieldName, boolean needResolve) {
13911391
if ((this.tagBits & TagBits.AreFieldsComplete) != 0)
13921392
return ReferenceBinding.binarySearch(fieldName, this.fields);
13931393

1394-
if ((this.tagBits & TagBits.HasUnresolvedComponents) != 0)
1394+
if (needResolve && (this.tagBits & TagBits.HasUnresolvedComponents) != 0)
13951395
components();
13961396

13971397
// lazily sort fields

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordsRestrictedClassTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11806,4 +11806,59 @@ record DoublePoint(Point p1, Point p2) {
1180611806
"Cannot make a static reference to the non-static variable args\n" +
1180711807
"----------\n");
1180811808
}
11809+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4835
11810+
// Constant referenced in annotation is not recognized as a constant value
11811+
public void testIssue4835() throws Exception {
11812+
this.runConformTest(
11813+
new String[] {
11814+
"testing/X.java",
11815+
"""
11816+
package testing;
11817+
11818+
import testing.X.JsonProperties;
11819+
11820+
import java.lang.annotation.ElementType;
11821+
import java.lang.annotation.Retention;
11822+
import java.lang.annotation.RetentionPolicy;
11823+
import java.lang.annotation.Target;
11824+
11825+
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
11826+
@Retention(RetentionPolicy.RUNTIME)
11827+
11828+
@interface JsonProperty {
11829+
String value();
11830+
}
11831+
11832+
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE,
11833+
ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})
11834+
@Retention(RetentionPolicy.RUNTIME)
11835+
11836+
@interface JsonPropertyOrder {
11837+
public String[] value() default { };
11838+
}
11839+
11840+
11841+
@JsonPropertyOrder({
11842+
JsonProperties.CONSTANT_ONE,
11843+
JsonProperties.CONSTANT_TWO,
11844+
})
11845+
public record X(
11846+
@JsonProperty(JsonProperties.CONSTANT_ONE) String attributeOne, // Error in this line
11847+
@JsonProperty(JsonProperties.CONSTANT_TWO) String attributeTwo) {
11848+
11849+
class JsonProperties {
11850+
11851+
public static final String CONSTANT_ONE = "one";
11852+
11853+
public static final String CONSTANT_TWO = "two";
11854+
}
11855+
11856+
public static void main(String [] args) {
11857+
System.out.println("OK!");
11858+
}
11859+
}
11860+
""",
11861+
},
11862+
"OK!");
11863+
}
1180911864
}

0 commit comments

Comments
 (0)