From bc85663bebb785e0b6a4d756851dc9ae13b52368 Mon Sep 17 00:00:00 2001 From: mindula Date: Tue, 25 Jun 2024 13:51:15 +0530 Subject: [PATCH 01/11] Add withoutAttributes option --- .../XMLToRecordConverter.java | 35 +++++++++++-------- .../XMLToRecordConverterService.java | 4 ++- .../XMLToRecordRequest.java | 15 +++++++- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java index 91d52eb8816e..0c6e2d2cc1ca 100644 --- a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java +++ b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java @@ -97,7 +97,8 @@ private XMLToRecordConverter() {} public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeDesc, boolean isClosed, boolean forceFormatRecordFields, - String textFieldName, boolean withNameSpaces) { + String textFieldName, boolean withNameSpaces, boolean withoutAttributes, + boolean withoutAttributeAnnot) { Map recordToTypeDescNodes = new LinkedHashMap<>(); Map recordToAnnotationNodes = new LinkedHashMap<>(); Map recordToElementNodes = new LinkedHashMap<>(); @@ -116,7 +117,8 @@ public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeD Element rootElement = doc.getDocumentElement(); generateRecords(rootElement, isClosed, recordToTypeDescNodes, recordToAnnotationNodes, - recordToElementNodes, diagnosticMessages, textFieldName, withNameSpaces); + recordToElementNodes, diagnosticMessages, textFieldName, withNameSpaces, withoutAttributes, + withoutAttributeAnnot); } catch (ParserConfigurationException parserConfigurationException) { DiagnosticMessage message = DiagnosticMessage.xmlToRecordConverter100(null); diagnosticMessages.add(message); @@ -187,7 +189,7 @@ public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeD */ public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeDesc, boolean isClosed, boolean forceFormatRecordFields) { - return convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields, null, true); + return convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields, null, true, false, false); } private static void generateRecords(Element xmlElement, boolean isClosed, @@ -195,7 +197,8 @@ private static void generateRecords(Element xmlElement, boolean isClosed, Map recordToAnnotationsNodes, Map recordToElementNodes, List diagnosticMessages, - String textFieldName, boolean withNameSpace) { + String textFieldName, boolean withNameSpace, boolean withoutAttributes, + boolean withoutAttributeAnnot) { Token recordKeyWord = AbstractNodeFactory.createToken(SyntaxKind.RECORD_KEYWORD); Token bodyStartDelimiter = AbstractNodeFactory.createToken(isClosed ? SyntaxKind.OPEN_BRACE_PIPE_TOKEN : SyntaxKind.OPEN_BRACE_TOKEN); @@ -204,7 +207,7 @@ private static void generateRecords(Element xmlElement, boolean isClosed, List recordFields = getRecordFieldsForXMLElement(xmlElement, isClosed, recordToTypeDescNodes, recordToAnnotationsNodes, recordToElementNodes, diagnosticMessages, textFieldName, - withNameSpace); + withNameSpace, withoutAttributes, withoutAttributeAnnot); if (recordToTypeDescNodes.containsKey(xmlNodeName)) { RecordTypeDescriptorNode previousRecordTypeDescriptorNode = (RecordTypeDescriptorNode) recordToTypeDescNodes.get(xmlNodeName); @@ -237,7 +240,8 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole Map recordToAnnotationNodes, Map recordToElementNodes, List diagnosticMessages, - String textFieldName, boolean withNameSpace) { + String textFieldName, boolean withNameSpace, + boolean withoutAttributes, boolean withoutAttributeAnnot) { List recordFields = new ArrayList<>(); String xmlNodeName = xmlElement.getNodeName(); @@ -249,16 +253,17 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole Element xmlElementNode = (Element) xmlNode; boolean isLeafXMLElementNode = isLeafXMLElementNode(xmlElementNode); NamedNodeMap xmlAttributesMap = xmlElementNode.getAttributes(); - if (!isLeafXMLElementNode || xmlAttributesMap.getLength() > 1 + if (!isLeafXMLElementNode || (!withoutAttributes && (xmlAttributesMap.getLength() > 1 || (xmlAttributesMap.getLength() == 1 - && !XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix()))) { + && !XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix()))))) { generateRecords(xmlElementNode, isClosed, recordToTypeDescNodes, recordToAnnotationNodes, - recordToElementNodes, diagnosticMessages, textFieldName, withNameSpace); + recordToElementNodes, diagnosticMessages, textFieldName, withNameSpace, withoutAttributes, + withoutAttributeAnnot); } Map prefixMap = hasMultipleFieldsWithSameName(xmlNodeList, xmlElementNode.getLocalName()); RecordFieldNode recordField = getRecordField(xmlElementNode, false, withNameSpace, - prefixMap.size() > 1); + prefixMap.size() > 1, withoutAttributes, withoutAttributeAnnot); if (withNameSpace && xmlElementNode.getPrefix() != null) { int indexOfRecordFieldNode = IntStream.range(0, recordFields.size()) @@ -314,7 +319,8 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole } AnnotationNode xmlNSNode = getXMLNamespaceNode(prefix, xmlNode.getNodeValue()); recordToAnnotationNodes.put(xmlNodeName, xmlNSNode); - } else if (!isLeafXMLElementNode(xmlElement) && !XMLNS_PREFIX.equals(xmlNode.getPrefix())) { + } else if (!isLeafXMLElementNode(xmlElement) && !XMLNS_PREFIX.equals(xmlNode.getPrefix()) + && !withoutAttributes) { if (elementNames.contains(xmlNode.getNodeName())) { continue; } @@ -325,7 +331,7 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole } int attributeLength = xmlElement.getAttributes().getLength(); org.w3c.dom.Node attributeItem = xmlElement.getAttributes().item(0); - if (isLeafXMLElementNode(xmlElement) && attributeLength > 0) { + if (isLeafXMLElementNode(xmlElement) && attributeLength > 0 && !withoutAttributes) { if (attributeLength == 1 && attributeItem.getPrefix() != null && XMLNS_PREFIX.equals(attributeItem.getPrefix())) { return recordFields; @@ -463,7 +469,8 @@ private static List updateRecordFields(Map previo } private static RecordFieldNode getRecordField(Element xmlElementNode, boolean isOptionalField, - boolean withNameSpace, boolean sameFieldExists) { + boolean withNameSpace, boolean sameFieldExists, + boolean withoutAttributes, boolean withoutAttributeAnnot) { Token typeName; Token questionMarkToken = AbstractNodeFactory.createToken(SyntaxKind.QUESTION_MARK_TOKEN); IdentifierToken fieldName = @@ -474,7 +481,7 @@ private static RecordFieldNode getRecordField(Element xmlElementNode, boolean is NamedNodeMap xmlAttributesMap = xmlElementNode.getAttributes(); if (isLeafXMLElementNode(xmlElementNode) && (xmlAttributesMap.getLength() == 0 || (xmlAttributesMap.getLength() == 1 - && XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix())))) { + && XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix()))) || withoutAttributes) { typeName = getPrimitiveTypeName(xmlElementNode.getFirstChild().getNodeValue()); } else { // At the moment all are considered as Objects here diff --git a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterService.java b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterService.java index c0f7b3782040..8e0b5252eff9 100644 --- a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterService.java +++ b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterService.java @@ -48,9 +48,11 @@ public CompletableFuture convert(XMLToRecordRequest request boolean forceFormatRecordFields = request.getForceFormatRecordFields(); String textFieldName = request.getTextFieldName(); boolean withNameSpace = request.getIsWithNameSpace(); + boolean withoutAttributes = request.getWithoutAttributes(); + boolean withoutAttributeAnnot = request.getWithoutAttributeAnnot(); return XMLToRecordConverter.convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields, - textFieldName, withNameSpace); + textFieldName, withNameSpace, withoutAttributes, withoutAttributeAnnot); }); } diff --git a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordRequest.java b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordRequest.java index be28d7cadfb4..46b0607c2a6c 100644 --- a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordRequest.java +++ b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordRequest.java @@ -31,15 +31,20 @@ public class XMLToRecordRequest { private final boolean forceFormatRecordFields; private final String textFieldName; private final boolean withNameSpace; + private final boolean withoutAttributes; + private final boolean withoutAttributeAnnot; public XMLToRecordRequest(String xmlValue, boolean isRecordTypeDesc, boolean isClosed, - boolean forceFormatRecordFields, String textFieldName, boolean withNameSpace) { + boolean forceFormatRecordFields, String textFieldName, boolean withNameSpace, + boolean withoutAttributes, boolean withoutAttributeAnnot) { this.xmlValue = xmlValue; this.isRecordTypeDesc = isRecordTypeDesc; this.isClosed = isClosed; this.forceFormatRecordFields = forceFormatRecordFields; this.textFieldName = textFieldName; this.withNameSpace = withNameSpace; + this.withoutAttributes = withoutAttributes; + this.withoutAttributeAnnot = withoutAttributeAnnot; } public String getXmlValue() { @@ -65,4 +70,12 @@ public String getTextFieldName() { public boolean getIsWithNameSpace() { return withNameSpace; } + + public boolean getWithoutAttributes() { + return withoutAttributes; + } + + public boolean getWithoutAttributeAnnot() { + return withoutAttributeAnnot; + } } From 6f9475fc75647fe2e7ebb415734a3d2d0f7bdafe Mon Sep 17 00:00:00 2001 From: mindula Date: Wed, 26 Jun 2024 14:26:25 +0530 Subject: [PATCH 02/11] Add without attribute annotation option --- .../xmltorecordconverter/XMLToRecordConverter.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java index 0c6e2d2cc1ca..c75554e1e1ed 100644 --- a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java +++ b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java @@ -263,7 +263,7 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole Map prefixMap = hasMultipleFieldsWithSameName(xmlNodeList, xmlElementNode.getLocalName()); RecordFieldNode recordField = getRecordField(xmlElementNode, false, withNameSpace, - prefixMap.size() > 1, withoutAttributes, withoutAttributeAnnot); + prefixMap.size() > 1, withoutAttributes); if (withNameSpace && xmlElementNode.getPrefix() != null) { int indexOfRecordFieldNode = IntStream.range(0, recordFields.size()) @@ -324,7 +324,7 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole if (elementNames.contains(xmlNode.getNodeName())) { continue; } - Node recordField = getRecordField(xmlNode); + Node recordField = getRecordField(xmlNode, withoutAttributeAnnot); recordFields.add(recordField); } } @@ -347,7 +347,7 @@ private static List getRecordFieldsForXMLElement(Element xmlElement, boole org.w3c.dom.Node xmlAttributeNode = xmlElement.getAttributes().item(j); if (xmlAttributeNode.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE && !XMLNS_PREFIX.equals(xmlAttributeNode.getPrefix())) { - Node recordField = getRecordField(xmlAttributeNode); + Node recordField = getRecordField(xmlAttributeNode, withoutAttributeAnnot); recordFields.add(recordField); } } @@ -470,7 +470,7 @@ private static List updateRecordFields(Map previo private static RecordFieldNode getRecordField(Element xmlElementNode, boolean isOptionalField, boolean withNameSpace, boolean sameFieldExists, - boolean withoutAttributes, boolean withoutAttributeAnnot) { + boolean withoutAttributes) { Token typeName; Token questionMarkToken = AbstractNodeFactory.createToken(SyntaxKind.QUESTION_MARK_TOKEN); IdentifierToken fieldName = @@ -481,7 +481,7 @@ private static RecordFieldNode getRecordField(Element xmlElementNode, boolean is NamedNodeMap xmlAttributesMap = xmlElementNode.getAttributes(); if (isLeafXMLElementNode(xmlElementNode) && (xmlAttributesMap.getLength() == 0 || (xmlAttributesMap.getLength() == 1 - && XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix()))) || withoutAttributes) { + && XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix())) || withoutAttributes)) { typeName = getPrimitiveTypeName(xmlElementNode.getFirstChild().getNodeValue()); } else { // At the moment all are considered as Objects here @@ -510,7 +510,7 @@ private static RecordFieldNode getRecordField(Element xmlElementNode, boolean is metadataNode, null, fieldTypeName, fieldName, optionalFieldToken, semicolonToken); } - private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode) { + private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode, boolean withoutAttributeAnnot) { Token typeName = AbstractNodeFactory.createToken(SyntaxKind.STRING_KEYWORD); TypeDescriptorNode fieldTypeName = NodeFactory.createBuiltinSimpleNameReferenceNode(typeName.kind(), typeName); IdentifierToken fieldName = @@ -518,7 +518,7 @@ private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode) { Token equalToken = AbstractNodeFactory.createToken(SyntaxKind.EQUAL_TOKEN); Token semicolonToken = AbstractNodeFactory.createToken(SyntaxKind.SEMICOLON_TOKEN); NodeList annotations = AbstractNodeFactory.createNodeList(getXMLAttributeNode()); - MetadataNode metadataNode = NodeFactory.createMetadataNode(null, annotations); + MetadataNode metadataNode = withoutAttributeAnnot ? null : NodeFactory.createMetadataNode(null, annotations); if (xmlAttributeNode.getPrefix() != null && xmlAttributeNode.getPrefix().equals(XMLNS_PREFIX)) { From 311d04ee7054f7dd949a3f8da25fce212eaf86f6 Mon Sep 17 00:00:00 2001 From: mindula Date: Wed, 26 Jun 2024 14:27:17 +0530 Subject: [PATCH 03/11] Add tests --- .../XMLToRecordConverterTests.java | 72 ++++++++++++++----- .../test/resources/ballerina/sample_35.bal | 11 +++ .../test/resources/ballerina/sample_36.bal | 22 ++++++ .../test/resources/ballerina/sample_37.bal | 37 ++++++++++ .../src/test/resources/xml/sample_35.xml | 8 +++ .../src/test/resources/xml/sample_36.xml | 13 ++++ 6 files changed, 147 insertions(+), 16 deletions(-) create mode 100644 misc/xml-to-record-converter/src/test/resources/ballerina/sample_35.bal create mode 100644 misc/xml-to-record-converter/src/test/resources/ballerina/sample_36.bal create mode 100644 misc/xml-to-record-converter/src/test/resources/ballerina/sample_37.bal create mode 100644 misc/xml-to-record-converter/src/test/resources/xml/sample_35.xml create mode 100644 misc/xml-to-record-converter/src/test/resources/xml/sample_36.xml diff --git a/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java b/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java index d4a7818a9f21..4e33124984d6 100644 --- a/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java +++ b/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java @@ -214,6 +214,18 @@ public class XMLToRecordConverterTests { private final Path sample34Bal = RES_DIR.resolve(BAL_DIR) .resolve("sample_34.bal"); + private final Path sample35XML = RES_DIR.resolve(XML_DIR) + .resolve("sample_35.xml"); + private final Path sample35Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_35.bal"); + private final Path sample36Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_36.bal"); + + private final Path sample36XML = RES_DIR.resolve(XML_DIR) + .resolve("sample_36.xml"); + private final Path sample37Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_37.bal"); + private static final String XMLToRecordServiceEP = "xmlToRecord/convert"; @@ -392,7 +404,7 @@ public void testWithMultipleAttributes() throws IOException { public void testXMLWithNamespacesWithoutNamespaceAttribute() throws IOException { String xmlFileContent = Files.readString(sample19XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - "amount", false).getCodeBlock().replaceAll("\\s+", ""); + "amount", false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample19Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -401,7 +413,7 @@ public void testXMLWithNamespacesWithoutNamespaceAttribute() throws IOException public void testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations() throws IOException { String xmlFileContent = Files.readString(sample20XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample20Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -410,7 +422,7 @@ public void testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations() throw public void testXMLWithMultipleAttributesAndNamespacesWithAnnotations() throws IOException { String xmlFileContent = Files.readString(sample21XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample21Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -419,7 +431,7 @@ public void testXMLWithMultipleAttributesAndNamespacesWithAnnotations() throws I public void testXMLWithoutNamespacePrefix() throws IOException { String xmlFileContent = Files.readString(sample22XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample22Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -428,7 +440,7 @@ public void testXMLWithoutNamespacePrefix() throws IOException { public void testXMLWithConflictingElementAndAttributeNames() throws IOException { String xmlFileContent = Files.readString(sample23XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample23Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -437,7 +449,7 @@ public void testXMLWithConflictingElementAndAttributeNames() throws IOException public void testXMLWithoutNamespaces() throws IOException { String xmlFileContent = Files.readString(sample24XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample24Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -447,7 +459,7 @@ public void testXMLToRecordService() throws IOException, ExecutionException, Int Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); String xmlValue = Files.readString(sample0XML); - XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, null, true); + XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, null, true, false, false); CompletableFuture result = serviceEndpoint.request(XMLToRecordServiceEP, request); XMLToRecordResponse response = (XMLToRecordResponse) result.get(); String generatedCodeBlock = response.getCodeBlock().replaceAll("\\s+", ""); @@ -461,7 +473,8 @@ public void testXMLToRecordServiceWithFieldNameAndWithoutNamespace() Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); String xmlValue = Files.readString(sample25XML); - XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, "__text", false); + XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, "__text", + false, false, false); CompletableFuture result = serviceEndpoint.request(XMLToRecordServiceEP, request); XMLToRecordResponse response = (XMLToRecordResponse) result.get(); String generatedCodeBlock = response.getCodeBlock().replaceAll("\\s+", ""); @@ -482,7 +495,7 @@ public void testXMLWithMultipleAttributes() throws IOException { public void testXMLWithoutNamespaceAnnotations() throws IOException { String xmlFileContent = Files.readString(sample27XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample27Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -491,7 +504,7 @@ public void testXMLWithoutNamespaceAnnotations() throws IOException { public void testXMLWithMultipleNamespacesAndSameElement() throws IOException { String xmlFileContent = Files.readString(sample28XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample28Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -500,7 +513,7 @@ public void testXMLWithMultipleNamespacesAndSameElement() throws IOException { public void testXMLWithMultipleNamespaces2() throws IOException { String xmlFileContent = Files.readString(sample29XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample29Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -509,7 +522,7 @@ public void testXMLWithMultipleNamespaces2() throws IOException { public void testXMLWithMultipleNamespaces3() throws IOException { String xmlFileContent = Files.readString(sample30XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample30Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -518,7 +531,7 @@ public void testXMLWithMultipleNamespaces3() throws IOException { public void testXMLWithoutNamespaceAnnotation() throws IOException { String xmlFileContent = Files.readString(sample31XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample31Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -527,7 +540,7 @@ public void testXMLWithoutNamespaceAnnotation() throws IOException { public void testXMLWithSameElementAndWithoutMultipleNamespaces() throws IOException { String xmlFileContent = Files.readString(sample32XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample32Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -536,7 +549,7 @@ public void testXMLWithSameElementAndWithoutMultipleNamespaces() throws IOExcept public void textXMLWithDefaultValueNode() throws IOException { String xmlFileContent = Files.readString(sample33XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample33Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -545,8 +558,35 @@ public void textXMLWithDefaultValueNode() throws IOException { public void textXMLWithDefaultValueNode2() throws IOException { String xmlFileContent = Files.readString(sample34XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - "__text", true).getCodeBlock().replaceAll("\\s+", ""); + "__text", true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample34Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } + + @Test(description = "textXMLWithoutAttributes") + public void textXMLWithoutAttributes() throws IOException { + String xmlFileContent = Files.readString(sample35XML); + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, + null, true, true, false).getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample35Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } + + @Test(description = "textXMLWithoutAttributeAnnotation") + public void textXMLWithoutAttributeAnnotation() throws IOException { + String xmlFileContent = Files.readString(sample35XML); + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, + "__text", false, false, true).getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample36Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } + + @Test(description = "textXMLWithoutMultipleAttributeAnnotation") + public void textXMLWithoutMultipleAttributeAnnotation() throws IOException { + String xmlFileContent = Files.readString(sample36XML); + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, + "__text", false, false, true).getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample37Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } } diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_35.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_35.bal new file mode 100644 index 000000000000..7e40d68e6076 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_35.bal @@ -0,0 +1,11 @@ +type Book record { + string title; + string author; + int year; + decimal price; +}; + +@xmldata:Name {value: "bookstore"} +type Bookstore record { + Book book; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_36.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_36.bal new file mode 100644 index 000000000000..30d9306ba750 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_36.bal @@ -0,0 +1,22 @@ +type Title record { + string __text; + string lang; +}; + +type Price record { + decimal __text; + string currency; +}; + +type Book record { + Title title; + string author; + int year; + Price price; + string category; +}; + +@xmldata:Name {value: "bookstore"} +type Bookstore record { + Book book; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_37.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_37.bal new file mode 100644 index 000000000000..de7054486b68 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_37.bal @@ -0,0 +1,37 @@ +type Book_Title record { + string __text; + string edition; + string format; + string lang; + string pages; +}; + +type Book_Price record { + decimal __text; + string currency; +}; + +type Book_Book record { + Book_Title title; + string author; + string publisher; + string publish_date; + int isbn; + Book_Price price; + string available; + string id; + string shelf; +}; + +type Books record { + string genre; + Book_Book book; +}; + +@xmldata:Name {value: "library"} +type Library record { + Books books; + string established; + string genre; + string location; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_35.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_35.xml new file mode 100644 index 000000000000..fb2f40571d64 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/xml/sample_35.xml @@ -0,0 +1,8 @@ + + + Harry Potter + J.K. Rowling + 2005 + 29.99 + + diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_36.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_36.xml new file mode 100644 index 000000000000..c0f3d434cd84 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/xml/sample_36.xml @@ -0,0 +1,13 @@ + + + Programming + + XML Developer's Guide + John Doe + Tech Press + 2000-10-01 + 1234567890 + 49.99 + + + From d5ae614d4d717735104f8073bf06f07be4e1c169 Mon Sep 17 00:00:00 2001 From: mindula Date: Thu, 4 Jul 2024 08:57:29 +0530 Subject: [PATCH 04/11] Address review suggestions --- .../XMLToRecordConverterTests.java | 11 +++++ .../test/resources/ballerina/sample_38.bal | 44 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 misc/xml-to-record-converter/src/test/resources/ballerina/sample_38.bal diff --git a/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java b/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java index 4e33124984d6..442c3ad1cbb9 100644 --- a/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java +++ b/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java @@ -225,6 +225,8 @@ public class XMLToRecordConverterTests { .resolve("sample_36.xml"); private final Path sample37Bal = RES_DIR.resolve(BAL_DIR) .resolve("sample_37.bal"); + private final Path sample38Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_38.bal"); private static final String XMLToRecordServiceEP = "xmlToRecord/convert"; @@ -589,4 +591,13 @@ public void textXMLWithoutMultipleAttributeAnnotation() throws IOException { String expectedCodeBlock = Files.readString(sample37Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } + + @Test(description = "textXMLWithNamespacesAndWithoutAttributeAnnotation") + public void textXMLWithNamespacesAndWithoutAttributeAnnotation() throws IOException { + String xmlFileContent = Files.readString(sample36XML); + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, + "__text", true, false, true).getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample38Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } } diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_38.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_38.bal new file mode 100644 index 000000000000..87853261f06e --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_38.bal @@ -0,0 +1,44 @@ +type Book_Title record { + string __text; + string edition; + string format; + string lang; + string pages; +}; + +type Book_Price record { + decimal __text; + string currency; +}; + +type Book_Book record { + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + Book_Title title; + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + string author; + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + string publisher; + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + string publish_date; + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + int isbn; + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + Book_Price price; + string available; + string id; + string shelf; +}; + +type Books record { + string genre; + @xmldata:Namespace {prefix: "book", uri: "http://example.com/books"} + Book_Book book; +}; + +@xmldata:Name {value: "library"} +type Library record { + Books books; + string established; + string genre; + string location; +}; From 2eec8760e8045c9035522cda946e019f4dad0d46 Mon Sep 17 00:00:00 2001 From: mindula Date: Wed, 21 Aug 2024 09:33:40 +0530 Subject: [PATCH 05/11] Rename test files --- .../XMLToRecordConverterTests.java | 42 +++++++++---------- .../{sample_35.bal => sample_39.bal} | 0 .../{sample_36.bal => sample_40.bal} | 0 .../{sample_37.bal => sample_41.bal} | 0 .../{sample_38.bal => sample_42.bal} | 0 .../xml/{sample_35.xml => sample_38.xml} | 0 .../xml/{sample_36.xml => sample_39.xml} | 0 7 files changed, 21 insertions(+), 21 deletions(-) rename misc/xml-to-record-converter/src/test/resources/ballerina/{sample_35.bal => sample_39.bal} (100%) rename misc/xml-to-record-converter/src/test/resources/ballerina/{sample_36.bal => sample_40.bal} (100%) rename misc/xml-to-record-converter/src/test/resources/ballerina/{sample_37.bal => sample_41.bal} (100%) rename misc/xml-to-record-converter/src/test/resources/ballerina/{sample_38.bal => sample_42.bal} (100%) rename misc/xml-to-record-converter/src/test/resources/xml/{sample_35.xml => sample_38.xml} (100%) rename misc/xml-to-record-converter/src/test/resources/xml/{sample_36.xml => sample_39.xml} (100%) diff --git a/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java b/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java index 442c3ad1cbb9..c2090ffc32c4 100644 --- a/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java +++ b/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java @@ -214,19 +214,19 @@ public class XMLToRecordConverterTests { private final Path sample34Bal = RES_DIR.resolve(BAL_DIR) .resolve("sample_34.bal"); - private final Path sample35XML = RES_DIR.resolve(XML_DIR) - .resolve("sample_35.xml"); - private final Path sample35Bal = RES_DIR.resolve(BAL_DIR) - .resolve("sample_35.bal"); - private final Path sample36Bal = RES_DIR.resolve(BAL_DIR) - .resolve("sample_36.bal"); - - private final Path sample36XML = RES_DIR.resolve(XML_DIR) - .resolve("sample_36.xml"); - private final Path sample37Bal = RES_DIR.resolve(BAL_DIR) - .resolve("sample_37.bal"); - private final Path sample38Bal = RES_DIR.resolve(BAL_DIR) - .resolve("sample_38.bal"); + private final Path sample38XML = RES_DIR.resolve(XML_DIR) + .resolve("sample_38.xml"); + private final Path sample39Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_39.bal"); + private final Path sample40Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_40.bal"); + + private final Path sample39XML = RES_DIR.resolve(XML_DIR) + .resolve("sample_39.xml"); + private final Path sample41Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_41.bal"); + private final Path sample42Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_42.bal"); private static final String XMLToRecordServiceEP = "xmlToRecord/convert"; @@ -567,37 +567,37 @@ public void textXMLWithDefaultValueNode2() throws IOException { @Test(description = "textXMLWithoutAttributes") public void textXMLWithoutAttributes() throws IOException { - String xmlFileContent = Files.readString(sample35XML); + String xmlFileContent = Files.readString(sample38XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, null, true, true, false).getCodeBlock().replaceAll("\\s+", ""); - String expectedCodeBlock = Files.readString(sample35Bal).replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample39Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @Test(description = "textXMLWithoutAttributeAnnotation") public void textXMLWithoutAttributeAnnotation() throws IOException { - String xmlFileContent = Files.readString(sample35XML); + String xmlFileContent = Files.readString(sample38XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, "__text", false, false, true).getCodeBlock().replaceAll("\\s+", ""); - String expectedCodeBlock = Files.readString(sample36Bal).replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample40Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @Test(description = "textXMLWithoutMultipleAttributeAnnotation") public void textXMLWithoutMultipleAttributeAnnotation() throws IOException { - String xmlFileContent = Files.readString(sample36XML); + String xmlFileContent = Files.readString(sample39XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, "__text", false, false, true).getCodeBlock().replaceAll("\\s+", ""); - String expectedCodeBlock = Files.readString(sample37Bal).replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample41Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @Test(description = "textXMLWithNamespacesAndWithoutAttributeAnnotation") public void textXMLWithNamespacesAndWithoutAttributeAnnotation() throws IOException { - String xmlFileContent = Files.readString(sample36XML); + String xmlFileContent = Files.readString(sample39XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, "__text", true, false, true).getCodeBlock().replaceAll("\\s+", ""); - String expectedCodeBlock = Files.readString(sample38Bal).replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample42Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } } diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_35.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_39.bal similarity index 100% rename from misc/xml-to-record-converter/src/test/resources/ballerina/sample_35.bal rename to misc/xml-to-record-converter/src/test/resources/ballerina/sample_39.bal diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_36.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_40.bal similarity index 100% rename from misc/xml-to-record-converter/src/test/resources/ballerina/sample_36.bal rename to misc/xml-to-record-converter/src/test/resources/ballerina/sample_40.bal diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_37.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_41.bal similarity index 100% rename from misc/xml-to-record-converter/src/test/resources/ballerina/sample_37.bal rename to misc/xml-to-record-converter/src/test/resources/ballerina/sample_41.bal diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_38.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_42.bal similarity index 100% rename from misc/xml-to-record-converter/src/test/resources/ballerina/sample_38.bal rename to misc/xml-to-record-converter/src/test/resources/ballerina/sample_42.bal diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_35.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_38.xml similarity index 100% rename from misc/xml-to-record-converter/src/test/resources/xml/sample_35.xml rename to misc/xml-to-record-converter/src/test/resources/xml/sample_38.xml diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_36.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_39.xml similarity index 100% rename from misc/xml-to-record-converter/src/test/resources/xml/sample_36.xml rename to misc/xml-to-record-converter/src/test/resources/xml/sample_39.xml From 6f9754c471ce0278a05215d3ded844540b920d59 Mon Sep 17 00:00:00 2001 From: mindula Date: Mon, 26 Aug 2024 09:31:30 +0530 Subject: [PATCH 06/11] Fix checkstyle failure --- .../ballerina/xmltorecordconverter/XMLToRecordConverter.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java index 123bf36b5f11..de665b58e170 100644 --- a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java +++ b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java @@ -527,7 +527,8 @@ private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode, boolean wi } annotations.add(getXMLAttributeNode()); NodeList annotationNodes = NodeFactory.createNodeList(annotations); - MetadataNode metadataNode = withoutAttributeAnnot ? null : NodeFactory.createMetadataNode(null, annotationNodes); + MetadataNode metadataNode = withoutAttributeAnnot ? null : + NodeFactory.createMetadataNode(null, annotationNodes); if (xmlAttributeNode.getPrefix() != null && xmlAttributeNode.getPrefix().equals(XMLNS_PREFIX)) { From 158855a3608cc489c552db2062728b251bd3874a Mon Sep 17 00:00:00 2001 From: hindujaB Date: Wed, 28 Aug 2024 14:31:33 +0530 Subject: [PATCH 07/11] Fix object resource call --- .../ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java index 2b3344d2627d..4c2e6551f476 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java @@ -202,9 +202,15 @@ public static String rewriteVirtualCallTypeName(String value, BType objectType) objectType = getImpliedType(objectType); // The call name will be in the format of`objectTypeName.funcName` for attached functions of imported modules. // Therefore, We need to remove the type name. + Name originalName = objectType.tsymbol.originalName; if (!objectType.tsymbol.name.value.isEmpty() && value.startsWith(objectType.tsymbol.name.value)) { value = value.replace(objectType.tsymbol.name.value + ".", "").trim(); } + // The call name will be in the format of`objectTypeOriginalName.funcName` for attached functions of + // object definitions. Therefore, We need to remove it. + if (originalName != null && !originalName.value.isEmpty() && value.startsWith(originalName.value)) { + value = value.replace(originalName.value + ".", "").trim(); + } return Utils.encodeFunctionIdentifier(value); } From 9c7be9b9f52968b98f15ff92bf03c36c40e076a0 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Wed, 28 Aug 2024 14:58:33 +0530 Subject: [PATCH 08/11] Add unit test --- .../test/object/ReadonlyObjectTest.java | 3 ++- .../test-src/object/readonly_objects.bal | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/object/ReadonlyObjectTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/object/ReadonlyObjectTest.java index 6069ca34238e..496ce1def87a 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/object/ReadonlyObjectTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/object/ReadonlyObjectTest.java @@ -50,7 +50,8 @@ public Object[] readOnlyObjectTests() { "testReadOnlyServiceClass", "testReadOnlyClassIntersectionWithMismatchedQualifiersRuntimeNegative", "testReadOnlyClassIntersectionWithValidQualifiers", - "testRecursiveObjectArrayReadonlyClone" + "testRecursiveObjectArrayReadonlyClone", + "testReadonlyObjectMethodCall" }; } diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/object/readonly_objects.bal b/tests/jballerina-unit-test/src/test/resources/test-src/object/readonly_objects.bal index 05a83a338408..862992621617 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/object/readonly_objects.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/object/readonly_objects.bal @@ -315,6 +315,21 @@ public function testRecursiveObjectArrayReadonlyClone() { assertTrue(x is readonly & Obj[]); } +public function testReadonlyObjectMethodCall() { + File file = new VirtualFile(); + string filename = file.filename(); + assertEquality("File Name", filename); +} + +public type File readonly & object { + public function filename() returns string; +}; + +public readonly class VirtualFile { + *File; + public function filename() returns string => "File Name"; +} + const ASSERTION_ERROR_REASON = "AssertionError"; function assertTrue(any|error actual) { From dce2c2f22abdb8d89929bb47b77e055f216e5bab Mon Sep 17 00:00:00 2001 From: hindujaB Date: Thu, 5 Sep 2024 12:41:50 +0530 Subject: [PATCH 09/11] Address review suggestions --- .../compiler/bir/codegen/JvmCodeGenUtil.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java index 4c2e6551f476..43eb90fe2268 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java @@ -200,16 +200,16 @@ public static String cleanupPathSeparators(String name) { public static String rewriteVirtualCallTypeName(String value, BType objectType) { objectType = getImpliedType(objectType); - // The call name will be in the format of`objectTypeName.funcName` for attached functions of imported modules. - // Therefore, We need to remove the type name. - Name originalName = objectType.tsymbol.originalName; - if (!objectType.tsymbol.name.value.isEmpty() && value.startsWith(objectType.tsymbol.name.value)) { - value = value.replace(objectType.tsymbol.name.value + ".", "").trim(); - } - // The call name will be in the format of`objectTypeOriginalName.funcName` for attached functions of - // object definitions. Therefore, We need to remove it. - if (originalName != null && !originalName.value.isEmpty() && value.startsWith(originalName.value)) { - value = value.replace(originalName.value + ".", "").trim(); + String typeName = objectType.tsymbol.name.value; + String originalName = objectType.tsymbol.originalName != null ? objectType.tsymbol.originalName.value : ""; + if (!typeName.isEmpty() && value.startsWith(typeName)) { + // The call name will be in the format of`objectTypeName.funcName` for attached functions of imported + // modules. Therefore, We need to remove the type name. + value = value.replace(typeName + ".", "").trim(); + } else if (value.startsWith(originalName)) { + // The call name will be in the format of`objectTypeOriginalName.funcName` for attached functions of + // object definitions. Therefore, We need to remove it. + value = value.replace(originalName + ".", "").trim(); } return Utils.encodeFunctionIdentifier(value); } From 963b5f55d2127f5910a26859584d4ca782838a5c Mon Sep 17 00:00:00 2001 From: hindujaB Date: Fri, 6 Sep 2024 13:49:24 +0530 Subject: [PATCH 10/11] Address suggestions --- .../ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java index 43eb90fe2268..56fad096cd1a 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java @@ -201,12 +201,12 @@ public static String cleanupPathSeparators(String name) { public static String rewriteVirtualCallTypeName(String value, BType objectType) { objectType = getImpliedType(objectType); String typeName = objectType.tsymbol.name.value; - String originalName = objectType.tsymbol.originalName != null ? objectType.tsymbol.originalName.value : ""; + Name originalName = objectType.tsymbol.originalName; if (!typeName.isEmpty() && value.startsWith(typeName)) { // The call name will be in the format of`objectTypeName.funcName` for attached functions of imported // modules. Therefore, We need to remove the type name. value = value.replace(typeName + ".", "").trim(); - } else if (value.startsWith(originalName)) { + } else if (originalName != null && value.startsWith(originalName.value)) { // The call name will be in the format of`objectTypeOriginalName.funcName` for attached functions of // object definitions. Therefore, We need to remove it. value = value.replace(originalName + ".", "").trim(); From c7be55e24b8c10c72cfcc39fc94735bc70bbb566 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Wed, 18 Sep 2024 14:31:13 +0530 Subject: [PATCH 11/11] Remove unnecessary check --- .../wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java index cbf065604206..9d98ea87c7f0 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCodeGenUtil.java @@ -203,7 +203,7 @@ public static String rewriteVirtualCallTypeName(String value, BType objectType) objectType = getImpliedType(objectType); String typeName = objectType.tsymbol.name.value; Name originalName = objectType.tsymbol.originalName; - if (!typeName.isEmpty() && value.startsWith(typeName)) { + if (value.startsWith(typeName)) { // The call name will be in the format of`objectTypeName.funcName` for attached functions of imported // modules. Therefore, We need to remove the type name. value = value.replace(typeName + ".", "").trim();