diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java index 487e8a3df468..33d4a24b6e5b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java @@ -927,11 +927,11 @@ private boolean populateRecordFields(BLangExpression expr, BConstantSymbol const return false; } keyValuePair.setBType(newType); - if (newType.getKind() != TypeKind.FINITE) { + TypeKind kind = newType.getKind(); + if (kind != TypeKind.FINITE) { constValueMap.get(key).type = newType; - if (newType.getKind() == TypeKind.INTERSECTION) { - exprValueField.setBType(((BIntersectionType) newType).effectiveType); - } + BType type = kind == TypeKind.INTERSECTION ? ((BIntersectionType) newType).effectiveType : newType; + exprValueField.setBType(type); } recordType.fields.put(key, createField(newSymbol, newType, key, pos)); diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationRuntimeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationRuntimeTest.java index af8cd3af966a..0f68191b3167 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationRuntimeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationRuntimeTest.java @@ -216,6 +216,11 @@ public void testServiceRemoteMethodAnnotations() { BRunUtil.invoke(result, "testServiceRemoteMethodAnnotations"); } + @Test + public void testConstTypeAnnotAccess() { + BRunUtil.invoke(resultOne, "testConstTypeAnnotAccess"); + } + @AfterClass public void tearDown() { resultOne = null; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_access.bal b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_access.bal index c684ff125edd..c55338617d4c 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_access.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_access.bal @@ -359,6 +359,68 @@ type MedicalNeed record { }; +public type AnnotationRecord record {| + string summary?; + Examples examples?; +|}; + +public type Examples record {| + map response; +|}; + +public type ExampleItem record { + map headers?; +}; + +const annotation AnnotationRecord annot on type; + +@annot { + examples: { + response: {} + } +} +type Employee record {| + int id; + string name; +|}; + +public type AnnotationRecord1 record {| + string summary?; + ExamplesReference examples?; +|}; + +type ExamplesReference Examples; + +const annotation AnnotationRecord1 annot1 on type; + +@annot1 { + examples: { + response: {} + } +} +type Student record {| + int id; + string name; +|}; + +function testConstTypeAnnotAccess() { + Employee employee = {id: 1, name: "chirans"}; + typedesc t = typeof employee; + AnnotationRecord? annot = t.@annot; + assertTrue(annot is AnnotationRecord); + AnnotationRecord config = annot; + assertEquality({"response":{}}, config.examples); + assertTrue(config.examples is readonly); + + Student student = {id: 2, name: "sachintha"}; + typedesc s = typeof student; + AnnotationRecord1? annot1 = s.@annot1; + assertTrue(annot1 is AnnotationRecord1); + AnnotationRecord1 config1 = annot1; + assertEquality({"response":{}}, config1.examples); + assertTrue(config1.examples is readonly); +} + function testListExprInConstAnnot() { EntityConfig? annot = MedicalNeed.@Entity; assertTrue(annot is EntityConfig);