From 8a75273d96b051353220e5409ec38945232ff762 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Mon, 3 Apr 2023 12:46:25 +0530 Subject: [PATCH 01/28] Remove internal imports in StrandMetadata --- .../ballerina/runtime/api/async/Callback.java | 2 +- .../runtime/api/async/StrandMetadata.java | 24 +++++++++---------- .../api/constants/RuntimeConstants.java | 2 ++ .../runtime/api/constants/TypeConstants.java | 2 ++ .../internal/scheduling/AsyncUtils.java | 5 ++-- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/async/Callback.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/async/Callback.java index e8c18b8842dd..91a959f64336 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/async/Callback.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/async/Callback.java @@ -30,7 +30,7 @@ public interface Callback { /** * This should be called when you want to notify that your operation * is done successfully. - * @param result + * @param result the result to be reported when the operation passed */ void notifySuccess(Object result); diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/async/StrandMetadata.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/async/StrandMetadata.java index 88149d80b331..26384fd48bbe 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/async/StrandMetadata.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/async/StrandMetadata.java @@ -17,10 +17,8 @@ */ package io.ballerina.runtime.api.async; -import io.ballerina.runtime.internal.scheduling.Strand; - /** - * Holds metadata of the @{@link Strand}. + * Holds metadata of a Ballerina strand. * * @since 2.0.0 */ @@ -28,27 +26,27 @@ public class StrandMetadata { /** - * Organization name of module @{@link Strand} was initiated. + * Organization name of module the strand was initiated. */ private final String moduleOrg; /** - * Name of module @{@link Strand} was initiated. + * Name of module the strand was initiated. */ private final String moduleName; /** - * Version of module @{@link Strand} was initiated. + * Version of module the strand was initiated. */ private final String moduleVersion; /** - * Type name if @{@link Strand} was initiated inside type. + * Type name if the strand was initiated inside type. */ private final String typeName; /** - * Parent function name where @{@link Strand} was initiated. + * Parent function name where the strand was initiated. */ private final String parentFunctionName; @@ -66,7 +64,7 @@ public StrandMetadata(String moduleOrg, String moduleName, String moduleVersion, } /** - * Gets the organization name of module @{@link Strand} was initiated. + * Gets the organization name of module the strand was initiated. * * @return Strand module org name. */ @@ -75,7 +73,7 @@ public String getModuleOrg() { } /** - * Gets the name of module @{@link Strand} was initiated. + * Gets the name of module the strand was initiated. * * @return Strand module name. */ @@ -84,7 +82,7 @@ public String getModuleName() { } /** - * Gets the version of module @{@link Strand} was initiated. + * Gets the version of module the strand was initiated. * * @return Strand module version. */ @@ -93,7 +91,7 @@ public String getModuleVersion() { } /** - * Gets the type name if @{@link Strand} was initiated inside type. + * Gets the type name if the strand was initiated inside type. * * @return Strand type name. */ @@ -102,7 +100,7 @@ public String getTypeName() { } /** - * Gets the parent function name where @{@link Strand} was initiated. + * Gets the parent function name where the strand was initiated. * * @return Strand parent function name. */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/constants/RuntimeConstants.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/constants/RuntimeConstants.java index ef37994be034..a9264a0542a4 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/constants/RuntimeConstants.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/constants/RuntimeConstants.java @@ -151,4 +151,6 @@ public class RuntimeConstants { public static final String CURRENT_TRANSACTION_CONTEXT_PROPERTY = "currentTrxContext"; + private RuntimeConstants() {} + } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/constants/TypeConstants.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/constants/TypeConstants.java index 945cac53827c..b6fd6192248a 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/constants/TypeConstants.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/constants/TypeConstants.java @@ -87,4 +87,6 @@ public class TypeConstants { // Return type of the next function in iterators public static final String ITERATOR_NEXT_RETURN_TYPE = "$$returnType$$"; + + private TypeConstants() {} } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/AsyncUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/AsyncUtils.java index 30bc94f00721..f3b9c88b899d 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/AsyncUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/AsyncUtils.java @@ -81,8 +81,9 @@ public static CompletableFuture markAsync() { * @return Future Value */ public static FutureValue invokeFunctionPointerAsync(BFunctionPointer func, String strandName, - StrandMetadata metadata, Object[] args, Function resultHandleFunction, Scheduler scheduler) { + StrandMetadata metadata, Object[] args, + Function resultHandleFunction, + Scheduler scheduler) { Strand parent = Scheduler.getStrand(); AsyncFunctionCallback callback = new AsyncFunctionCallback(parent) { @Override From 74158594ce34eb34d23c7af74fac0a2c385a2536 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Mon, 3 Apr 2023 16:11:59 +0530 Subject: [PATCH 02/28] Remove internal imports in creator APIs --- .../runtime/api/creators/TypeCreator.java | 14 +++--- .../runtime/api/creators/ValueCreator.java | 44 +++++++++++-------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/TypeCreator.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/TypeCreator.java index 017bccbfcbb1..fc3b277ee195 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/TypeCreator.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/TypeCreator.java @@ -213,7 +213,7 @@ public static MapType createMapType(String typeName, Type constraint, Module mod } /** - * Create a {@code BRecordType} which represents the user defined record type. + * Create a {@code RecordType} which represents the user defined record type. * * @param typeName string name of the record type * @param module package of the record type @@ -228,7 +228,7 @@ public static RecordType createRecordType(String typeName, Module module, long f } /** - * Create a {@code BRecordType} which represents the user defined record type. + * Create a {@code RecordType} which represents the user defined record type. * * @param typeName string name of the record type * @param module package of the record type @@ -319,7 +319,7 @@ public static UnionType createUnionType(Type... memberTypes) { } /** - * Create a {@code BUnionType} which represents the union type. + * Create a {@code UnionType} which represents the union type. * * @param memberTypes of the union type * @return the new union type @@ -329,7 +329,7 @@ public static UnionType createUnionType(List memberTypes) { } /** - * Create a {@code BUnionType} which represents the union type. + * Create a {@code UnionType} which represents the union type. * * @param memberTypes of the union type * @param typeFlags flags associated with the type @@ -340,7 +340,7 @@ public static UnionType createUnionType(List memberTypes, int typeFlags) { } /** - * Create a {@code BUnionType} which represents the union type. + * Create a {@code UnionType} which represents the union type. * * @param memberTypes of the union type * @param readonly whether immutable @@ -351,7 +351,7 @@ public static UnionType createUnionType(List memberTypes, boolean readonly } /** - * Create a {@code BUnionType} which represents the union type. + * Create a {@code UnionType} which represents the union type. * * @param memberTypes of the union type * @param typeFlags flags associated with the type @@ -363,7 +363,7 @@ public static UnionType createUnionType(List memberTypes, int typeFlags, b } /** - * Create a {@code BUnionType} which represents the union type. + * Create a {@code UnionType} which represents the union type. * * @param memberTypes list of member types in the union type * @param name type name diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/ValueCreator.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/ValueCreator.java index 1804615a52fc..e3b6910d05d2 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/ValueCreator.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/ValueCreator.java @@ -297,22 +297,25 @@ public static BDecimal createDecimalValue(String value) { return new DecimalValue(value); } + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 /** * Create a decimal from given string and value kind. * * @param value string value * @param valueKind value kind * @return decimal value + * @deprecated use {@link #createDecimalValue(String)} instead. */ + @Deprecated public static BDecimal createDecimalValue(String value, DecimalValueKind valueKind) { return new DecimalValue(value, valueKind); } /** - * Create function pointer to the given function with given {@code BType}. + * Create function pointer to the given function with given {@code FunctionType}. * * @param function pointing function - * @param type {@code BFunctionType} of the function pointer + * @param type {@code FunctionType} of the function pointer * @return function pointer */ public static BFunctionPointer createFPValue(Function function, FunctionType type) { @@ -320,10 +323,10 @@ public static BFunctionPointer createFPValue(Function function, FunctionType typ } /** - * Create function pointer to the given function with given {@code BType}. + * Create function pointer to the given function with given {@code FunctionType}. * * @param function pointing function - * @param type {@code BFunctionType} of the function pointer + * @param type {@code FunctionType} of the function pointer * @param strandName name for newly creating strand which is used to run the function pointer * @return function pointer */ @@ -331,12 +334,15 @@ public static BFunctionPointer createFPValue(Function function, FunctionType typ return new FPValue(function, type, strandName, false); } + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 /** * Create {@code StreamingJsonValue} with given datasource. * * @param datasource {@code JSONDataSource} to be used * @return created {@code StreamingJsonValue} + * @deprecated */ + @Deprecated public static BStreamingJson createStreamingJsonValue(JsonDataSource datasource) { return new StreamingJsonValue(datasource); } @@ -365,7 +371,7 @@ public static BStream createStreamValue(StreamType type, BObject iteratorObj) { /** * Create a type descriptor value. * - * @param describingType {@code BType} of the value describe by this value + * @param describingType {@code Type} of the value describe by this value * @return type descriptor */ public static BTypedesc createTypedescValue(Type describingType) { @@ -373,73 +379,73 @@ public static BTypedesc createTypedescValue(Type describingType) { } /** - * Create an empty {@code XMLItem}. + * Create an empty {@code BXmlItem}. * - * @return {@code XMLItem} + * @return {@code BXmlItem} */ public static BXmlItem createXmlItem() { return new XmlItem(new QName(null), new XmlSequence()); } /** - * Create a {@code XMLItem} from a XML string. + * Create a {@code BXmlItem} from a XML string. * * @param name QName * @param children Xml sequence - * @return {@code XMLItem} + * @return {@code BXmlItem} */ public static BXmlItem createXmlItem(QName name, BXmlSequence children) { return new XmlItem(name, (XmlSequence) children); } /** - * Create a {@code XMLItem} from a XML string. + * Create a {@code BXmlItem} from a XML string. * * @param name QName - * @return {@code XMLItem} + * @return {@code BXmlItem} */ public static BXmlItem createXmlItem(QName name) { return new XmlItem(name); } /** - * Create a {@code XMLItem} from a XML string. + * Create a {@code BXmlItem} from a XML string. * * @param name QName * @param readonly Whether the element is immutable - * @return {@code XMLItem} + * @return {@code BXmlItem} */ public static BXmlItem createXmlItem(QName name, boolean readonly) { return new XmlItem(name, readonly); } /** - * Create a {@code XMLItem} from a XML string. + * Create a {@code BXmlItem} from a XML string. * * @param name QName * @param children Xml sequence * @param readonly Whether the element is immutable - * @return {@code XMLItem} + * @return {@code BXmlItem} */ public static BXmlItem createXmlItem(QName name, BXmlSequence children, boolean readonly) { return new XmlItem(name, (XmlSequence) children, readonly); } /** - * Create a {@code XMLItem} from a XML string. + * Create a {@code BXml} from a XML string. * * @param xmlValue A XML string - * @return {@code XMLItem} + * @return {@code BXml} */ public static BXml createXmlValue(String xmlValue) { return XmlFactory.parse(xmlValue); } /** - * Create a {@code XMLItem} from a {@link InputStream}. + * Create a {@code BXml} from a {@link InputStream}. * * @param inputStream Input Stream - * @return {@code XMLItem} + * @return {@code BXml} */ public static BXml createXmlValue(InputStream inputStream) { return XmlFactory.parse(inputStream); From 000083e093e3f5dc017e68923958b8ca1518c8f5 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Tue, 11 Apr 2023 15:54:09 +0530 Subject: [PATCH 03/28] Remove internal imports in types package --- .../main/java/io/ballerina/runtime/api/types/AnyType.java | 2 +- .../java/io/ballerina/runtime/api/types/AnydataType.java | 2 +- .../main/java/io/ballerina/runtime/api/types/ArrayType.java | 2 +- .../java/io/ballerina/runtime/api/types/BooleanType.java | 2 +- .../main/java/io/ballerina/runtime/api/types/ByteType.java | 2 +- .../java/io/ballerina/runtime/api/types/DecimalType.java | 2 +- .../main/java/io/ballerina/runtime/api/types/ErrorType.java | 2 +- .../src/main/java/io/ballerina/runtime/api/types/Field.java | 2 +- .../java/io/ballerina/runtime/api/types/FiniteType.java | 2 +- .../main/java/io/ballerina/runtime/api/types/FloatType.java | 2 +- .../java/io/ballerina/runtime/api/types/FunctionType.java | 2 +- .../java/io/ballerina/runtime/api/types/FutureType.java | 2 +- .../java/io/ballerina/runtime/api/types/HandleType.java | 3 ++- .../java/io/ballerina/runtime/api/types/IntegerType.java | 2 +- .../io/ballerina/runtime/api/types/IntersectionType.java | 2 +- .../java/io/ballerina/runtime/api/types/IteratorType.java | 2 +- .../main/java/io/ballerina/runtime/api/types/JsonType.java | 2 +- .../main/java/io/ballerina/runtime/api/types/MapType.java | 2 +- .../java/io/ballerina/runtime/api/types/MethodType.java | 6 +++--- .../main/java/io/ballerina/runtime/api/types/NeverType.java | 3 ++- .../main/java/io/ballerina/runtime/api/types/NullType.java | 2 +- .../java/io/ballerina/runtime/api/types/ObjectType.java | 2 +- .../main/java/io/ballerina/runtime/api/types/Parameter.java | 2 +- .../java/io/ballerina/runtime/api/types/ReadonlyType.java | 2 +- .../java/io/ballerina/runtime/api/types/RecordType.java | 2 +- .../io/ballerina/runtime/api/types/RemoteMethodType.java | 2 +- .../io/ballerina/runtime/api/types/ResourceMethodType.java | 3 +++ .../java/io/ballerina/runtime/api/types/StringType.java | 2 +- .../java/io/ballerina/runtime/api/types/StructureType.java | 2 +- .../main/java/io/ballerina/runtime/api/types/TableType.java | 2 +- .../main/java/io/ballerina/runtime/api/types/TupleType.java | 2 +- .../src/main/java/io/ballerina/runtime/api/types/Type.java | 3 +-- .../java/io/ballerina/runtime/api/types/TypedescType.java | 2 +- .../main/java/io/ballerina/runtime/api/types/UnionType.java | 2 +- .../io/ballerina/runtime/api/types/XmlAttributesType.java | 2 +- .../main/java/io/ballerina/runtime/api/types/XmlType.java | 2 +- .../io/ballerina/runtime/internal/types/BHandleType.java | 3 ++- .../io/ballerina/runtime/internal/types/BTypedescType.java | 2 +- 38 files changed, 45 insertions(+), 40 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/AnyType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/AnyType.java index 0d8b7cf15f6e..1e16ec7f673f 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/AnyType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/AnyType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BAnyType} represents any type in Ballerina. It is the root of the Ballerina type system. + * {@code AnyType} represents any type in Ballerina. It is the root of the Ballerina type system. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/AnydataType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/AnydataType.java index cdabd670d3ec..04e017ddb665 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/AnydataType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/AnydataType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BAnydataType} represents the data types in Ballerina. + * {@code AnydataType} represents the data types in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ArrayType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ArrayType.java index 777bdf12b139..f4b10a88da06 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ArrayType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ArrayType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BArrayType} represents a type of an arrays in Ballerina. + * {@code ArrayType} represents a type of an arrays in Ballerina. *

* Arrays are defined using the arrays constructor [] as follows: * TypeName[] diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/BooleanType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/BooleanType.java index fa8cf2e0fb45..a6f3b03854a6 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/BooleanType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/BooleanType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BBooleanType} represents boolean type in Ballerina. + * {@code BooleanType} represents boolean type in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ByteType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ByteType.java index ac1d8f4bc8ab..2af02f355dfa 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ByteType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ByteType.java @@ -19,7 +19,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BByteType} represents byte type in Ballerina. + * {@code ByteType} represents byte type in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/DecimalType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/DecimalType.java index 713d5b33f5b8..73173927de29 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/DecimalType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/DecimalType.java @@ -19,7 +19,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BDecimalType} represents decimal type in Ballerina. + * {@code DecimalType} represents decimal type in Ballerina. * This is a 128-bit decimal floating-point number according to the standard IEEE 754-2008 specifications. * * @since 2.0.0 diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ErrorType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ErrorType.java index 7ccd603458b2..8c12047dca01 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ErrorType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ErrorType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BErrorType} represents error type in Ballerina. + * {@code ErrorType} represents error type in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Field.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Field.java index 89f9a02b3e73..843de1bfaafe 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Field.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Field.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BField} represents a field in user defined type in Ballerina. + * {@code Field} represents a field in user defined type in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FiniteType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FiniteType.java index 936fa0e12719..577ac084fb53 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FiniteType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FiniteType.java @@ -21,7 +21,7 @@ import java.util.Set; /** - * {@code BFiniteType} represents the finite type in Ballerina. + * {@code FiniteType} represents the finite type in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FloatType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FloatType.java index 0af0272f627a..8b39dbbb8675 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FloatType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FloatType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BFloatType} represents a integer which is a 32-bit floating-point number according to the + * {@code FloatType} represents a integer which is a 32-bit floating-point number according to the * standard IEEE 754 specifications. * * @since 2.0.0 diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FunctionType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FunctionType.java index 107557f17599..31795cd7d126 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FunctionType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FunctionType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code {@link FunctionType }} represents a function type in ballerina. + * {@code FunctionType} represents a function type in ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FutureType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FutureType.java index 230078fc7971..c217d4bba2fd 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FutureType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/FutureType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BFutureType} represents a future value in Ballerina. + * {@code FutureType} represents a future value in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/HandleType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/HandleType.java index f94a888a19f5..18ca63e1f6f5 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/HandleType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/HandleType.java @@ -18,7 +18,8 @@ package io.ballerina.runtime.api.types; /** - * {@code BAnyType} represents any type in Ballerina. It is the root of the Ballerina type system. + * {@code HandleType} represents a handle type in Ballerina. + * A handle value is a reference to storage managed externally to a Ballerina program. * * @since 1.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/IntegerType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/IntegerType.java index 389085a635bf..8d79171f0ede 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/IntegerType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/IntegerType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BIntegerType} represents an integer which is a 32-bit signed number. + * {@code IntegerType} represents an integer which is a 32-bit signed number. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/IntersectionType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/IntersectionType.java index 67fadea24cb3..622b464b343c 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/IntersectionType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/IntersectionType.java @@ -20,7 +20,7 @@ import java.util.List; /** - * {@code BIntersectionType} represents an intersection type in Ballerina. + * {@code IntersectionType} represents an intersection type in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/IteratorType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/IteratorType.java index 091f2ee718b7..bf25ccb34929 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/IteratorType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/IteratorType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BIteratorType} represents the type of iterator in ballerina. + * {@code IteratorType} represents the type of iterator in ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/JsonType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/JsonType.java index 646d50eecba7..0ccb09fb7112 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/JsonType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/JsonType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BJSONType} represents a JSON value. + * {@code JSONType} represents a JSON value. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/MapType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/MapType.java index ee19d6825971..8ac5e80582a5 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/MapType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/MapType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BMapType} represents a type of a map in Ballerina. + * {@code MapType} represents a type of a map in Ballerina. *

* Maps are defined using the map keyword as follows: * map mapName diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/MethodType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/MethodType.java index 229068a52b15..1f416679836e 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/MethodType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/MethodType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code {@link MethodType }} represents a function type in ballerina. + * {@code MethodType} represents a function type in ballerina. * * @since 2.0.0 */ @@ -29,9 +29,9 @@ public interface MethodType extends FunctionType { FunctionType getType(); /** - * Provides given @{@link MethodType} method is isolated. + * Provides given {@link MethodType} method is isolated. * - * @return true if @{@link MethodType} method is isolated otherwise false. + * @return true if {@link MethodType} method is isolated otherwise false. */ boolean isIsolated(); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/NeverType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/NeverType.java index bbe71d5d182c..6385710ca32f 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/NeverType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/NeverType.java @@ -18,7 +18,8 @@ package io.ballerina.runtime.api.types; /** - * {@code BNeverType} represents the type of a {@code Never}. + * {@code NeverType} describes the a type that does not contain any shapes. + * No value ever belongs to a {@code NeverType}. * * @since 2.0.0-preview1 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/NullType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/NullType.java index 09a941c88049..103727750dda 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/NullType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/NullType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BNullType} represents the type of a {@code NullLiteral}. + * {@code NullType} represents the type of a {@code null} literal. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ObjectType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ObjectType.java index f10bc11b82be..7672ce17e361 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ObjectType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ObjectType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BObjectType} represents a user defined object type in Ballerina. + * {@code ObjectType} represents a user defined object type in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Parameter.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Parameter.java index c02be2092376..a438db0b6057 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Parameter.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Parameter.java @@ -19,7 +19,7 @@ package io.ballerina.runtime.api.types; /** - * {@code {@link Parameter } represents the parameter of a function in ballerina. + * {@code Parameter} represents the parameter of a function in ballerina. * * @since 2.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ReadonlyType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ReadonlyType.java index 590b848176c8..1d3a54ecaad5 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ReadonlyType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ReadonlyType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BReadonlyType} represents the shapes that have their read-only bit on. + * {@code ReadonlyType} represents the shapes that have their read-only bit on. * * @since 1.3.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/RecordType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/RecordType.java index 9ba9dff09d80..00f8bfbe935d 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/RecordType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/RecordType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BRecordType} represents a user defined record type in Ballerina. + * {@code RecordType} represents a user defined record type in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/RemoteMethodType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/RemoteMethodType.java index 2799cb62b2e1..cd0aeff2ff44 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/RemoteMethodType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/RemoteMethodType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code {@link RemoteMethodType }} represents remote function type in ballerina. + * {@code RemoteMethodType} represents remote function type in ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ResourceMethodType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ResourceMethodType.java index 041dfe526caf..86e1eb6931d6 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ResourceMethodType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/ResourceMethodType.java @@ -28,8 +28,11 @@ public interface ResourceMethodType extends MethodType { */ @Deprecated String[] getParamNames(); + String getAccessor(); + String[] getResourcePath(); + /* * @deprecated use {@link #getParameters()} instead. */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/StringType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/StringType.java index d8d762dc867f..be742f3a2069 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/StringType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/StringType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BStringType} represents a String type in ballerina. + * {@code StringType} represents a String type in ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/StructureType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/StructureType.java index 486d6d9be8ec..7ed99c5a3f7d 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/StructureType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/StructureType.java @@ -20,7 +20,7 @@ import java.util.Map; /** - * {@code BStructureType} represents a user defined structure type in Ballerina. + * {@code StructureType} represents a user defined structure type in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/TableType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/TableType.java index e7edde33c72b..16dc77239098 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/TableType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/TableType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BTableType} represents tabular data in Ballerina. + * {@code TableType} represents tabular data in Ballerina. * * @since 1.3.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/TupleType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/TupleType.java index 285f889e77f1..03e828ac23fa 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/TupleType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/TupleType.java @@ -19,7 +19,7 @@ import java.util.List; /** - * {@code {@link TupleType }} represents a tuple type in Ballerina. + * {@code TupleType} represents a tuple type in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Type.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Type.java index 88000f6d9a1c..54f15bf537a3 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Type.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Type.java @@ -20,7 +20,7 @@ import io.ballerina.runtime.api.Module; /** - * {@code BType} represents a type in Ballerina. + * {@code Type} represents a type in Ballerina. *

* Ballerina has variables of various types. The type system includes built-in primitive or value types, * a collection of built-in structured types, and arrays, record and iterator type constructors. @@ -31,7 +31,6 @@ */ public interface Type { - /** * Get the default value of the type. This is the value of an uninitialized variable of this type. * For value types, this is same as the value get from {@code BType#getInitValue()}. diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/TypedescType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/TypedescType.java index 45aeb0b0f810..394eb92af508 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/TypedescType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/TypedescType.java @@ -19,7 +19,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BTypeType} represents type of type in Ballerina type system. + * {@code TypedescType} represents a type of a type in the Ballerina type system. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/UnionType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/UnionType.java index 9a545281c89d..000b727e56f5 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/UnionType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/UnionType.java @@ -20,7 +20,7 @@ import java.util.List; /** - * {@code BUnionType} represents a union type in Ballerina. + * {@code UnionType} represents a union type in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/XmlAttributesType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/XmlAttributesType.java index 77f4d45e0555..239c20142e1c 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/XmlAttributesType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/XmlAttributesType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BXMLAttributesType} represents the type of an xml-attribute-map in ballerina. + * {@code XMLAttributesType} represents the type of an xml-attribute-map in ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/XmlType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/XmlType.java index 4f451a06f284..f250002e8636 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/XmlType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/XmlType.java @@ -18,7 +18,7 @@ package io.ballerina.runtime.api.types; /** - * {@code BXMLType} represents an XML Element. + * {@code XMLType} represents an XML Element. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BHandleType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BHandleType.java index b5adcf89a4d3..7de79e92454c 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BHandleType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BHandleType.java @@ -23,7 +23,8 @@ import io.ballerina.runtime.internal.values.RefValue; /** - * {@code BAnyType} represents any type in Ballerina. It is the root of the Ballerina type system. + * {@code BHandleType} represents a handle type in Ballerina. + * A handle value is a reference to storage managed externally to a Ballerina program. * * @since 1.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BTypedescType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BTypedescType.java index 18874690225c..a03a30737ad7 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BTypedescType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BTypedescType.java @@ -28,7 +28,7 @@ import io.ballerina.runtime.internal.values.TypedescValueImpl; /** - * {@code BTypeType} represents type of type in Ballerina type system. + * {@code BTypedescType} represents a type of a type in the Ballerina type system. * * @since 0.995.0 */ From 11b2ad70c18c585413c82ae7e64eb256f87b7aff Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Tue, 11 Apr 2023 16:55:41 +0530 Subject: [PATCH 04/28] Remove internal imports in utils package --- .../runtime/api/utils/JsonUtils.java | 31 +++++++++++++++++-- .../runtime/api/utils/StringUtils.java | 2 +- .../ballerinalang/langlib/value/ToJson.java | 4 +-- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/JsonUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/JsonUtils.java index 7e53e095adbe..ef334c3e248f 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/JsonUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/JsonUtils.java @@ -50,6 +50,7 @@ import java.io.Reader; import java.io.Writer; import java.nio.charset.Charset; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -59,7 +60,7 @@ import static io.ballerina.runtime.internal.util.exceptions.RuntimeErrors.INCOMPATIBLE_CONVERT_OPERATION; /** - * Class @{@link JsonParser} provides APIs to handle json values. + * Class {@link JsonParser} provides APIs to handle json values. * * @since 2.0.0 */ @@ -279,7 +280,31 @@ public static void serialize(Object json, Writer writer) throws BError { } } + /** + * Create a json value from the given source value. + * + * @param value source value + * @return json value + */ + public static Object convertToJson(Object value) { + return convertToJsonType(value, new ArrayList<>()); + } + + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 + /** + * Create a json value from the given source value. + * + * @param value source value + * @param unresolvedValues list of unresolved values + * @return json value + * @deprecated use {@link #convertToJson(Object)} instead. + */ + @Deprecated public static Object convertToJson(Object value, List unresolvedValues) { + return convertToJsonType(value, unresolvedValues); + } + + private static Object convertToJsonType(Object value, List unresolvedValues) { Type jsonType = PredefinedTypes.TYPE_JSON; if (value == null) { return null; @@ -359,7 +384,7 @@ private static Object convertMapToJson(BMap map, List unres BMap newMap = ValueCreator.createMapValue(TypeCreator.createMapType(PredefinedTypes.TYPE_JSON)); for (Map.Entry entry : map.entrySet()) { - Object newValue = convertToJson(entry.getValue(), unresolvedValues); + Object newValue = convertToJsonType(entry.getValue(), unresolvedValues); newMap.put(StringUtils.fromString(entry.getKey().toString()), newValue); } return newMap; @@ -368,7 +393,7 @@ private static Object convertMapToJson(BMap map, List unres private static Object convertArrayToJson(BArray array, List unresolvedValues) { BArray newArray = ValueCreator.createArrayValue((ArrayType) PredefinedTypes.TYPE_JSON_ARRAY); for (int i = 0; i < array.size(); i++) { - Object newValue = convertToJson(array.get(i), unresolvedValues); + Object newValue = convertToJsonType(array.get(i), unresolvedValues); newArray.add(i, newValue); } return newArray; diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java index 90e712456c4a..ba5266237d2e 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java @@ -355,7 +355,7 @@ public static Object parseExpressionStringValue(String value, BLink parent) thro * @return Json String value of the value */ public static String getJsonString(Object value) { - Object jsonValue = JsonUtils.convertToJson(value, new ArrayList<>()); + Object jsonValue = JsonUtils.convertToJson(value); Type type = TypeUtils.getReferredType(TypeChecker.getType(jsonValue)); switch (type.getTag()) { diff --git a/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/ToJson.java b/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/ToJson.java index 551f47095513..3228b216920f 100644 --- a/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/ToJson.java +++ b/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/ToJson.java @@ -19,8 +19,6 @@ import io.ballerina.runtime.api.utils.JsonUtils; -import java.util.ArrayList; - /** * Extern function lang.values:toJson. * Converts a value of type `anydata` to `json`. @@ -33,7 +31,7 @@ private ToJson() { } public static Object toJson(Object value) { - return JsonUtils.convertToJson(value, new ArrayList<>()); + return JsonUtils.convertToJson(value); } } From 4ddc3b244837876e312c3f75674698ac7b802894 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Wed, 12 Apr 2023 10:37:04 +0530 Subject: [PATCH 05/28] Deprecate StringUtils.getStringValue method --- .../runtime/api/utils/JsonUtils.java | 2 +- .../runtime/api/utils/StringUtils.java | 78 ++++--------- .../runtime/internal/FloatUtils.java | 2 +- .../runtime/internal/TableJsonDataSource.java | 2 +- .../runtime/internal/util/StringUtils.java | 105 ++++++++++++++++++ .../internal/values/ArrayValueImpl.java | 3 +- .../runtime/internal/values/ErrorValue.java | 3 +- .../runtime/internal/values/MapValueImpl.java | 3 +- .../internal/values/RegExpAtomQuantifier.java | 5 +- .../internal/values/RegExpDisjunction.java | 5 +- .../internal/values/TableValueImpl.java | 5 +- .../internal/values/TupleValueImpl.java | 3 +- .../ballerinalang/langlib/error/ToString.java | 2 +- .../langlib/regexp/RegexUtil.java | 2 +- .../ballerinalang/langlib/regexp/Split.java | 2 +- .../ballerinalang/langlib/value/ToString.java | 2 +- .../shell/service/util/TypeUtils.java | 2 +- .../testerina/natives/io/StringUtils.java | 2 +- .../benchmark/nativeimpl/Utils.java | 7 +- .../test/types/string/StringUtilsTest.java | 2 +- .../test/utils/interop/Utils.java | 4 +- 21 files changed, 156 insertions(+), 85 deletions(-) create mode 100644 bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/JsonUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/JsonUtils.java index ef334c3e248f..70a39dd0e422 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/JsonUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/JsonUtils.java @@ -334,7 +334,7 @@ private static Object getJsonObject(Object value, List unresolved case TypeTags.XML_PI_TAG: case TypeTags.XML_TEXT_TAG: case TypeTags.REG_EXP_TYPE_TAG: - newValue = StringUtils.fromString(StringUtils.getStringValue(value, null)); + newValue = StringUtils.fromString(StringUtils.getStringValue(value)); break; case TypeTags.TUPLE_TAG: case TypeTags.ARRAY_TAG: diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java index ba5266237d2e..37b3258bec52 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java @@ -24,9 +24,7 @@ import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BLink; -import io.ballerina.runtime.api.values.BObject; import io.ballerina.runtime.api.values.BString; -import io.ballerina.runtime.api.values.BValue; import io.ballerina.runtime.internal.BalStringUtils; import io.ballerina.runtime.internal.CycleUtils; import io.ballerina.runtime.internal.JsonGenerator; @@ -57,6 +55,9 @@ import java.util.Set; import static io.ballerina.runtime.api.constants.RuntimeConstants.STRING_LANG_LIB; +import static io.ballerina.runtime.internal.util.StringUtils.STR_CYCLE; +import static io.ballerina.runtime.internal.util.StringUtils.TO_STRING; +import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INDEX_OUT_OF_RANGE_ERROR_IDENTIFIER; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.getModulePrefixedReason; @@ -67,9 +68,6 @@ */ public class StringUtils { - private static final String STR_CYCLE = "..."; - public static final String TO_STRING = "toString"; - /** * Convert input stream to String. * @@ -163,57 +161,24 @@ public static BArray fromStringSet(Set set) { * Returns the human-readable string value of Ballerina values. * * @param value The value on which the function is invoked - * @param parent The link to the parent node - * @return String value of the value + * @return String value of the provided value */ - public static String getStringValue(Object value, BLink parent) { - if (value == null) { - return ""; - } - - Type type = TypeUtils.getReferredType(TypeChecker.getType(value)); - - if (type.getTag() == TypeTags.STRING_TAG) { - return ((BString) value).getValue(); - } - - if (type.getTag() < TypeTags.NULL_TAG) { - return String.valueOf(value); - } - - CycleUtils.Node node = new CycleUtils.Node(value, parent); - - if (node.hasCyclesSoFar()) { - return STR_CYCLE; - } - - if (type.getTag() == TypeTags.MAP_TAG || type.getTag() == TypeTags.RECORD_TYPE_TAG) { - MapValueImpl mapValue = (MapValueImpl) value; - return mapValue.stringValue(parent); - } - - if (type.getTag() == TypeTags.ARRAY_TAG || type.getTag() == TypeTags.TUPLE_TAG) { - ArrayValue arrayValue = (ArrayValue) value; - return arrayValue.stringValue(parent); - } - - if (type.getTag() == TypeTags.TABLE_TAG) { - return ((RefValue) value).informalStringValue(parent); - } - - if (type.getTag() == TypeTags.OBJECT_TYPE_TAG) { - BObject objectValue = (BObject) value; - ObjectType objectType = (ObjectType) TypeUtils.getReferredType(objectValue.getType()); - for (MethodType func : objectType.getMethods()) { - if (func.getName().equals(TO_STRING) && func.getParameters().length == 0 && - func.getType().getReturnType().getTag() == TypeTags.STRING_TAG) { - return objectValue.call(Scheduler.getStrand(), TO_STRING).toString(); - } - } - } + public static String getStringValue(Object value) { + return getStringVal(value, null); + } - BValue bValue = (BValue) value; - return bValue.stringValue(parent); + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 + /** + * Returns the human-readable string value of Ballerina values. + * + * @param value The value on which the function is invoked + * @param parent The link to the parent node + * @return String value of the value + * @deprecated use {@link #getStringValue(Object)} instead. + */ + @Deprecated + public static String getStringValue(Object value, BLink parent) { + return getStringVal(value, parent); } /** @@ -283,11 +248,6 @@ public static String getExpressionStringValue(Object value, BLink parent) { } } - if (type.getTag() == TypeTags.ERROR_TAG) { - RefValue errorValue = (RefValue) value; - return errorValue.expressionStringValue(parent); - } - RefValue refValue = (RefValue) value; return refValue.expressionStringValue(parent); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/FloatUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/FloatUtils.java index 564189b20b49..2f3229eed662 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/FloatUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/FloatUtils.java @@ -30,7 +30,7 @@ public class FloatUtils { public static BString getBStringIfInfiniteOrNaN(double x) { if (Double.isInfinite(x) || Double.isNaN(x)) { - return StringUtils.fromString(StringUtils.getStringValue(x, null)); + return StringUtils.fromString(StringUtils.getStringValue(x)); } return null; } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TableJsonDataSource.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TableJsonDataSource.java index d7c6057a2488..46bf72499fcd 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TableJsonDataSource.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TableJsonDataSource.java @@ -163,7 +163,7 @@ private static void constructJsonData(BMap record, MapValue @@ -158,7 +159,7 @@ private String getDetailsToString(BLink parent) { sj.add(key + "=" + ((BValue) value).informalStringValue(parent)); break; default: - sj.add(key + "=" + StringUtils.getStringValue(value, parent)); + sj.add(key + "=" + getStringVal(value, parent)); break; } } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java index 569081ef1678..c063c7d08e29 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java @@ -66,6 +66,7 @@ import static io.ballerina.runtime.api.utils.TypeUtils.getReferredType; import static io.ballerina.runtime.internal.JsonInternalUtils.mergeJson; import static io.ballerina.runtime.internal.ValueUtils.getTypedescValue; +import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INVALID_UPDATE_ERROR_IDENTIFIER; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.MAP_KEY_NOT_FOUND_ERROR; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.getModulePrefixedReason; @@ -487,7 +488,7 @@ public String stringValue(BLink parent) { sj.add("\"" + key + "\":" + ((BValue) value).informalStringValue(mapParent)); break; default: - sj.add("\"" + key + "\":" + StringUtils.getStringValue(value, mapParent)); + sj.add("\"" + key + "\":" + getStringVal(value, mapParent)); break; } } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/RegExpAtomQuantifier.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/RegExpAtomQuantifier.java index 7f2c01d16345..5e400ffa8a6a 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/RegExpAtomQuantifier.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/RegExpAtomQuantifier.java @@ -17,11 +17,12 @@ */ package io.ballerina.runtime.internal.values; -import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.values.BLink; import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.internal.regexp.RegExpFactory; +import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; + /** *

* Represents ReAtom [ReQuantifier] in regular expression. @@ -72,6 +73,6 @@ private void validateInsertion(BString insertion) { @Override public String stringValue(BLink parent) { - return StringUtils.getStringValue(this.reAtom, parent) + this.reQuantifier.stringValue(parent); + return getStringVal(this.reAtom, parent) + this.reQuantifier.stringValue(parent); } } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/RegExpDisjunction.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/RegExpDisjunction.java index 072ed7da9e62..96f0cbfb0cf5 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/RegExpDisjunction.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/RegExpDisjunction.java @@ -17,11 +17,12 @@ */ package io.ballerina.runtime.internal.values; -import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.values.BLink; import java.util.StringJoiner; +import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; + /** *

* Represents an ReDisjunction in regular expression. @@ -58,7 +59,7 @@ public String stringValue(BLink parent) { terms.add(((String) t)); continue; } - terms.add(StringUtils.getStringValue(t, parent)); + terms.add(getStringVal(t, parent)); } return terms.toString(); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TableValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TableValueImpl.java index 4dd249d056bd..d83415cc6953 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TableValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TableValueImpl.java @@ -65,6 +65,7 @@ import static io.ballerina.runtime.api.constants.RuntimeConstants.TABLE_LANG_LIB; import static io.ballerina.runtime.internal.ValueUtils.getTypedescValue; +import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INHERENT_TYPE_VIOLATION_ERROR_IDENTIFIER; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.OPERATION_NOT_SUPPORTED_ERROR; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.TABLE_HAS_A_VALUE_FOR_KEY_ERROR; @@ -399,11 +400,11 @@ private String createStringValueDataEntry(Iterator>> itr Map.Entry> struct = itr.next(); if (struct.getValue().size() > 1) { for (V data: struct.getValue()) { - sj.add(StringUtils.getStringValue(data, + sj.add(getStringVal(data, new CycleUtils.Node(this, parent))); } } else { - sj.add(StringUtils.getStringValue(struct.getValue().get(0), + sj.add(getStringVal(struct.getValue().get(0), new CycleUtils.Node(this, parent))); } } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TupleValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TupleValueImpl.java index 922f935f2a34..57a0a6cd7734 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TupleValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TupleValueImpl.java @@ -48,6 +48,7 @@ import static io.ballerina.runtime.api.constants.RuntimeConstants.ARRAY_LANG_LIB; import static io.ballerina.runtime.internal.ValueUtils.getTypedescValue; +import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INDEX_OUT_OF_RANGE_ERROR_IDENTIFIER; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INHERENT_TYPE_VIOLATION_ERROR_IDENTIFIER; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.getModulePrefixedReason; @@ -456,7 +457,7 @@ public String stringValue(BLink parent) { sj.add("null"); break; default: - sj.add(StringUtils.getStringValue(value, new CycleUtils.Node(this, parentNode))); + sj.add(getStringVal(value, new CycleUtils.Node(this, parentNode))); break; } } diff --git a/langlib/lang.error/src/main/java/org/ballerinalang/langlib/error/ToString.java b/langlib/lang.error/src/main/java/org/ballerinalang/langlib/error/ToString.java index b871096990f8..ef0462541b8b 100644 --- a/langlib/lang.error/src/main/java/org/ballerinalang/langlib/error/ToString.java +++ b/langlib/lang.error/src/main/java/org/ballerinalang/langlib/error/ToString.java @@ -29,6 +29,6 @@ */ public class ToString { public static BString toString(BError value) { - return StringUtils.fromString(StringUtils.getStringValue(value, null)); + return StringUtils.fromString(StringUtils.getStringValue(value)); } } diff --git a/langlib/lang.regexp/src/main/java/org/ballerinalang/langlib/regexp/RegexUtil.java b/langlib/lang.regexp/src/main/java/org/ballerinalang/langlib/regexp/RegexUtil.java index fb9085d8790d..4580a93b8357 100644 --- a/langlib/lang.regexp/src/main/java/org/ballerinalang/langlib/regexp/RegexUtil.java +++ b/langlib/lang.regexp/src/main/java/org/ballerinalang/langlib/regexp/RegexUtil.java @@ -61,7 +61,7 @@ static Matcher getMatcher(BRegexpValue regexpVal, BString inputStr) { static Matcher getMatcher(BRegexpValue regexpVal, String inputStr) { // Map the required ballerina regexp constructs to java. RegExpValue translatedRegExpVal = RegExpFactory.translateRegExpConstructs((RegExpValue) regexpVal); - String patternStr = StringUtils.getStringValue(translatedRegExpVal, null); + String patternStr = StringUtils.getStringValue(translatedRegExpVal); Pattern pattern = Pattern.compile(patternStr); return pattern.matcher(inputStr); } diff --git a/langlib/lang.regexp/src/main/java/org/ballerinalang/langlib/regexp/Split.java b/langlib/lang.regexp/src/main/java/org/ballerinalang/langlib/regexp/Split.java index b7ddfa978ae6..f9c8a0f80e9b 100644 --- a/langlib/lang.regexp/src/main/java/org/ballerinalang/langlib/regexp/Split.java +++ b/langlib/lang.regexp/src/main/java/org/ballerinalang/langlib/regexp/Split.java @@ -35,7 +35,7 @@ public class Split { public static BArray split(BRegexpValue regExp, BString str) { String originalString = str.getValue(); RegExpValue translatedRegExpVal = RegExpFactory.translateRegExpConstructs((RegExpValue) regExp); - String regex = StringUtils.getStringValue(translatedRegExpVal, null); + String regex = StringUtils.getStringValue(translatedRegExpVal); String[] splitStrArr = originalString.split(regex, -1); return StringUtils.fromStringArray(splitStrArr); } diff --git a/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/ToString.java b/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/ToString.java index b3ef7ac97117..f67dc9a04810 100644 --- a/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/ToString.java +++ b/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/ToString.java @@ -28,6 +28,6 @@ */ public class ToString { public static BString toString(Object value) { - return StringUtils.fromString(StringUtils.getStringValue(value, null)); + return StringUtils.fromString(StringUtils.getStringValue(value)); } } diff --git a/misc/ls-extensions/modules/bal-shell-service/src/main/java/io/ballerina/shell/service/util/TypeUtils.java b/misc/ls-extensions/modules/bal-shell-service/src/main/java/io/ballerina/shell/service/util/TypeUtils.java index 0bc266f10f51..1d1f56d15073 100644 --- a/misc/ls-extensions/modules/bal-shell-service/src/main/java/io/ballerina/shell/service/util/TypeUtils.java +++ b/misc/ls-extensions/modules/bal-shell-service/src/main/java/io/ballerina/shell/service/util/TypeUtils.java @@ -77,7 +77,7 @@ public static String convertToJsonIfAcceptable(Object value) { case TypeTags.XML_PI_TAG: case TypeTags.XML_TEXT_TAG: case TypeTags.OBJECT_TYPE_TAG: - return StringUtils.getStringValue(value, null); + return StringUtils.getStringValue(value); default: return StringUtils.getExpressionStringValue(value, null); } diff --git a/misc/testerina/modules/testerina-core/src/main/java/org/ballerinalang/testerina/natives/io/StringUtils.java b/misc/testerina/modules/testerina-core/src/main/java/org/ballerinalang/testerina/natives/io/StringUtils.java index c5ff40c66c54..35fc29fc3acf 100644 --- a/misc/testerina/modules/testerina-core/src/main/java/org/ballerinalang/testerina/natives/io/StringUtils.java +++ b/misc/testerina/modules/testerina-core/src/main/java/org/ballerinalang/testerina/natives/io/StringUtils.java @@ -215,7 +215,7 @@ public static BString sprintf(BString format, Object... args) { case 's': if (ref != null) { result.append(String.format("%" + padding + "s", - io.ballerina.runtime.api.utils.StringUtils.getStringValue(ref, null))); + io.ballerina.runtime.api.utils.StringUtils.getStringValue(ref))); } break; case '%': diff --git a/tests/jballerina-benchmark-test/src/main/java/org/ballerinalang/benchmark/nativeimpl/Utils.java b/tests/jballerina-benchmark-test/src/main/java/org/ballerinalang/benchmark/nativeimpl/Utils.java index 84f3fa7c57c8..126469ccc9da 100644 --- a/tests/jballerina-benchmark-test/src/main/java/org/ballerinalang/benchmark/nativeimpl/Utils.java +++ b/tests/jballerina-benchmark-test/src/main/java/org/ballerinalang/benchmark/nativeimpl/Utils.java @@ -48,7 +48,7 @@ public static void print(Object... values) { } for (Object value : values) { if (value != null) { - out.print(StringUtils.getStringValue(value, null)); + out.print(StringUtils.getStringValue(value)); } } } @@ -62,7 +62,7 @@ public static void println(Object... values) { StringBuilder content = new StringBuilder(); for (Object value : values) { if (value != null) { - content.append(StringUtils.getStringValue(value, null)); + content.append(StringUtils.getStringValue(value)); } } out.println(content); @@ -112,8 +112,7 @@ public static BString sprintf(BString format, Object... args) { break; case 's': if (ref != null) { - result.append(String.format("%" + padding + "s", StringUtils.getStringValue(ref, - null))); + result.append(String.format("%" + padding + "s", StringUtils.getStringValue(ref))); } break; case '%': diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/string/StringUtilsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/string/StringUtilsTest.java index 60909a8f097e..a29d1e29047c 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/string/StringUtilsTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/string/StringUtilsTest.java @@ -47,7 +47,7 @@ public static BString getStringVal(Object... values) { StringBuilder content = new StringBuilder(); for (Object value : values) { if (value != null) { - content.append(StringUtils.getStringValue(value, null)); + content.append(StringUtils.getStringValue(value)); } } return StringUtils.fromString(content.toString()); diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/utils/interop/Utils.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/utils/interop/Utils.java index bf82b6363025..54ffa6f1e70b 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/utils/interop/Utils.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/utils/interop/Utils.java @@ -50,7 +50,7 @@ public static void print(Object... values) { } for (Object value : values) { if (value != null) { - out.print(StringUtils.getStringValue(value, null)); + out.print(StringUtils.getStringValue(value)); } } } @@ -64,7 +64,7 @@ public static void println(Object... values) { StringBuilder content = new StringBuilder(); for (Object value : values) { if (value != null) { - content.append(StringUtils.getStringValue(value, null)); + content.append(StringUtils.getStringValue(value)); } } out.println(content); From 627d63267dd02fc55cb7e8e9ae3e6d1a578e0912 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Wed, 12 Apr 2023 11:26:58 +0530 Subject: [PATCH 06/28] Deprecate StringUtils.getExpressionStringValue --- .../io/ballerina/shell/utils/StringUtils.java | 2 +- .../runtime/api/utils/StringUtils.java | 84 ++++--------------- .../runtime/internal/util/StringUtils.java | 75 ++++++++++++++++- .../internal/values/ArrayValueImpl.java | 16 ++-- .../runtime/internal/values/ErrorValue.java | 4 +- .../runtime/internal/values/MapValueImpl.java | 3 +- .../internal/values/TableValueImpl.java | 7 +- .../internal/values/TupleValueImpl.java | 4 +- .../langlib/error/ToBalString.java | 2 +- .../langlib/value/ToBalString.java | 2 +- .../shell/service/util/TypeUtils.java | 2 +- 11 files changed, 108 insertions(+), 93 deletions(-) diff --git a/ballerina-shell/modules/shell-core/src/main/java/io/ballerina/shell/utils/StringUtils.java b/ballerina-shell/modules/shell-core/src/main/java/io/ballerina/shell/utils/StringUtils.java index 03454b21705b..00bf4493a5c2 100644 --- a/ballerina-shell/modules/shell-core/src/main/java/io/ballerina/shell/utils/StringUtils.java +++ b/ballerina-shell/modules/shell-core/src/main/java/io/ballerina/shell/utils/StringUtils.java @@ -149,7 +149,7 @@ public static String encodeIdentifier(String string) { * @return Converted string. */ public static String getExpressionStringValue(Object object) { - return io.ballerina.runtime.api.utils.StringUtils.getExpressionStringValue(object, null); + return io.ballerina.runtime.api.utils.StringUtils.getExpressionStringValue(object); } /** diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java index 37b3258bec52..2cb75f61b853 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java @@ -18,30 +18,24 @@ import io.ballerina.runtime.api.TypeTags; import io.ballerina.runtime.api.creators.ErrorCreator; -import io.ballerina.runtime.api.types.MethodType; -import io.ballerina.runtime.api.types.ObjectType; import io.ballerina.runtime.api.types.Type; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BLink; import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.internal.BalStringUtils; -import io.ballerina.runtime.internal.CycleUtils; import io.ballerina.runtime.internal.JsonGenerator; import io.ballerina.runtime.internal.TypeChecker; import io.ballerina.runtime.internal.regexp.RegExpFactory; -import io.ballerina.runtime.internal.scheduling.Scheduler; import io.ballerina.runtime.internal.util.exceptions.BLangExceptionHelper; import io.ballerina.runtime.internal.util.exceptions.BallerinaException; import io.ballerina.runtime.internal.util.exceptions.RuntimeErrors; -import io.ballerina.runtime.internal.values.AbstractObjectValue; import io.ballerina.runtime.internal.values.ArrayValue; import io.ballerina.runtime.internal.values.ArrayValueImpl; import io.ballerina.runtime.internal.values.BmpStringValue; import io.ballerina.runtime.internal.values.DecimalValue; import io.ballerina.runtime.internal.values.MapValueImpl; import io.ballerina.runtime.internal.values.NonBmpStringValue; -import io.ballerina.runtime.internal.values.RefValue; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; @@ -55,8 +49,7 @@ import java.util.Set; import static io.ballerina.runtime.api.constants.RuntimeConstants.STRING_LANG_LIB; -import static io.ballerina.runtime.internal.util.StringUtils.STR_CYCLE; -import static io.ballerina.runtime.internal.util.StringUtils.TO_STRING; +import static io.ballerina.runtime.internal.util.StringUtils.getExpressionStringVal; import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INDEX_OUT_OF_RANGE_ERROR_IDENTIFIER; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.getModulePrefixedReason; @@ -181,75 +174,28 @@ public static String getStringValue(Object value, BLink parent) { return getStringVal(value, parent); } + /** + * Returns the string value of Ballerina values in expression style. + * + * @param value The value on which the function is invoked + * @return String value of the value in expression style + */ + public static String getExpressionStringValue(Object value) { + return getExpressionStringVal(value, null); + } + + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 /** * Returns the string value of Ballerina values in expression style. * * @param value The value on which the function is invoked * @param parent The link to the parent node * @return String value of the value in expression style + * @deprecated use {@link #getExpressionStringValue(Object)} instead. */ + @Deprecated public static String getExpressionStringValue(Object value, BLink parent) { - if (value == null) { - return "()"; - } - - Type type = TypeUtils.getReferredType(TypeChecker.getType(value)); - - if (type.getTag() == TypeTags.STRING_TAG) { - return "\"" + ((BString) value).getValue() + "\""; - } - - if (type.getTag() == TypeTags.DECIMAL_TAG) { - DecimalValue decimalValue = (DecimalValue) value; - return decimalValue.expressionStringValue(parent); - } - - if (type.getTag() == TypeTags.FLOAT_TAG) { - if (Double.isNaN((Double) value)) { - return "float:" + value; - } - if (Double.isInfinite((Double) value)) { - return "float:" + value; - } - } - - if (type.getTag() < TypeTags.NULL_TAG) { - return String.valueOf(value); - } - - CycleUtils.Node node = new CycleUtils.Node(value, parent); - - if (node.hasCyclesSoFar()) { - return STR_CYCLE + "[" + node.getIndex() + "]"; - } - - if (type.getTag() == TypeTags.MAP_TAG || type.getTag() == TypeTags.RECORD_TYPE_TAG) { - MapValueImpl mapValue = (MapValueImpl) value; - return mapValue.expressionStringValue(parent); - } - - if (type.getTag() == TypeTags.ARRAY_TAG || type.getTag() == TypeTags.TUPLE_TAG) { - ArrayValue arrayValue = (ArrayValue) value; - return arrayValue.expressionStringValue(parent); - } - - if (type.getTag() == TypeTags.TABLE_TAG) { - return ((RefValue) value).expressionStringValue(parent); - } - - if (type.getTag() == TypeTags.OBJECT_TYPE_TAG) { - AbstractObjectValue objectValue = (AbstractObjectValue) value; - ObjectType objectType = (ObjectType) TypeUtils.getReferredType(objectValue.getType()); - for (MethodType func : objectType.getMethods()) { - if (func.getName().equals(TO_STRING) && func.getParameters().length == 0 && - func.getType().getReturnType().getTag() == TypeTags.STRING_TAG) { - return "object " + objectValue.call(Scheduler.getStrand(), TO_STRING).toString(); - } - } - } - - RefValue refValue = (RefValue) value; - return refValue.expressionStringValue(parent); + return getExpressionStringVal(value, parent); } /** diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java index 9df6ce4b986b..0b779bdd60b0 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java @@ -30,7 +30,9 @@ import io.ballerina.runtime.internal.CycleUtils; import io.ballerina.runtime.internal.TypeChecker; import io.ballerina.runtime.internal.scheduling.Scheduler; +import io.ballerina.runtime.internal.values.AbstractObjectValue; import io.ballerina.runtime.internal.values.ArrayValue; +import io.ballerina.runtime.internal.values.DecimalValue; import io.ballerina.runtime.internal.values.MapValueImpl; import io.ballerina.runtime.internal.values.RefValue; @@ -44,6 +46,8 @@ public class StringUtils { public static final String STR_CYCLE = "..."; public static final String TO_STRING = "toString"; + private StringUtils() {} + /** * Returns the human-readable string value of Ballerina values. * @@ -101,5 +105,74 @@ public static String getStringVal(Object value, BLink parent) { return bValue.stringValue(parent); } - private StringUtils() {} + /** + * Returns the string value of Ballerina values in expression style. + * + * @param value The value on which the function is invoked + * @param parent The link to the parent node + * @return String value of the value in expression style + */ + public static String getExpressionStringVal(Object value, BLink parent) { + if (value == null) { + return "()"; + } + + Type type = TypeUtils.getReferredType(TypeChecker.getType(value)); + + if (type.getTag() == TypeTags.STRING_TAG) { + return "\"" + ((BString) value).getValue() + "\""; + } + + if (type.getTag() == TypeTags.DECIMAL_TAG) { + DecimalValue decimalValue = (DecimalValue) value; + return decimalValue.expressionStringValue(parent); + } + + if (type.getTag() == TypeTags.FLOAT_TAG) { + if (Double.isNaN((Double) value)) { + return "float:" + value; + } + if (Double.isInfinite((Double) value)) { + return "float:" + value; + } + } + + if (type.getTag() < TypeTags.NULL_TAG) { + return String.valueOf(value); + } + + CycleUtils.Node node = new CycleUtils.Node(value, parent); + + if (node.hasCyclesSoFar()) { + return STR_CYCLE + "[" + node.getIndex() + "]"; + } + + if (type.getTag() == TypeTags.MAP_TAG || type.getTag() == TypeTags.RECORD_TYPE_TAG) { + MapValueImpl mapValue = (MapValueImpl) value; + return mapValue.expressionStringValue(parent); + } + + if (type.getTag() == TypeTags.ARRAY_TAG || type.getTag() == TypeTags.TUPLE_TAG) { + ArrayValue arrayValue = (ArrayValue) value; + return arrayValue.expressionStringValue(parent); + } + + if (type.getTag() == TypeTags.TABLE_TAG) { + return ((RefValue) value).expressionStringValue(parent); + } + + if (type.getTag() == TypeTags.OBJECT_TYPE_TAG) { + AbstractObjectValue objectValue = (AbstractObjectValue) value; + ObjectType objectType = (ObjectType) TypeUtils.getReferredType(objectValue.getType()); + for (MethodType func : objectType.getMethods()) { + if (func.getName().equals(TO_STRING) && func.getParameters().length == 0 && + func.getType().getReturnType().getTag() == TypeTags.STRING_TAG) { + return "object " + objectValue.call(Scheduler.getStrand(), TO_STRING).toString(); + } + } + } + + RefValue refValue = (RefValue) value; + return refValue.expressionStringValue(parent); + } } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java index 546a60b6ca96..25f06a3646b2 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java @@ -53,6 +53,7 @@ import static io.ballerina.runtime.api.constants.RuntimeConstants.ARRAY_LANG_LIB; import static io.ballerina.runtime.internal.ValueUtils.getTypedescValue; +import static io.ballerina.runtime.internal.util.StringUtils.getExpressionStringVal; import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INDEX_OUT_OF_RANGE_ERROR_IDENTIFIER; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INHERENT_TYPE_VIOLATION_ERROR_IDENTIFIER; @@ -719,14 +720,12 @@ public String expressionStringValue(BLink parent) { case TypeTags.UNSIGNED16_INT_TAG: case TypeTags.UNSIGNED8_INT_TAG: for (int i = 0; i < size; i++) { - sj.add(StringUtils.getExpressionStringValue(intValues[i], - new CycleUtils.Node(this, parent))); + sj.add(getExpressionStringVal(intValues[i], new CycleUtils.Node(this, parent))); } break; case TypeTags.BOOLEAN_TAG: for (int i = 0; i < size; i++) { - sj.add(StringUtils.getExpressionStringValue(booleanValues[i], - new CycleUtils.Node(this, parent))); + sj.add(getExpressionStringVal(booleanValues[i], new CycleUtils.Node(this, parent))); } break; case TypeTags.BYTE_TAG: @@ -736,21 +735,18 @@ public String expressionStringValue(BLink parent) { break; case TypeTags.FLOAT_TAG: for (int i = 0; i < size; i++) { - sj.add(StringUtils.getExpressionStringValue(floatValues[i], - new CycleUtils.Node(this, parent))); + sj.add(getExpressionStringVal(floatValues[i], new CycleUtils.Node(this, parent))); } break; case TypeTags.STRING_TAG: case TypeTags.CHAR_STRING_TAG: for (int i = 0; i < size; i++) { - sj.add(StringUtils.getExpressionStringValue(bStringValues[i], - new CycleUtils.Node(this, parent))); + sj.add(getExpressionStringVal(bStringValues[i], new CycleUtils.Node(this, parent))); } break; default: for (int i = 0; i < size; i++) { - sj.add(StringUtils.getExpressionStringValue(refValues[i], - new CycleUtils.Node(this, parent))); + sj.add(getExpressionStringVal(refValues[i], new CycleUtils.Node(this, parent))); } break; } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ErrorValue.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ErrorValue.java index f231c599a630..2ca7b88f14b7 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ErrorValue.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ErrorValue.java @@ -24,7 +24,6 @@ import io.ballerina.runtime.api.constants.TypeConstants; import io.ballerina.runtime.api.types.Type; import io.ballerina.runtime.api.types.TypeId; -import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BLink; @@ -49,6 +48,7 @@ import static io.ballerina.runtime.api.constants.RuntimeConstants.BLANG_SRC_FILE_SUFFIX; import static io.ballerina.runtime.api.constants.RuntimeConstants.DOT; import static io.ballerina.runtime.api.constants.RuntimeConstants.MODULE_INIT_CLASS_NAME; +import static io.ballerina.runtime.internal.util.StringUtils.getExpressionStringVal; import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; /** @@ -175,7 +175,7 @@ private String getDetailsToBalString(BLink parent) { StringJoiner sj = new StringJoiner(","); for (Object key : ((MapValue) details).getKeys()) { Object value = ((MapValue) details).get(key); - sj.add(key + "=" + StringUtils.getExpressionStringValue(value, parent)); + sj.add(key + "=" + getExpressionStringVal(value, parent)); } return "," + sj; } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java index c063c7d08e29..405eea6b1952 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java @@ -66,6 +66,7 @@ import static io.ballerina.runtime.api.utils.TypeUtils.getReferredType; import static io.ballerina.runtime.internal.JsonInternalUtils.mergeJson; import static io.ballerina.runtime.internal.ValueUtils.getTypedescValue; +import static io.ballerina.runtime.internal.util.StringUtils.getExpressionStringVal; import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INVALID_UPDATE_ERROR_IDENTIFIER; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.MAP_KEY_NOT_FOUND_ERROR; @@ -504,7 +505,7 @@ public String expressionStringValue(BLink parent) { K key = kvEntry.getKey(); V value = kvEntry.getValue(); CycleUtils.Node mapParent = new CycleUtils.Node(this, node); - sj.add("\"" + key + "\":" + StringUtils.getExpressionStringValue(value, mapParent)); + sj.add("\"" + key + "\":" + getExpressionStringVal(value, mapParent)); } return "{" + sj.toString() + "}"; } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TableValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TableValueImpl.java index d83415cc6953..9041caee9ed8 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TableValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TableValueImpl.java @@ -65,6 +65,7 @@ import static io.ballerina.runtime.api.constants.RuntimeConstants.TABLE_LANG_LIB; import static io.ballerina.runtime.internal.ValueUtils.getTypedescValue; +import static io.ballerina.runtime.internal.util.StringUtils.getExpressionStringVal; import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INHERENT_TYPE_VIOLATION_ERROR_IDENTIFIER; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.OPERATION_NOT_SUPPORTED_ERROR; @@ -422,12 +423,10 @@ private String createExpressionStringValueDataEntry(Iterator> struct = itr.next(); if (struct.getValue().size() > 1) { for (V data: struct.getValue()) { - sj.add(StringUtils.getExpressionStringValue(data, - new CycleUtils.Node(this, parent))); + sj.add(getExpressionStringVal(data, new CycleUtils.Node(this, parent))); } } else { - sj.add(StringUtils.getExpressionStringValue(struct.getValue().get(0), - new CycleUtils.Node(this, parent))); + sj.add(getExpressionStringVal(struct.getValue().get(0), new CycleUtils.Node(this, parent))); } } return "table key(" + keyJoiner + ") [" + sj + "]"; diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TupleValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TupleValueImpl.java index 57a0a6cd7734..bd3eda84d883 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TupleValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TupleValueImpl.java @@ -21,7 +21,6 @@ import io.ballerina.runtime.api.creators.ErrorCreator; import io.ballerina.runtime.api.types.TupleType; import io.ballerina.runtime.api.types.Type; -import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BIterator; @@ -48,6 +47,7 @@ import static io.ballerina.runtime.api.constants.RuntimeConstants.ARRAY_LANG_LIB; import static io.ballerina.runtime.internal.ValueUtils.getTypedescValue; +import static io.ballerina.runtime.internal.util.StringUtils.getExpressionStringVal; import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INDEX_OUT_OF_RANGE_ERROR_IDENTIFIER; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INHERENT_TYPE_VIOLATION_ERROR_IDENTIFIER; @@ -468,7 +468,7 @@ public String stringValue(BLink parent) { public String expressionStringValue(BLink parent) { StringJoiner sj = new StringJoiner(","); for (int i = 0; i < this.size; i++) { - sj.add(StringUtils.getExpressionStringValue(this.refValues[i], new CycleUtils.Node(this, parent))); + sj.add(getExpressionStringVal(this.refValues[i], new CycleUtils.Node(this, parent))); } return "[" + sj + "]"; } diff --git a/langlib/lang.error/src/main/java/org/ballerinalang/langlib/error/ToBalString.java b/langlib/lang.error/src/main/java/org/ballerinalang/langlib/error/ToBalString.java index b8e5a360e097..65929cdd3e1c 100644 --- a/langlib/lang.error/src/main/java/org/ballerinalang/langlib/error/ToBalString.java +++ b/langlib/lang.error/src/main/java/org/ballerinalang/langlib/error/ToBalString.java @@ -29,6 +29,6 @@ */ public class ToBalString { public static BString toBalString(BError value) { - return StringUtils.fromString(StringUtils.getExpressionStringValue(value, null)); + return StringUtils.fromString(StringUtils.getExpressionStringValue(value)); } } diff --git a/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/ToBalString.java b/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/ToBalString.java index ab37839feefc..6754dfffc4a8 100644 --- a/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/ToBalString.java +++ b/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/ToBalString.java @@ -28,6 +28,6 @@ */ public class ToBalString { public static BString toBalString(Object value) { - return StringUtils.fromString(StringUtils.getExpressionStringValue(value, null)); + return StringUtils.fromString(StringUtils.getExpressionStringValue(value)); } } diff --git a/misc/ls-extensions/modules/bal-shell-service/src/main/java/io/ballerina/shell/service/util/TypeUtils.java b/misc/ls-extensions/modules/bal-shell-service/src/main/java/io/ballerina/shell/service/util/TypeUtils.java index 1d1f56d15073..e3ba81083045 100644 --- a/misc/ls-extensions/modules/bal-shell-service/src/main/java/io/ballerina/shell/service/util/TypeUtils.java +++ b/misc/ls-extensions/modules/bal-shell-service/src/main/java/io/ballerina/shell/service/util/TypeUtils.java @@ -79,7 +79,7 @@ public static String convertToJsonIfAcceptable(Object value) { case TypeTags.OBJECT_TYPE_TAG: return StringUtils.getStringValue(value); default: - return StringUtils.getExpressionStringValue(value, null); + return StringUtils.getExpressionStringValue(value); } } } From d6ae1c83dfce8ea6ae89a75232c65216fcdb86c3 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Wed, 12 Apr 2023 12:28:13 +0530 Subject: [PATCH 07/28] Deprecate StringUtils.parseExpressionStringValue --- .../runtime/api/utils/StringUtils.java | 79 ++++++------------- .../runtime/internal/BalStringUtils.java | 9 ++- .../runtime/internal/util/StringUtils.java | 60 ++++++++++++++ .../langlib/value/FromBalString.java | 2 +- 4 files changed, 91 insertions(+), 59 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java index 2cb75f61b853..55a327037f56 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java @@ -23,17 +23,14 @@ import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BLink; import io.ballerina.runtime.api.values.BString; -import io.ballerina.runtime.internal.BalStringUtils; import io.ballerina.runtime.internal.JsonGenerator; import io.ballerina.runtime.internal.TypeChecker; -import io.ballerina.runtime.internal.regexp.RegExpFactory; import io.ballerina.runtime.internal.util.exceptions.BLangExceptionHelper; import io.ballerina.runtime.internal.util.exceptions.BallerinaException; import io.ballerina.runtime.internal.util.exceptions.RuntimeErrors; import io.ballerina.runtime.internal.values.ArrayValue; import io.ballerina.runtime.internal.values.ArrayValueImpl; import io.ballerina.runtime.internal.values.BmpStringValue; -import io.ballerina.runtime.internal.values.DecimalValue; import io.ballerina.runtime.internal.values.MapValueImpl; import io.ballerina.runtime.internal.values.NonBmpStringValue; @@ -49,8 +46,10 @@ import java.util.Set; import static io.ballerina.runtime.api.constants.RuntimeConstants.STRING_LANG_LIB; +import static io.ballerina.runtime.api.creators.ErrorCreator.createError; import static io.ballerina.runtime.internal.util.StringUtils.getExpressionStringVal; import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; +import static io.ballerina.runtime.internal.util.StringUtils.parseExpressionStringVal; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INDEX_OUT_OF_RANGE_ERROR_IDENTIFIER; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.getModulePrefixedReason; @@ -188,9 +187,9 @@ public static String getExpressionStringValue(Object value) { /** * Returns the string value of Ballerina values in expression style. * - * @param value The value on which the function is invoked - * @param parent The link to the parent node - * @return String value of the value in expression style + * @param value The value on which the function is invoked + * @param parent The link to the parent node + * @return String value of the value in expression style * @deprecated use {@link #getExpressionStringValue(Object)} instead. */ @Deprecated @@ -202,56 +201,28 @@ public static String getExpressionStringValue(Object value, BLink parent) { * Returns the Ballerina value represented by Ballerina expression syntax. * * @param value The value on which the function is invoked - * @return Ballerina value represented by Ballerina expression syntax - * @throws BError for any parsing error + * @return Ballerina value represented by Ballerina expression syntax + * @throws BError for any parsing error */ - public static Object parseExpressionStringValue(String value, BLink parent) throws BallerinaException { - String exprValue = value.trim(); - int endIndex = exprValue.length() - 1; - if (exprValue.equals("()")) { - return null; - } - if (exprValue.startsWith("\"") && exprValue.endsWith("\"")) { - return StringUtils.fromString(exprValue.substring(1, endIndex)); - } - if (exprValue.matches("[+-]?[0-9][0-9]*")) { - return Long.parseLong(exprValue); - } - if (exprValue.equals("float:Infinity") || exprValue.equals("float:NaN")) { - return Double.parseDouble(exprValue.substring(6)); - } - if (exprValue.matches("[+-]?[0-9]+([.][0-9]+)?([Ee][+-]?[0-9]+)?")) { - return Double.parseDouble(exprValue); - } - if (exprValue.matches("[+-]?[0-9]+(.[0-9]+)?([Ee][+-]?[0-9]+)?[d]")) { - return new DecimalValue(exprValue.substring(0, endIndex)); - } - if (exprValue.equals("true") || exprValue.equals("false")) { - return Boolean.parseBoolean(exprValue); - } - if (exprValue.startsWith("[") && exprValue.endsWith("]")) { - return BalStringUtils.parseArrayExpressionStringValue(exprValue, parent); - } - if (exprValue.startsWith("{") && exprValue.endsWith("}")) { - return BalStringUtils.parseMapExpressionStringValue(exprValue, parent); - } - if (exprValue.startsWith("table key")) { - return BalStringUtils.parseTableExpressionStringValue(exprValue, parent); - } - if (exprValue.startsWith("xml")) { - String xml = exprValue.substring(exprValue.indexOf('`') + 1, - exprValue.lastIndexOf('`')).trim(); - return BalStringUtils.parseXmlExpressionStringValue(xml); - } - if (exprValue.startsWith("re")) { - String regexp = exprValue.substring(exprValue.indexOf('`') + 1, - exprValue.lastIndexOf('`')).trim(); - return RegExpFactory.parse(regexp); - } - if (exprValue.startsWith("...")) { - return BalStringUtils.parseCycleDetectedExpressionStringValue(exprValue, parent); + public static Object parseExpressionStringValue(String value) throws BError { + try { + return parseExpressionStringVal(value, null); + } catch (BallerinaException e) { + throw createError(e); } - throw new BallerinaException("invalid expression style string value"); + } + + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 + /** + * Returns the Ballerina value represented by Ballerina expression syntax. + * + * @param value The value on which the function is invoked + * @return Ballerina value represented by Ballerina expression syntax + * @deprecated use {@link #parseExpressionStringValue(String)} instead. + */ + @Deprecated + public static Object parseExpressionStringValue(String value, BLink parent) throws BallerinaException { + return parseExpressionStringVal(value, parent); } /** diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalStringUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalStringUtils.java index dabc45e63d28..c1d8b7197a3b 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalStringUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalStringUtils.java @@ -41,6 +41,7 @@ import java.util.Set; import static io.ballerina.runtime.api.PredefinedTypes.TYPE_ANYDATA; +import static io.ballerina.runtime.internal.util.StringUtils.parseExpressionStringVal; /** * Common utility methods used for Ballerina expression syntax manipulation. @@ -68,7 +69,7 @@ public static Object parseArrayExpressionStringValue(String exprValue, BLink par Set typeSet = new HashSet<>(); for (int i = 0; i < list.size(); i++) { String e = list.get(i); - Object val = StringUtils.parseExpressionStringValue(e, node); + Object val = parseExpressionStringVal(e, node); Type type = TypeChecker.getType(val); typeSet.add(type); arr.add(i, val); @@ -163,7 +164,7 @@ public static Object parseMapExpressionStringValue(String exprValue, BLink paren } String key = e.substring(1, colonIndex - 1); String value = e.substring(colonIndex + 1); - Object val = StringUtils.parseExpressionStringValue(value, node); + Object val = parseExpressionStringVal(value, node); eleMap.put(StringUtils.fromString(key), val); Type type = TypeChecker.getType(val); typeSet.add(type); @@ -197,8 +198,8 @@ public static Object parseTableExpressionStringValue(String exprValue, BLink par ArrayValue keyFieldNames = keys[0].isEmpty() ? (ArrayValue) ValueCreator.createArrayValue(new BString[]{}) : (ArrayValue) StringUtils.fromStringArray(keys); // start index of table members string = index of ')' + 2 - ArrayValueImpl data = (ArrayValueImpl) StringUtils.parseExpressionStringValue(exprValue.substring - (exprValue.indexOf(')') + 2), parent); + ArrayValueImpl data = (ArrayValueImpl) parseExpressionStringVal(exprValue.substring(exprValue.indexOf(')') + 2), + parent); MapType mapType = TypeCreator.createMapType(TYPE_ANYDATA, false); BTableType tableType; diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java index 0b779bdd60b0..5cddb6daca7a 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java @@ -23,13 +23,17 @@ import io.ballerina.runtime.api.types.ObjectType; import io.ballerina.runtime.api.types.Type; import io.ballerina.runtime.api.utils.TypeUtils; +import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BLink; import io.ballerina.runtime.api.values.BObject; import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.api.values.BValue; +import io.ballerina.runtime.internal.BalStringUtils; import io.ballerina.runtime.internal.CycleUtils; import io.ballerina.runtime.internal.TypeChecker; +import io.ballerina.runtime.internal.regexp.RegExpFactory; import io.ballerina.runtime.internal.scheduling.Scheduler; +import io.ballerina.runtime.internal.util.exceptions.BallerinaException; import io.ballerina.runtime.internal.values.AbstractObjectValue; import io.ballerina.runtime.internal.values.ArrayValue; import io.ballerina.runtime.internal.values.DecimalValue; @@ -175,4 +179,60 @@ public static String getExpressionStringVal(Object value, BLink parent) { RefValue refValue = (RefValue) value; return refValue.expressionStringValue(parent); } + + /** + * Returns the Ballerina value represented by Ballerina expression syntax. + * + * @param value The value on which the function is invoked + * @return Ballerina value represented by Ballerina expression syntax + * @throws BError for any parsing error + */ + public static Object parseExpressionStringVal(String value, BLink parent) throws BallerinaException { + String exprValue = value.trim(); + int endIndex = exprValue.length() - 1; + if (exprValue.equals("()")) { + return null; + } + if (exprValue.startsWith("\"") && exprValue.endsWith("\"")) { + return io.ballerina.runtime.api.utils.StringUtils.fromString(exprValue.substring(1, endIndex)); + } + if (exprValue.matches("[+-]?[0-9][0-9]*")) { + return Long.parseLong(exprValue); + } + if (exprValue.equals("float:Infinity") || exprValue.equals("float:NaN")) { + return Double.parseDouble(exprValue.substring(6)); + } + if (exprValue.matches("[+-]?[0-9]+([.][0-9]+)?([Ee][+-]?[0-9]+)?")) { + return Double.parseDouble(exprValue); + } + if (exprValue.matches("[+-]?[0-9]+(.[0-9]+)?([Ee][+-]?[0-9]+)?[d]")) { + return new DecimalValue(exprValue.substring(0, endIndex)); + } + if (exprValue.equals("true") || exprValue.equals("false")) { + return Boolean.parseBoolean(exprValue); + } + if (exprValue.startsWith("[") && exprValue.endsWith("]")) { + return BalStringUtils.parseArrayExpressionStringValue(exprValue, parent); + } + if (exprValue.startsWith("{") && exprValue.endsWith("}")) { + return BalStringUtils.parseMapExpressionStringValue(exprValue, parent); + } + if (exprValue.startsWith("table key")) { + return BalStringUtils.parseTableExpressionStringValue(exprValue, parent); + } + if (exprValue.startsWith("xml")) { + String xml = exprValue.substring(exprValue.indexOf('`') + 1, + exprValue.lastIndexOf('`')).trim(); + return BalStringUtils.parseXmlExpressionStringValue(xml); + } + if (exprValue.startsWith("re")) { + String regexp = exprValue.substring(exprValue.indexOf('`') + 1, + exprValue.lastIndexOf('`')).trim(); + return RegExpFactory.parse(regexp); + } + if (exprValue.startsWith("...")) { + return BalStringUtils.parseCycleDetectedExpressionStringValue(exprValue, parent); + } + throw new BallerinaException("invalid expression style string value"); + } } diff --git a/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/FromBalString.java b/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/FromBalString.java index 0b464a298b0f..32db342951e9 100644 --- a/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/FromBalString.java +++ b/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/FromBalString.java @@ -38,7 +38,7 @@ public static Object fromBalString(BString value) { return null; } try { - return StringUtils.parseExpressionStringValue(str, null); + return StringUtils.parseExpressionStringValue(str); } catch (BallerinaException e) { return ErrorCreator.createError(FROM_BAL_STRING_ERROR, StringUtils.fromString(e.getMessage())); } catch (BError bError) { From 1ed5f5d4cee845c57b9275dd83b10a92db0caed0 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Wed, 12 Apr 2023 12:38:53 +0530 Subject: [PATCH 08/28] Remove internal imports in TypeUtils and XmlUtils --- .../io/ballerina/runtime/api/utils/TypeUtils.java | 3 +-- .../io/ballerina/runtime/api/utils/XmlUtils.java | 13 +++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/TypeUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/TypeUtils.java index 59360d9ab29a..f5ecb68e002c 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/TypeUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/TypeUtils.java @@ -24,7 +24,6 @@ import io.ballerina.runtime.internal.TypeChecker; import io.ballerina.runtime.internal.types.BArrayType; import io.ballerina.runtime.internal.types.BFiniteType; -import io.ballerina.runtime.internal.types.BType; import static io.ballerina.runtime.api.PredefinedTypes.TYPE_ANY; import static io.ballerina.runtime.api.PredefinedTypes.TYPE_ANYDATA; @@ -46,7 +45,7 @@ import static io.ballerina.runtime.api.PredefinedTypes.TYPE_XML_ATTRIBUTES; /** - * This class contains various methods manipulate {@link BType}s in Ballerina. + * This class contains various methods to manipulate {@link Type}s in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/XmlUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/XmlUtils.java index 9aebeb112ac2..ea7731763dd4 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/XmlUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/XmlUtils.java @@ -25,18 +25,19 @@ import io.ballerina.runtime.internal.XmlFactory; import io.ballerina.runtime.internal.XmlValidator; import io.ballerina.runtime.internal.values.TableValueImpl; -import io.ballerina.runtime.internal.values.XmlQName; import java.io.InputStream; import java.io.Reader; /** - * Class @{@link XmlUtils} provides APIs to handle xml values. + * Class {@link XmlUtils} provides APIs to handle xml values. * * @since 2.0.0 */ public class XmlUtils { + private XmlUtils() {} + /** * Create a XML item from string literal. * @@ -89,9 +90,9 @@ public static BXml parse(Reader reader) { } /** - * Converts a {@link io.ballerina.runtime.internal.values.TableValue} to {@link BXml}. + * Converts a {@link BTable} to {@link BXml}. * - * @param table {@link io.ballerina.runtime.internal.values.TableValue} to convert + * @param table {@link BTable} to convert * @return converted {@link BXml} */ public static BXml parse(BTable table) { @@ -100,14 +101,14 @@ public static BXml parse(BTable table) { /** *

- * Validate a {@link XmlQName} against the XSD definition. + * Validate a {@link BXmlQName} against the XSD definition. *

* * NCName ::= (Letter | '_') (NCNameChar)* * NCNameChar ::= Letter | Digit | '.' | '-' | '_' | CombiningChar | Extender * * - * @param qname {@link XmlQName} to check the validity + * @param qname {@link BXmlQName} to check the validity * @throws BError if invalid XML qname */ public static void validateXmlQName(BXmlQName qname) throws BError { From 14a1ca799b38e24ac0e67987d2fc3ac473770b26 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Mon, 17 Apr 2023 11:47:25 +0530 Subject: [PATCH 09/28] Remove internal imports in values package --- .../io/ballerina/runtime/api/values/BArray.java | 2 +- .../ballerina/runtime/api/values/BCollection.java | 2 +- .../io/ballerina/runtime/api/values/BDecimal.java | 9 ++++++--- .../io/ballerina/runtime/api/values/BFuture.java | 13 ++++++++----- .../io/ballerina/runtime/api/values/BIterator.java | 2 +- .../java/io/ballerina/runtime/api/values/BMap.java | 9 ++++----- .../ballerina/runtime/api/values/BRegexpValue.java | 2 ++ .../io/ballerina/runtime/api/values/BStream.java | 4 ++-- .../runtime/api/values/BStreamingJson.java | 4 +--- .../io/ballerina/runtime/api/values/BTable.java | 9 ++++----- .../io/ballerina/runtime/api/values/BTypedesc.java | 8 ++++---- .../ballerina/runtime/api/values/BXmlSequence.java | 2 +- .../runtime/internal/values/MapValueImpl.java | 4 ++-- 13 files changed, 37 insertions(+), 33 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BArray.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BArray.java index c7d407290822..efc1e70cb995 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BArray.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BArray.java @@ -222,7 +222,7 @@ public interface BArray extends BRefValue, BCollection { /** - * Get {@code BType} of the array elements. + * Get {@code Type} of the array elements. * @return element type */ Type getElementType(); diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BCollection.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BCollection.java index 625ff52e8bbc..a95834a70d6e 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BCollection.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BCollection.java @@ -19,7 +19,7 @@ /** *

- * {@code {@link BCollection}} represents a collection in Ballerina. + * {@link BCollection} represents a collection in Ballerina. *

* * @since 1.1.0 diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BDecimal.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BDecimal.java index 6e03a5e27c54..1c7f4acc4aba 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BDecimal.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BDecimal.java @@ -78,7 +78,7 @@ public interface BDecimal { BigDecimal value(); /** - * Returns the {@code BType} of the value. + * Returns the {@code Type} of the value. * * @return the type */ @@ -135,11 +135,14 @@ public interface BDecimal { */ BDecimal negate(); + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 /** - * Returns value kind of {@code (-this)}. + * Returns value kind of {@code this}. * - * @return value kind + * @return value kind + * @deprecated use {@link #decimalValue()} instead. */ + @Deprecated DecimalValueKind getValueKind(); /** diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BFuture.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BFuture.java index 860a9fa3b6ef..47c6fc9cad4d 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BFuture.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BFuture.java @@ -30,16 +30,19 @@ public interface BFuture extends BValue { /** - * Abort execution of the {@code Strand} that the future is attached. + * Abort execution of the Ballerina strand that the future is attached. * The abortion occurs only after the next yield point. */ void cancel(); + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 /** * Returns the strand that the future is attached to. * - * @return {@code Strand} + * @return strand + * @deprecated */ + @Deprecated Strand getStrand(); /** @@ -50,7 +53,7 @@ public interface BFuture extends BValue { Object getResult(); /** - * Returns completion status of the {@code Strand} that the future is attached. + * Returns completion status of the Ballerina strand that the future is attached. * * @return true if future is completed */ @@ -64,9 +67,9 @@ public interface BFuture extends BValue { Throwable getPanic(); /** - * {@code CallableUnitCallback} listening on the completion of this future. + * Returns {@link Callback} listening on the completion of this future. * - * @return registered {@code CallableUnitCallback} + * @return registered {@link Callback} */ Callback getCallback(); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BIterator.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BIterator.java index edfca7b8b2c7..f4e8d8ae4247 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BIterator.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BIterator.java @@ -19,7 +19,7 @@ /** *

- * Represents an iterator of a Ballerina {@code {@link BCollection}}. + * Represents an iterator of a Ballerina {@link BCollection}. *

* * @param The type of elements returned by this iterator diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BMap.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BMap.java index a988f2c74eb9..fb6409176fe7 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BMap.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BMap.java @@ -18,7 +18,6 @@ package io.ballerina.runtime.api.values; import io.ballerina.runtime.api.types.Type; -import io.ballerina.runtime.internal.util.exceptions.BallerinaException; import java.util.Collection; import java.util.Map; @@ -116,19 +115,19 @@ public interface BMap extends BRefValue, BCollection { void clear(); /** - * Returns the value to which the specified key is mapped. A {@link BallerinaException} will be thrown + * Returns the value to which the specified key is mapped. A {@link BError} will be thrown * if the key does not exists. * * @param key the key whose associated value is to be returned * @return the value to which the specified key is mapped - * @throws BallerinaException if the key does not exists + * @throws BError if the key does not exists */ V getOrThrow(Object key); /** * Returns the value for the given key from map. If the key does not exist, but there exists a filler - * value for the expected type, a new value will be created and added and then returned. A {@link - * BallerinaException} will be thrown if the key does not exists and a filler value does not exist. + * value for the expected type, a new value will be created and added and then returned. A {@link BError} + * will be thrown if the key does not exists and a filler value does not exist. * * @param key key used to get the value * @return value associated with the key diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BRegexpValue.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BRegexpValue.java index d53019d5a79f..765ce7a04c30 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BRegexpValue.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BRegexpValue.java @@ -28,6 +28,8 @@ */ public interface BRegexpValue extends BValue { + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 + @Deprecated(since = "2201.7.0", forRemoval = true) public RegExpDisjunction getRegExpDisjunction(); BTypedesc getTypedesc(); diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BStream.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BStream.java index 15bbe8915d83..9be484ed0759 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BStream.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BStream.java @@ -29,14 +29,14 @@ */ public interface BStream extends BValue { /** - * Returns the constrained {@code BType} of the stream. + * Returns the constrained {@code Type} of the stream. * * @return constrained type */ Type getConstraintType(); /** - * Returns the completion {@code BType} of the stream. + * Returns the completion {@code Type} of the stream. * * @return completion type */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BStreamingJson.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BStreamingJson.java index 79cf25c82cde..f68cf4c0efb8 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BStreamingJson.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BStreamingJson.java @@ -17,13 +17,11 @@ */ package io.ballerina.runtime.api.values; -import io.ballerina.runtime.internal.JsonDataSource; - import java.io.Writer; /** *

- * {@link BStreamingJson} represent a JSON array generated from a {@link JsonDataSource}. + * {@link BStreamingJson} represent a JSON array generated from a JsonDataSource. *

* * @since 1.1.0 diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BTable.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BTable.java index a0cec28b5d82..605ad9a94162 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BTable.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BTable.java @@ -18,7 +18,6 @@ package io.ballerina.runtime.api.values; import io.ballerina.runtime.api.types.Type; -import io.ballerina.runtime.internal.util.exceptions.BallerinaException; import java.util.Collection; import java.util.Map; @@ -116,19 +115,19 @@ public interface BTable extends BRefValue, BCollection { void clear(); /** - * Returns the value to which the specified key is mapped. A {@link BallerinaException} will be thrown + * Returns the value to which the specified key is mapped. A {@link BError} will be thrown * if the key does not exists. * * @param key the key whose associated value is to be returned * @return the value to which the specified key is mapped - * @throws BallerinaException if the key does not exists + * @throws BError if the key does not exists */ V getOrThrow(Object key); /** * Returns the value for the given key from map. If the key does not exist, but there exists a filler - * value for the expected type, a new value will be created and added and then returned. A {@link - * BallerinaException} will be thrown if the key does not exists and a filler value does not exist. + * value for the expected type, a new value will be created and added and then returned. A {@link BError} + * will be thrown if the key does not exists and a filler value does not exist. * * @param key key used to get the value * @return value associated with the key diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BTypedesc.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BTypedesc.java index 99885b06312c..8e2d800890a1 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BTypedesc.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BTypedesc.java @@ -22,10 +22,10 @@ /** *

- * Ballerina runtime value representation of a *type*. + * Ballerina runtime value representation of a {@link Type}. * - * {@code typedesc} is used to describe type of a value in Ballerina. - * For example {@code typedesc} of number 5 is {@code int}, where as {@code typedesc} of a record value is the + * {@code BTypedesc} is used to describe type of a value in Ballerina. + * For example {@code BTypedesc} of number 5 is {@code int}, where as {@code BTypedesc} of a record value is the * record type that used to create this particular value instance. *

* @@ -34,7 +34,7 @@ public interface BTypedesc extends BValue { /** - * Returns the {@code BType} of the value describe by this type descriptor. + * Returns the {@code Type} of the value describe by this type descriptor. * * @return describing type */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BXmlSequence.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BXmlSequence.java index 43ae0eb5bf29..d92ed1433244 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BXmlSequence.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BXmlSequence.java @@ -19,7 +19,7 @@ import java.util.List; /** - * {@code BXMLItem} represents an XML Sequence in Ballerina. + * {@code BXmlSequence} represents an XML Sequence in Ballerina. * * @since 2.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java index 405eea6b1952..6efeee591740 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java @@ -162,7 +162,7 @@ public long getDefaultableIntValue(BString key) { /** * Retrieve the value for the given key from map. - * A {@link BallerinaException} will be thrown if the key does not exists. + * A {@link BError} will be thrown if the key does not exists. * * @param key key used to get the value * @return value associated with the key @@ -178,7 +178,7 @@ public V getOrThrow(Object key) { /** * Retrieve the value for the given key from map. If the key does not exist, but there exists a filler value for * the expected type, a new value will be created and added and then returned. - * A {@link BallerinaException} will be thrown if the key does not exists and a filler value does not exist. + * A {@link BError} will be thrown if the key does not exists and a filler value does not exist. * * @param key key used to get the value * @return value associated with the key From 6d6593fb6548040ae13bbfaf47af7d2a762ee63b Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Mon, 17 Apr 2023 14:32:58 +0530 Subject: [PATCH 10/28] Change return type of BString.getIterator() --- .../main/java/io/ballerina/runtime/api/values/BString.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BString.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BString.java index 0185bb6dad7f..4450cda817ca 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BString.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BString.java @@ -17,8 +17,6 @@ */ package io.ballerina.runtime.api.values; -import io.ballerina.runtime.internal.values.IteratorValue; - /** * Interface representing ballerina strings. * @@ -40,6 +38,6 @@ public interface BString { BString substring(int beginIndex, int endIndex); - IteratorValue getIterator(); + BIterator getIterator(); } From e40bcf40ed62ec60ccb93a8e6f9c29a005a8f21a Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Mon, 17 Apr 2023 14:50:26 +0530 Subject: [PATCH 11/28] Remove internal imports in api package --- .../src/main/java/io/ballerina/runtime/api/Environment.java | 4 ++-- .../src/main/java/io/ballerina/runtime/api/Future.java | 1 + .../src/main/java/io/ballerina/runtime/api/Runtime.java | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java index aafb6a95d8c4..b9be6c62443c 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java @@ -91,7 +91,7 @@ public Runtime getRuntime() { } /** - * Gets current module @{@link Module}. + * Gets current module {@link Module}. * * @return module of the environment. */ @@ -119,7 +119,7 @@ public Optional getStrandName() { } /** - * Gets @{@link StrandMetadata}. + * Gets {@link StrandMetadata}. * * @return metadata of the strand. */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Future.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Future.java index ca22a1ef05ab..5ba90a31d671 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Future.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Future.java @@ -33,6 +33,7 @@ public class Future { private final Strand strand; private final AtomicBoolean visited = new AtomicBoolean(); + Future(Strand strand) { this.strand = strand; } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java index 69cdfbcb61b4..8a2eb8a7ac5c 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java @@ -75,7 +75,7 @@ public static Runtime getCurrentRuntime() { * @param properties Set of properties for strand. * @param returnType Expected return type of this method. * @param args Ballerina function arguments. - * @return {@link FutureValue} containing return value for executing this method. + * @return {@link BFuture} containing return value for executing this method. *

* This method needs to be called if object.getType().isIsolated() or * object.getType().isIsolated(methodName) returns false. @@ -121,7 +121,7 @@ public void notifyFailure(BError error) { * @param properties Set of properties for strand. * @param returnType Expected return type of this method. * @param args Ballerina function arguments. - * @return {@link FutureValue} containing return value for executing this method. + * @return {@link BFuture} containing return value for executing this method. *

* This method needs to be called if both object.getType().isIsolated() and * object.getType().isIsolated(methodName) returns true. @@ -170,7 +170,7 @@ public void notifyFailure(BError error) { * @param properties Set of properties for strand * @param returnType Expected return type of this method * @param args Ballerina function arguments. - * @return {@link FutureValue} containing return value for executing this method. + * @return {@link BFuture} containing return value for executing this method. * @deprecated If caller can ensure that given object and object method is isolated and no data race is possible * for the mutable state with given arguments, use @invokeMethodAsyncConcurrently * otherwise @invokeMethodAsyncSequentially . From 25704cdc62ad65bbf5713641cd8584525936e23d Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Mon, 17 Apr 2023 16:42:45 +0530 Subject: [PATCH 12/28] Fix failing test --- .../io/ballerina/runtime/api/utils/StringUtils.java | 7 +++++-- .../ballerinalang/langlib/value/FromBalString.java | 11 ++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java index 55a327037f56..61b7dfa9c464 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java @@ -46,11 +46,11 @@ import java.util.Set; import static io.ballerina.runtime.api.constants.RuntimeConstants.STRING_LANG_LIB; -import static io.ballerina.runtime.api.creators.ErrorCreator.createError; import static io.ballerina.runtime.internal.util.StringUtils.getExpressionStringVal; import static io.ballerina.runtime.internal.util.StringUtils.getStringVal; import static io.ballerina.runtime.internal.util.StringUtils.parseExpressionStringVal; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.INDEX_OUT_OF_RANGE_ERROR_IDENTIFIER; +import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.STRING_OPERATION_ERROR; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.getModulePrefixedReason; /** @@ -208,7 +208,10 @@ public static Object parseExpressionStringValue(String value) throws BError { try { return parseExpressionStringVal(value, null); } catch (BallerinaException e) { - throw createError(e); + throw ErrorCreator.createError(STRING_OPERATION_ERROR, StringUtils.fromString(e.getMessage())); + } catch (BError bError) { + throw ErrorCreator.createError(STRING_OPERATION_ERROR, + StringUtils.fromString(bError.getErrorMessage().getValue())); } } diff --git a/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/FromBalString.java b/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/FromBalString.java index 32db342951e9..e972c9dda600 100644 --- a/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/FromBalString.java +++ b/langlib/lang.value/src/main/java/org/ballerinalang/langlib/value/FromBalString.java @@ -18,12 +18,12 @@ package org.ballerinalang.langlib.value; -import io.ballerina.runtime.api.creators.ErrorCreator; import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.values.BError; +import io.ballerina.runtime.api.values.BMap; import io.ballerina.runtime.api.values.BString; -import io.ballerina.runtime.internal.util.exceptions.BallerinaException; +import static io.ballerina.runtime.api.creators.ErrorCreator.createError; import static io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons.FROM_BAL_STRING_ERROR; /** @@ -39,11 +39,8 @@ public static Object fromBalString(BString value) { } try { return StringUtils.parseExpressionStringValue(str); - } catch (BallerinaException e) { - return ErrorCreator.createError(FROM_BAL_STRING_ERROR, StringUtils.fromString(e.getMessage())); - } catch (BError bError) { - return ErrorCreator.createError(FROM_BAL_STRING_ERROR, - StringUtils.fromString(bError.getErrorMessage().getValue())); + } catch (BError e) { + return createError(FROM_BAL_STRING_ERROR, (BMap) e.getDetails()); } } } From 844779f82a38e359aff977e801e7fef1b98093d9 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Fri, 21 Apr 2023 11:15:16 +0530 Subject: [PATCH 13/28] Replace internal objects with proper ones --- .../java/io/ballerina/runtime/api/PredefinedTypes.java | 7 ++++--- .../io/ballerina/runtime/api/creators/ErrorCreator.java | 6 +++--- .../java/io/ballerina/runtime/api/values/BRefValue.java | 7 +++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/PredefinedTypes.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/PredefinedTypes.java index 5294376a2dab..90c8ea37bf75 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/PredefinedTypes.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/PredefinedTypes.java @@ -41,6 +41,7 @@ import io.ballerina.runtime.api.types.StringType; import io.ballerina.runtime.api.types.Type; import io.ballerina.runtime.api.types.TypedescType; +import io.ballerina.runtime.api.types.UnionType; import io.ballerina.runtime.api.types.XmlAttributesType; import io.ballerina.runtime.api.types.XmlType; import io.ballerina.runtime.internal.IteratorUtils; @@ -175,10 +176,10 @@ public class PredefinedTypes { public static final MapType TYPE_DETAIL; public static final Type TYPE_ERROR_DETAIL; public static final ErrorType TYPE_ERROR; - public static final BUnionType TYPE_CLONEABLE; + public static final UnionType TYPE_CLONEABLE; - public static final BUnionType TYPE_JSON_DECIMAL; - public static final BUnionType TYPE_JSON_FLOAT; + public static final UnionType TYPE_JSON_DECIMAL; + public static final UnionType TYPE_JSON_FLOAT; public static final RecordType STRING_ITR_NEXT_RETURN_TYPE = IteratorUtils.createIteratorNextReturnType(PredefinedTypes.TYPE_STRING_CHAR); diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/ErrorCreator.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/ErrorCreator.java index 118b81fca454..51022e14b647 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/ErrorCreator.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/ErrorCreator.java @@ -181,7 +181,7 @@ public static BError createError(Module module, String errorTypeName, * @param typeIdPkg type id module * @param message error message * @return new error - * @deprecated Use @createError(module, errorTypeName, message, cause, details) to create a distinct error. + * @deprecated Use {@link #createError(Module, String, BString, BError, BMap)} to create a distinct error. */ @Deprecated public static BError createDistinctError(String typeIdName, Module typeIdPkg, BString message) { @@ -197,7 +197,7 @@ public static BError createDistinctError(String typeIdName, Module typeIdPkg, BS * @param message error message * @param details error details * @return new error - * @deprecated Use @createError(module, errorTypeName, message, cause, details) to create a distinct error. + * @deprecated Use {@link #createError(Module, String, BString, BError, BMap)} to create a distinct error. */ @Deprecated public static BError createDistinctError(String typeIdName, Module typeIdPkg, BString message, @@ -215,7 +215,7 @@ public static BError createDistinctError(String typeIdName, Module typeIdPkg, BS * @param message error message * @param cause error cause * @return new error - * @deprecated Use @createError(module, errorTypeName, message, cause, details) to create a distinct error. + * @deprecated Use {@link #createError(Module, String, BString, BError, BMap)} to create a distinct error. */ @Deprecated public static BError createDistinctError(String typeIdName, Module typeIdPkg, BString message, diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BRefValue.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BRefValue.java index 223a50fe5e67..4136a965211b 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BRefValue.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BRefValue.java @@ -17,9 +17,8 @@ */ package io.ballerina.runtime.api.values; +import io.ballerina.runtime.api.creators.ErrorCreator; import io.ballerina.runtime.api.utils.StringUtils; -import io.ballerina.runtime.internal.util.exceptions.BLangFreezeException; -import io.ballerina.runtime.internal.util.exceptions.BallerinaException; import java.io.IOException; import java.io.OutputStream; @@ -79,7 +78,7 @@ default boolean isFrozen() { * the {@link BRefValue} is in the middle of freezing by another process. */ default void freezeDirect() { - throw new BLangFreezeException("'freezeDirect()' not allowed on '" + getType() + "'"); + throw ErrorCreator.createError(StringUtils.fromString("'freezeDirect()' not allowed on '" + getType() + "'")); } /** @@ -91,7 +90,7 @@ default void serialize(OutputStream outputStream) { try { outputStream.write(StringUtils.getJsonString(this).getBytes(Charset.defaultCharset())); } catch (IOException e) { - throw new BallerinaException("error occurred while serializing data", e); + throw ErrorCreator.createError(StringUtils.fromString("error occurred while serializing data"), e); } } From 22a65784c1ee485b9c05149afbadfd9c46a8909d Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Fri, 21 Apr 2023 12:58:11 +0530 Subject: [PATCH 14/28] Deprecate methods that accept or return internals --- .../io/ballerina/runtime/api/creators/ValueCreator.java | 4 ++-- .../main/java/io/ballerina/runtime/api/utils/JsonUtils.java | 2 +- .../java/io/ballerina/runtime/api/utils/StringUtils.java | 6 +++--- .../main/java/io/ballerina/runtime/api/values/BDecimal.java | 2 +- .../main/java/io/ballerina/runtime/api/values/BFuture.java | 2 +- .../main/java/io/ballerina/runtime/api/values/BObject.java | 4 ++++ .../java/io/ballerina/runtime/api/values/BRegexpValue.java | 2 +- .../java/io/ballerina/runtime/api/values/BTypedesc.java | 6 ++++++ 8 files changed, 19 insertions(+), 9 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/ValueCreator.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/ValueCreator.java index e3b6910d05d2..037b26c8da09 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/ValueCreator.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/ValueCreator.java @@ -306,7 +306,7 @@ public static BDecimal createDecimalValue(String value) { * @return decimal value * @deprecated use {@link #createDecimalValue(String)} instead. */ - @Deprecated + @Deprecated(since = "2201.6.0", forRemoval = true) public static BDecimal createDecimalValue(String value, DecimalValueKind valueKind) { return new DecimalValue(value, valueKind); } @@ -342,7 +342,7 @@ public static BFunctionPointer createFPValue(Function function, FunctionType typ * @return created {@code StreamingJsonValue} * @deprecated */ - @Deprecated + @Deprecated(since = "2201.6.0", forRemoval = true) public static BStreamingJson createStreamingJsonValue(JsonDataSource datasource) { return new StreamingJsonValue(datasource); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/JsonUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/JsonUtils.java index 70a39dd0e422..5bc39f06329c 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/JsonUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/JsonUtils.java @@ -299,7 +299,7 @@ public static Object convertToJson(Object value) { * @return json value * @deprecated use {@link #convertToJson(Object)} instead. */ - @Deprecated + @Deprecated(since = "2201.6.0", forRemoval = true) public static Object convertToJson(Object value, List unresolvedValues) { return convertToJsonType(value, unresolvedValues); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java index 61b7dfa9c464..9723b34b7487 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/utils/StringUtils.java @@ -168,7 +168,7 @@ public static String getStringValue(Object value) { * @return String value of the value * @deprecated use {@link #getStringValue(Object)} instead. */ - @Deprecated + @Deprecated(since = "2201.6.0", forRemoval = true) public static String getStringValue(Object value, BLink parent) { return getStringVal(value, parent); } @@ -192,7 +192,7 @@ public static String getExpressionStringValue(Object value) { * @return String value of the value in expression style * @deprecated use {@link #getExpressionStringValue(Object)} instead. */ - @Deprecated + @Deprecated(since = "2201.6.0", forRemoval = true) public static String getExpressionStringValue(Object value, BLink parent) { return getExpressionStringVal(value, parent); } @@ -223,7 +223,7 @@ public static Object parseExpressionStringValue(String value) throws BError { * @return Ballerina value represented by Ballerina expression syntax * @deprecated use {@link #parseExpressionStringValue(String)} instead. */ - @Deprecated + @Deprecated(since = "2201.6.0", forRemoval = true) public static Object parseExpressionStringValue(String value, BLink parent) throws BallerinaException { return parseExpressionStringVal(value, parent); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BDecimal.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BDecimal.java index 1c7f4acc4aba..ab8243822b75 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BDecimal.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BDecimal.java @@ -142,7 +142,7 @@ public interface BDecimal { * @return value kind * @deprecated use {@link #decimalValue()} instead. */ - @Deprecated + @Deprecated(since = "2201.6.0", forRemoval = true) DecimalValueKind getValueKind(); /** diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BFuture.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BFuture.java index 47c6fc9cad4d..4dd5a683febd 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BFuture.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BFuture.java @@ -42,7 +42,7 @@ public interface BFuture extends BValue { * @return strand * @deprecated */ - @Deprecated + @Deprecated(since = "2201.6.0", forRemoval = true) Strand getStrand(); /** diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BObject.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BObject.java index fc2a730fe156..0e15e00629c1 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BObject.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BObject.java @@ -34,8 +34,12 @@ */ public interface BObject extends RefValue { + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 + @Deprecated(since = "2201.6.0", forRemoval = true) Object call(Strand strand, String funcName, Object... args); + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 + @Deprecated(since = "2201.6.0", forRemoval = true) BFuture start(Strand strand, String funcName, Object... args); /** diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BRegexpValue.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BRegexpValue.java index 765ce7a04c30..7de5799b3f45 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BRegexpValue.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BRegexpValue.java @@ -29,7 +29,7 @@ public interface BRegexpValue extends BValue { // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 - @Deprecated(since = "2201.7.0", forRemoval = true) + @Deprecated(since = "2201.6.0", forRemoval = true) public RegExpDisjunction getRegExpDisjunction(); BTypedesc getTypedesc(); diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BTypedesc.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BTypedesc.java index 8e2d800890a1..cb38bdaf455f 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BTypedesc.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BTypedesc.java @@ -40,16 +40,22 @@ public interface BTypedesc extends BValue { */ Type getDescribingType(); + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 /** * @param strand strand to be used to run the user-defined-type initialization code. * @return instantiated object + * @deprecated */ + @Deprecated(since = "2201.6.0", forRemoval = true) Object instantiate(Strand strand); + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 /** * @param strand strand to be used to run the user-defined-type initialization code. * @param initialValues the initial values provided in the constructor expression * @return instantiated object + * @deprecated */ + @Deprecated(since = "2201.6.0", forRemoval = true) Object instantiate(Strand strand, BInitialValueEntry[] initialValues); } From fc7da551bd02b16c7ac97e8f4ee1d0451ab09bc2 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Fri, 21 Apr 2023 16:53:01 +0530 Subject: [PATCH 15/28] Refactor Environment Future and Runtime classes --- .../io/ballerina/runtime/api/Environment.java | 68 +---- .../java/io/ballerina/runtime/api/Future.java | 24 +- .../io/ballerina/runtime/api/Runtime.java | 159 +--------- .../runtime/internal/BalEnvironment.java | 151 ++++++++++ .../ballerina/runtime/internal/BalFuture.java | 51 ++++ .../runtime/internal/BalRuntime.java | 276 ++++++++++++++++++ .../runtime/internal/values/ObjectValue.java | 5 + .../internal/values/TypedescValue.java | 6 + .../internal/values/TypedescValueImpl.java | 2 +- .../natives/mock/GenericMockObjectValue.java | 7 +- .../testerina/natives/mock/ObjectMock.java | 3 +- 11 files changed, 523 insertions(+), 229 deletions(-) create mode 100644 bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java create mode 100644 bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalFuture.java create mode 100644 bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java index b9be6c62443c..06ceae067e5f 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java @@ -19,8 +19,6 @@ import io.ballerina.runtime.api.async.StrandMetadata; import io.ballerina.runtime.api.types.Parameter; -import io.ballerina.runtime.internal.scheduling.State; -import io.ballerina.runtime.internal.scheduling.Strand; import java.util.Optional; @@ -30,47 +28,21 @@ * * @since 2.0.0 */ -public class Environment { - - private final Strand strand; - private Future future; - private Module currentModule; - private String funcName; - private Parameter[] funcPathParams; - - public Environment(Strand strand) { - this.strand = strand; - } - - public Environment(Strand strand, Module currentModule) { - this.strand = strand; - this.currentModule = currentModule; - future = new Future(this.strand); - } - - public Environment(Strand strand, Module currentModule, String funcName, Parameter[] funcPathParams) { - this(strand, currentModule); - this.funcName = funcName; - this.funcPathParams = funcPathParams; - } +public interface Environment { /** * Returns the Ballerina function name for the corresponding external interop method. * * @return function name */ - public String getFunctionName() { - return funcName; - } + String getFunctionName(); /** * Returns an array consisting of the path parameters of the resource function defined as external. * * @return array of {@link Parameter} */ - public Parameter[] getFunctionPathParameters() { - return funcPathParams; - } + Parameter[] getFunctionPathParameters(); /** * Mark the current executing strand as async. Execution of Ballerina code after the current @@ -80,33 +52,23 @@ public Parameter[] getFunctionPathParameters() { * * @return BalFuture which will resume the current strand when completed. */ - public Future markAsync() { - strand.blockedOnExtern = true; - strand.setState(State.BLOCK_AND_YIELD); - return future; - } + Future markAsync(); - public Runtime getRuntime() { - return new Runtime(strand.scheduler); - } + Runtime getRuntime(); /** * Gets current module {@link Module}. * * @return module of the environment. */ - public Module getCurrentModule() { - return currentModule; - } + Module getCurrentModule(); /** * Gets the strand id. This will be generated on strand initialization. * * @return Strand id. */ - public int getStrandId() { - return strand.getId(); - } + int getStrandId(); /** * Gets the strand name. This will be optional. Strand name can be either name given in strand annotation or async @@ -114,18 +76,14 @@ public int getStrandId() { * * @return Optional strand name. */ - public Optional getStrandName() { - return strand.getName(); - } + Optional getStrandName(); /** * Gets {@link StrandMetadata}. * * @return metadata of the strand. */ - public StrandMetadata getStrandMetadata() { - return strand.getMetadata(); - } + StrandMetadata getStrandMetadata(); /** * Sets given local key value pair in strand. @@ -133,9 +91,7 @@ public StrandMetadata getStrandMetadata() { * @param key string key * @param value value to be store in the strand */ - public void setStrandLocal(String key, Object value) { - strand.setProperty(key, value); - } + void setStrandLocal(String key, Object value); /** * Gets the value stored in the strand on given key. @@ -143,7 +99,5 @@ public void setStrandLocal(String key, Object value) { * @param key key * @return value stored in the strand. */ - public Object getStrandLocal(String key) { - return strand.getProperty(key); - } + Object getStrandLocal(String key); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Future.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Future.java index 5ba90a31d671..b03eb808ffde 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Future.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Future.java @@ -18,32 +18,12 @@ package io.ballerina.runtime.api; -import io.ballerina.runtime.api.creators.ErrorCreator; -import io.ballerina.runtime.api.utils.StringUtils; -import io.ballerina.runtime.internal.scheduling.Strand; -import io.ballerina.runtime.internal.values.MapValueImpl; - -import java.util.concurrent.atomic.AtomicBoolean; - /** * A future that will resume the underling strand when completed. * * @since 2.0.0 */ -public class Future { - private final Strand strand; - private final AtomicBoolean visited = new AtomicBoolean(); - - Future(Strand strand) { - this.strand = strand; - } +public interface Future { - public void complete(Object returnValue) { - if (visited.getAndSet(true)) { - throw ErrorCreator.createError(StringUtils.fromString("cannot complete the same future twice."), - new MapValueImpl<>(PredefinedTypes.TYPE_ERROR_DETAIL)); - } - strand.returnValue = returnValue; - strand.scheduler.unblockStrand(strand); - } + void complete(Object returnValue); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java index 8a2eb8a7ac5c..540602d1a6b0 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java @@ -19,47 +19,19 @@ import io.ballerina.runtime.api.async.Callback; import io.ballerina.runtime.api.async.StrandMetadata; -import io.ballerina.runtime.api.creators.ErrorCreator; -import io.ballerina.runtime.api.types.ObjectType; import io.ballerina.runtime.api.types.Type; -import io.ballerina.runtime.api.utils.StringUtils; -import io.ballerina.runtime.api.utils.TypeUtils; -import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BFunctionPointer; import io.ballerina.runtime.api.values.BFuture; import io.ballerina.runtime.api.values.BObject; -import io.ballerina.runtime.internal.scheduling.AsyncUtils; -import io.ballerina.runtime.internal.scheduling.Scheduler; -import io.ballerina.runtime.internal.scheduling.Strand; -import io.ballerina.runtime.internal.values.FutureValue; import java.util.Map; -import java.util.function.Function; /** * External API to be used by the interop users to control Ballerina runtime behavior. * * @since 1.0.0 */ -public class Runtime { - - private final Scheduler scheduler; - - Runtime(Scheduler scheduler) { - this.scheduler = scheduler; - } - - /** - * Gets the instance of ballerina runtime. - * - * @return Ballerina runtime instance. - * @deprecated use {@link Environment#getRuntime()} instead. - */ - @Deprecated - public static Runtime getCurrentRuntime() { - Strand strand = Scheduler.getStrand(); - return new Runtime(strand.scheduler); - } +public interface Runtime { /** * Invoke Object method asynchronously and sequentially. This method will ensure that the object methods are @@ -80,32 +52,9 @@ public static Runtime getCurrentRuntime() { * This method needs to be called if object.getType().isIsolated() or * object.getType().isIsolated(methodName) returns false. */ - public BFuture invokeMethodAsyncSequentially(BObject object, String methodName, String strandName, - StrandMetadata metadata, - Callback callback, Map properties, - Type returnType, Object... args) { - try { - validateArgs(object, methodName); - FutureValue future = scheduler.createFuture(null, callback, properties, returnType, strandName, metadata); - AsyncUtils.getArgsWithDefaultValues(scheduler, object, methodName, new Callback() { - @Override - public void notifySuccess(Object result) { - Function func = getFunction((Object[]) result, object, methodName); - scheduler.scheduleToObjectGroup(new Object[1], func, future); - } - @Override - public void notifyFailure(BError error) { - callback.notifyFailure(error); - } - }, args); - return future; - } catch (BError e) { - callback.notifyFailure(e); - } catch (Throwable e) { - callback.notifyFailure(ErrorCreator.createError(StringUtils.fromString(e.getMessage()))); - } - return null; - } + BFuture invokeMethodAsyncSequentially(BObject object, String methodName, String strandName, + StrandMetadata metadata, Callback callback, Map properties, + Type returnType, Object... args); /** * Invoke Object method asynchronously and concurrently. Caller needs to ensure that no data race is possible for @@ -126,32 +75,9 @@ public void notifyFailure(BError error) { * This method needs to be called if both object.getType().isIsolated() and * object.getType().isIsolated(methodName) returns true. */ - public BFuture invokeMethodAsyncConcurrently(BObject object, String methodName, String strandName, - StrandMetadata metadata, - Callback callback, Map properties, - Type returnType, Object... args) { - try { - validateArgs(object, methodName); - FutureValue future = scheduler.createFuture(null, callback, properties, returnType, strandName, metadata); - AsyncUtils.getArgsWithDefaultValues(scheduler, object, methodName, new Callback() { - @Override - public void notifySuccess(Object result) { - Function func = getFunction((Object[]) result, object, methodName); - scheduler.schedule(new Object[1], func, future); - } - @Override - public void notifyFailure(BError error) { - callback.notifyFailure(error); - } - }, args); - return future; - } catch (BError e) { - callback.notifyFailure(e); - } catch (Throwable e) { - callback.notifyFailure(ErrorCreator.createError(StringUtils.fromString(e.getMessage()))); - } - return null; - } + BFuture invokeMethodAsyncConcurrently(BObject object, String methodName, String strandName, + StrandMetadata metadata, Callback callback, Map properties, + Type returnType, Object... args); /** * Invoke Object method asynchronously. This will schedule the function and block the strand. @@ -179,37 +105,8 @@ public void notifyFailure(BError error) { * object.getType().isIsolated(methodName) returns true. */ @Deprecated - public BFuture invokeMethodAsync(BObject object, String methodName, String strandName, StrandMetadata metadata, - Callback callback, Map properties, - Type returnType, Object... args) { - try { - validateArgs(object, methodName); - ObjectType objectType = (ObjectType) TypeUtils.getReferredType(object.getType()); - boolean isIsolated = objectType.isIsolated() && objectType.isIsolated(methodName); - FutureValue future = scheduler.createFuture(null, callback, properties, returnType, strandName, metadata); - AsyncUtils.getArgsWithDefaultValues(scheduler, object, methodName, new Callback() { - @Override - public void notifySuccess(Object result) { - Function func = getFunction((Object[]) result, object, methodName); - if (isIsolated) { - scheduler.schedule(new Object[1], func, future); - } else { - scheduler.scheduleToObjectGroup(new Object[1], func, future); - } - } - @Override - public void notifyFailure(BError error) { - callback.notifyFailure(error); - } - }, args); - return future; - } catch (BError e) { - callback.notifyFailure(e); - } catch (Throwable e) { - callback.notifyFailure(ErrorCreator.createError(StringUtils.fromString(e.getMessage()))); - } - return null; - } + BFuture invokeMethodAsync(BObject object, String methodName, String strandName, StrandMetadata metadata, + Callback callback, Map properties, Type returnType, Object... args); /** * Invoke Object method asynchronously. This will schedule the function and block the strand. @@ -230,40 +127,12 @@ public void notifyFailure(BError error) { * object.getType().isIsolated(methodName) returns true. */ @Deprecated - public Object invokeMethodAsync(BObject object, String methodName, String strandName, StrandMetadata metadata, - Callback callback, Object... args) { - return invokeMethodAsync(object, methodName, strandName, metadata, callback, null, - PredefinedTypes.TYPE_NULL, args); - } - - private void validateArgs(BObject object, String methodName) { - if (object == null) { - throw ErrorCreator.createError(StringUtils.fromString("object cannot be null")); - } - if (methodName == null) { - throw ErrorCreator.createError(StringUtils.fromString("method name cannot be null")); - } - } - - public void registerListener(BObject listener) { - scheduler.getRuntimeRegistry().registerListener(listener); - } + Object invokeMethodAsync(BObject object, String methodName, String strandName, StrandMetadata metadata, + Callback callback, Object... args); - public void deregisterListener(BObject listener) { - scheduler.getRuntimeRegistry().deregisterListener(listener); - } + void registerListener(BObject listener); - public void registerStopHandler(BFunctionPointer stopHandler) { - scheduler.getRuntimeRegistry().registerStopHandler(stopHandler); - } + void deregisterListener(BObject listener); - private Function getFunction(Object[] argsWithDefaultValues, BObject object, String methodName) { - Function func; - if (argsWithDefaultValues.length == 1) { - func = o -> object.call((Strand) (((Object[]) o)[0]), methodName, argsWithDefaultValues[0]); - } else { - func = o -> object.call((Strand) (((Object[]) o)[0]), methodName, argsWithDefaultValues); - } - return func; - } + void registerStopHandler(BFunctionPointer stopHandler); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java new file mode 100644 index 000000000000..38a606e36e05 --- /dev/null +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.ballerina.runtime.internal; + +import io.ballerina.runtime.api.Environment; +import io.ballerina.runtime.api.Module; +import io.ballerina.runtime.api.async.StrandMetadata; +import io.ballerina.runtime.api.types.Parameter; +import io.ballerina.runtime.internal.scheduling.State; +import io.ballerina.runtime.internal.scheduling.Strand; + +import java.util.Optional; + +/** + * When this class is used as the first argument of an interop method, Ballerina will inject an instance of the class + * when calling. That instance can be used to communicate with currently executing Ballerina runtime. + * + * @since 2.0.0 + */ +public class BalEnvironment implements Environment { + + private final Strand strand; + private BalFuture future; + private Module currentModule; + private String funcName; + private Parameter[] funcPathParams; + + public BalEnvironment(Strand strand) { + this.strand = strand; + } + + public BalEnvironment(Strand strand, Module currentModule) { + this.strand = strand; + this.currentModule = currentModule; + future = new BalFuture(this.strand); + } + + public BalEnvironment(Strand strand, Module currentModule, String funcName, Parameter[] funcPathParams) { + this(strand, currentModule); + this.funcName = funcName; + this.funcPathParams = funcPathParams; + } + + /** + * Returns the Ballerina function name for the corresponding external interop method. + * + * @return function name + */ + public String getFunctionName() { + return funcName; + } + + /** + * Returns an array consisting of the path parameters of the resource function defined as external. + * + * @return array of {@link Parameter} + */ + public Parameter[] getFunctionPathParameters() { + return funcPathParams; + } + + /** + * Mark the current executing strand as async. Execution of Ballerina code after the current + * interop will stop until given BalFuture is completed. However the java thread will not be blocked + * and will be reused for running other Ballerina code in the meantime. Therefore callee of this method + * must return as soon as possible to avoid starvation of ballerina code execution. + * + * @return BalFuture which will resume the current strand when completed. + */ + public BalFuture markAsync() { + strand.blockedOnExtern = true; + strand.setState(State.BLOCK_AND_YIELD); + return future; + } + + public BalRuntime getRuntime() { + return new BalRuntime(strand.scheduler); + } + + /** + * Gets current module {@link Module}. + * + * @return module of the environment. + */ + public Module getCurrentModule() { + return currentModule; + } + + /** + * Gets the strand id. This will be generated on strand initialization. + * + * @return Strand id. + */ + public int getStrandId() { + return strand.getId(); + } + + /** + * Gets the strand name. This will be optional. Strand name can be either name given in strand annotation or async + * call or function pointer variable name. + * + * @return Optional strand name. + */ + public Optional getStrandName() { + return strand.getName(); + } + + /** + * Gets {@link StrandMetadata}. + * + * @return metadata of the strand. + */ + public StrandMetadata getStrandMetadata() { + return strand.getMetadata(); + } + + /** + * Sets given local key value pair in strand. + * + * @param key string key + * @param value value to be store in the strand + */ + public void setStrandLocal(String key, Object value) { + strand.setProperty(key, value); + } + + /** + * Gets the value stored in the strand on given key. + * + * @param key key + * @return value stored in the strand. + */ + public Object getStrandLocal(String key) { + return strand.getProperty(key); + } +} diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalFuture.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalFuture.java new file mode 100644 index 000000000000..2519b677856b --- /dev/null +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalFuture.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.ballerina.runtime.internal; + +import io.ballerina.runtime.api.Future; +import io.ballerina.runtime.api.PredefinedTypes; +import io.ballerina.runtime.api.creators.ErrorCreator; +import io.ballerina.runtime.api.utils.StringUtils; +import io.ballerina.runtime.internal.scheduling.Strand; +import io.ballerina.runtime.internal.values.MapValueImpl; + +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * A future that will resume the underling strand when completed. + * + * @since 2.0.0 + */ +public class BalFuture implements Future { + private final Strand strand; + private final AtomicBoolean visited = new AtomicBoolean(); + + public BalFuture(Strand strand) { + this.strand = strand; + } + + public void complete(Object returnValue) { + if (visited.getAndSet(true)) { + throw ErrorCreator.createError(StringUtils.fromString("cannot complete the same future twice."), + new MapValueImpl<>(PredefinedTypes.TYPE_ERROR_DETAIL)); + } + strand.returnValue = returnValue; + strand.scheduler.unblockStrand(strand); + } +} diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java new file mode 100644 index 000000000000..fd5d7e81eb98 --- /dev/null +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java @@ -0,0 +1,276 @@ +/* + * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.ballerina.runtime.internal; + +import io.ballerina.runtime.api.Environment; +import io.ballerina.runtime.api.PredefinedTypes; +import io.ballerina.runtime.api.Runtime; +import io.ballerina.runtime.api.async.Callback; +import io.ballerina.runtime.api.async.StrandMetadata; +import io.ballerina.runtime.api.creators.ErrorCreator; +import io.ballerina.runtime.api.types.ObjectType; +import io.ballerina.runtime.api.types.Type; +import io.ballerina.runtime.api.utils.StringUtils; +import io.ballerina.runtime.api.utils.TypeUtils; +import io.ballerina.runtime.api.values.BError; +import io.ballerina.runtime.api.values.BFunctionPointer; +import io.ballerina.runtime.api.values.BFuture; +import io.ballerina.runtime.api.values.BObject; +import io.ballerina.runtime.internal.scheduling.AsyncUtils; +import io.ballerina.runtime.internal.scheduling.Scheduler; +import io.ballerina.runtime.internal.scheduling.Strand; +import io.ballerina.runtime.internal.values.FutureValue; +import io.ballerina.runtime.internal.values.ObjectValue; + +import java.util.Map; +import java.util.function.Function; + +/** + * External API to be used by the interop users to control Ballerina runtime behavior. + * + * @since 1.0.0 + */ +public class BalRuntime implements Runtime { + + private final Scheduler scheduler; + + public BalRuntime(Scheduler scheduler) { + this.scheduler = scheduler; + } + + /** + * Gets the instance of ballerina runtime. + * + * @return Ballerina runtime instance. + * @deprecated use {@link Environment#getRuntime()} instead. + */ + @Deprecated + public static BalRuntime getCurrentRuntime() { + Strand strand = Scheduler.getStrand(); + return new BalRuntime(strand.scheduler); + } + + /** + * Invoke Object method asynchronously and sequentially. This method will ensure that the object methods are + * invoked in the same thread where other object methods are executed. So, the methods will be executed + * sequentially per object level. + * + * @param object Object Value. + * @param methodName Name of the method. + * @param strandName Name for newly created strand which is used to execute the function pointer. This is + * optional and can be null. + * @param metadata Meta data of new strand. + * @param callback Callback which will get notified once the method execution is done. + * @param properties Set of properties for strand. + * @param returnType Expected return type of this method. + * @param args Ballerina function arguments. + * @return {@link BFuture} containing return value for executing this method. + *

+ * This method needs to be called if object.getType().isIsolated() or + * object.getType().isIsolated(methodName) returns false. + */ + public BFuture invokeMethodAsyncSequentially(BObject object, String methodName, String strandName, + StrandMetadata metadata, + Callback callback, Map properties, + Type returnType, Object... args) { + try { + validateArgs(object, methodName); + ObjectValue objectVal = (ObjectValue) object; + FutureValue future = scheduler.createFuture(null, callback, properties, returnType, strandName, metadata); + AsyncUtils.getArgsWithDefaultValues(scheduler, objectVal, methodName, new Callback() { + @Override + public void notifySuccess(Object result) { + Function func = getFunction((Object[]) result, objectVal, methodName); + scheduler.scheduleToObjectGroup(new Object[1], func, future); + } + @Override + public void notifyFailure(BError error) { + callback.notifyFailure(error); + } + }, args); + return future; + } catch (BError e) { + callback.notifyFailure(e); + } catch (Throwable e) { + callback.notifyFailure(ErrorCreator.createError(StringUtils.fromString(e.getMessage()))); + } + return null; + } + + /** + * Invoke Object method asynchronously and concurrently. Caller needs to ensure that no data race is possible for + * the mutable state with given object method and with arguments. So, the method can be concurrently run with + * different os threads. + * + * @param object Object Value. + * @param methodName Name of the method. + * @param strandName Name for newly created strand which is used to execute the function pointer. This is + * optional and can be null. + * @param metadata Meta data of new strand. + * @param callback Callback which will get notified once the method execution is done. + * @param properties Set of properties for strand. + * @param returnType Expected return type of this method. + * @param args Ballerina function arguments. + * @return {@link BFuture} containing return value for executing this method. + *

+ * This method needs to be called if both object.getType().isIsolated() and + * object.getType().isIsolated(methodName) returns true. + */ + public BFuture invokeMethodAsyncConcurrently(BObject object, String methodName, String strandName, + StrandMetadata metadata, + Callback callback, Map properties, + Type returnType, Object... args) { + try { + validateArgs(object, methodName); + ObjectValue objectVal = (ObjectValue) object; + FutureValue future = scheduler.createFuture(null, callback, properties, returnType, strandName, metadata); + AsyncUtils.getArgsWithDefaultValues(scheduler, objectVal, methodName, new Callback() { + @Override + public void notifySuccess(Object result) { + Function func = getFunction((Object[]) result, objectVal, methodName); + scheduler.schedule(new Object[1], func, future); + } + @Override + public void notifyFailure(BError error) { + callback.notifyFailure(error); + } + }, args); + return future; + } catch (BError e) { + callback.notifyFailure(e); + } catch (Throwable e) { + callback.notifyFailure(ErrorCreator.createError(StringUtils.fromString(e.getMessage()))); + } + return null; + } + + /** + * Invoke Object method asynchronously. This will schedule the function and block the strand. + * This API checks whether the object or object method is isolated. So, if an object method is isolated, method + * will be concurrently executed in different os threads. + *

+ * Caller needs to ensure that no data race is possible for the mutable state with given arguments. So, the + * method can be concurrently run with different os threads. + * + * @param object Object Value. + * @param methodName Name of the method. + * @param strandName Name for newly creating strand which is used to execute the function pointer. This is + * optional and can be null. + * @param metadata Meta data of new strand. + * @param callback Callback which will get notify once method execution done. + * @param properties Set of properties for strand + * @param returnType Expected return type of this method + * @param args Ballerina function arguments. + * @return {@link BFuture} containing return value for executing this method. + * @deprecated If caller can ensure that given object and object method is isolated and no data race is possible + * for the mutable state with given arguments, use @invokeMethodAsyncConcurrently + * otherwise @invokeMethodAsyncSequentially . + *

+ * We can decide the object method isolation if and only if both object.getType().isIsolated() and + * object.getType().isIsolated(methodName) returns true. + */ + @Deprecated + public BFuture invokeMethodAsync(BObject object, String methodName, String strandName, StrandMetadata metadata, + Callback callback, Map properties, + Type returnType, Object... args) { + try { + validateArgs(object, methodName); + ObjectValue objectVal = (ObjectValue) object; + ObjectType objectType = (ObjectType) TypeUtils.getReferredType(objectVal.getType()); + boolean isIsolated = objectType.isIsolated() && objectType.isIsolated(methodName); + FutureValue future = scheduler.createFuture(null, callback, properties, returnType, strandName, metadata); + AsyncUtils.getArgsWithDefaultValues(scheduler, objectVal, methodName, new Callback() { + @Override + public void notifySuccess(Object result) { + Function func = getFunction((Object[]) result, objectVal, methodName); + if (isIsolated) { + scheduler.schedule(new Object[1], func, future); + } else { + scheduler.scheduleToObjectGroup(new Object[1], func, future); + } + } + @Override + public void notifyFailure(BError error) { + callback.notifyFailure(error); + } + }, args); + return future; + } catch (BError e) { + callback.notifyFailure(e); + } catch (Throwable e) { + callback.notifyFailure(ErrorCreator.createError(StringUtils.fromString(e.getMessage()))); + } + return null; + } + + /** + * Invoke Object method asynchronously. This will schedule the function and block the strand. + * + * @param object Object Value. + * @param methodName Name of the method. + * @param strandName Name for newly created strand which is used to execute the function pointer. This is optional + * and can be null. + * @param metadata Meta data of new strand. + * @param callback Callback which will get notified once the method execution is done. + * @param args Ballerina function arguments. + * @return the result of the function invocation. + * @deprecated If caller can ensure that given object and object method is isolated and no data race is possible + * for the mutable state with given arguments, use @invokeMethodAsyncConcurrently + * otherwise @invokeMethodAsyncSequentially . + *

+ * We can decide the object method isolation if both object.getType().isIsolated() and + * object.getType().isIsolated(methodName) returns true. + */ + @Deprecated + public Object invokeMethodAsync(BObject object, String methodName, String strandName, StrandMetadata metadata, + Callback callback, Object... args) { + return invokeMethodAsync(object, methodName, strandName, metadata, callback, null, + PredefinedTypes.TYPE_NULL, args); + } + + private void validateArgs(BObject object, String methodName) { + if (object instanceof ObjectValue) { + throw ErrorCreator.createError(StringUtils.fromString("invalid object")); + } + if (methodName == null) { + throw ErrorCreator.createError(StringUtils.fromString("method name cannot be null")); + } + } + + public void registerListener(BObject listener) { + scheduler.getRuntimeRegistry().registerListener(listener); + } + + public void deregisterListener(BObject listener) { + scheduler.getRuntimeRegistry().deregisterListener(listener); + } + + public void registerStopHandler(BFunctionPointer stopHandler) { + scheduler.getRuntimeRegistry().registerStopHandler(stopHandler); + } + + private Function getFunction(Object[] argsWithDefaultValues, ObjectValue objectVal, String methodName) { + Function func; + if (argsWithDefaultValues.length == 1) { + func = o -> objectVal.call((Strand) (((Object[]) o)[0]), methodName, argsWithDefaultValues[0]); + } else { + func = o -> objectVal.call((Strand) (((Object[]) o)[0]), methodName, argsWithDefaultValues); + } + return func; + } +} diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ObjectValue.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ObjectValue.java index 6edc543f28c4..2dbe41561ef1 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ObjectValue.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ObjectValue.java @@ -17,7 +17,9 @@ */ package io.ballerina.runtime.internal.values; +import io.ballerina.runtime.api.values.BFuture; import io.ballerina.runtime.api.values.BObject; +import io.ballerina.runtime.internal.scheduling.Strand; /** *

@@ -31,4 +33,7 @@ */ public interface ObjectValue extends BObject { + Object call(Strand strand, String funcName, Object... args); + + BFuture start(Strand strand, String funcName, Object... args); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValue.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValue.java index 6cb915b3fa65..8fb9c222dce9 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValue.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValue.java @@ -17,7 +17,9 @@ */ package io.ballerina.runtime.internal.values; +import io.ballerina.runtime.api.values.BInitialValueEntry; import io.ballerina.runtime.api.values.BTypedesc; +import io.ballerina.runtime.internal.scheduling.Strand; /** *

@@ -26,4 +28,8 @@ * @since 1.3.0 */ public interface TypedescValue extends RefValue, BTypedesc { + + Object instantiate(Strand strand); + + Object instantiate(Strand strand, BInitialValueEntry[] initialValues); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java index 4ea12426ba74..b35424e92401 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java @@ -48,7 +48,7 @@ * * @since 0.995.0 */ -public class TypedescValueImpl implements TypedescValue { +public class TypedescValueImpl implements TypedescValue { final Type type; final Type describingType; // Type of the value describe by this typedesc. diff --git a/misc/testerina/modules/testerina-core/src/main/java/org/ballerinalang/testerina/natives/mock/GenericMockObjectValue.java b/misc/testerina/modules/testerina-core/src/main/java/org/ballerinalang/testerina/natives/mock/GenericMockObjectValue.java index 3377e745e508..6be046892631 100644 --- a/misc/testerina/modules/testerina-core/src/main/java/org/ballerinalang/testerina/natives/mock/GenericMockObjectValue.java +++ b/misc/testerina/modules/testerina-core/src/main/java/org/ballerinalang/testerina/natives/mock/GenericMockObjectValue.java @@ -31,6 +31,7 @@ import io.ballerina.runtime.api.values.BTypedesc; import io.ballerina.runtime.internal.scheduling.Strand; import io.ballerina.runtime.internal.util.exceptions.BallerinaException; +import io.ballerina.runtime.internal.values.ObjectValue; import io.ballerina.runtime.internal.values.TypedescValueImpl; import java.util.ArrayList; @@ -42,14 +43,14 @@ /** * A generic mock object to create a mock of any given typedesc. */ -public class GenericMockObjectValue implements BObject { +public class GenericMockObjectValue implements ObjectValue { - private BObject mockObj; + private ObjectValue mockObj; private ObjectType type; private BTypedesc typedesc; - public GenericMockObjectValue(ObjectType type, BObject mockObj) { + public GenericMockObjectValue(ObjectType type, ObjectValue mockObj) { this.type = type; this.mockObj = mockObj; this.typedesc = new TypedescValueImpl(type); diff --git a/misc/testerina/modules/testerina-core/src/main/java/org/ballerinalang/testerina/natives/mock/ObjectMock.java b/misc/testerina/modules/testerina-core/src/main/java/org/ballerinalang/testerina/natives/mock/ObjectMock.java index 49de516f8265..048cf2974563 100644 --- a/misc/testerina/modules/testerina-core/src/main/java/org/ballerinalang/testerina/natives/mock/ObjectMock.java +++ b/misc/testerina/modules/testerina-core/src/main/java/org/ballerinalang/testerina/natives/mock/ObjectMock.java @@ -39,6 +39,7 @@ import io.ballerina.runtime.internal.TypeChecker; import io.ballerina.runtime.internal.types.BClientType; import io.ballerina.runtime.internal.values.MapValueImpl; +import io.ballerina.runtime.internal.values.ObjectValue; import java.util.List; import java.util.Map; @@ -60,7 +61,7 @@ private ObjectMock() { * @param objectValue mock object to impersonate the type * @return mock object of provided type */ - public static BObject mock(BTypedesc bTypedesc, BObject objectValue) { + public static BObject mock(BTypedesc bTypedesc, ObjectValue objectValue) { ObjectType objectValueType = (ObjectType) TypeUtils.getReferredType(objectValue.getType()); if (!objectValueType.getName().contains(MockConstants.DEFAULT_MOCK_OBJ_ANON)) { // handle user-defined mock object From 5f275e77e3c84fa4af3377e89acaa6863c4d39ac Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Tue, 2 May 2023 12:55:27 +0530 Subject: [PATCH 16/28] Fix code generation issues --- .../io/ballerina/runtime/api/Environment.java | 10 +++++----- .../runtime/internal/BalEnvironment.java | 13 +++++++------ .../ballerina/runtime/internal/BalFuture.java | 8 ++++---- .../runtime/internal/BalRuntime.java | 19 ++++++++++--------- .../compiler/bir/codegen/JvmConstants.java | 1 + .../bir/codegen/JvmInstructionGen.java | 6 +++--- .../bir/codegen/JvmTerminatorGen.java | 6 +++--- 7 files changed, 33 insertions(+), 30 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java index 06ceae067e5f..615e51320089 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java @@ -23,8 +23,8 @@ import java.util.Optional; /** - * When this class is used as the first argument of an interop method, Ballerina will inject an instance of the class - * when calling. That instance can be used to communicate with currently executing Ballerina runtime. + * When this interface is used as the first argument of an interop method, Ballerina will inject an instance of + * the class when calling. That instance can be used to communicate with currently executing Ballerina runtime. * * @since 2.0.0 */ @@ -46,11 +46,11 @@ public interface Environment { /** * Mark the current executing strand as async. Execution of Ballerina code after the current - * interop will stop until given BalFuture is completed. However the java thread will not be blocked + * interop will stop until given Ballerina Future is completed. However the java thread will not be blocked * and will be reused for running other Ballerina code in the meantime. Therefore callee of this method - * must return as soon as possible to avoid starvation of ballerina code execution. + * must return as soon as possible to avoid starvation of Ballerina code execution. * - * @return BalFuture which will resume the current strand when completed. + * @return {@link Future} which will resume the current strand when completed. */ Future markAsync(); diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java index 38a606e36e05..4718728da469 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java @@ -1,12 +1,12 @@ /* - * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, + * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an @@ -15,6 +15,7 @@ * specific language governing permissions and limitations * under the License. */ + package io.ballerina.runtime.internal; import io.ballerina.runtime.api.Environment; @@ -27,10 +28,10 @@ import java.util.Optional; /** - * When this class is used as the first argument of an interop method, Ballerina will inject an instance of the class - * when calling. That instance can be used to communicate with currently executing Ballerina runtime. + * When {@link Environment} is used as the first argument of an interop method, Ballerina will inject an instance + * of this class when calling. That instance can be used to communicate with currently executing Ballerina runtime. * - * @since 2.0.0 + * @since 2201.6.0 */ public class BalEnvironment implements Environment { diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalFuture.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalFuture.java index 2519b677856b..265f027767f6 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalFuture.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalFuture.java @@ -1,12 +1,12 @@ /* - * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, + * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an @@ -30,7 +30,7 @@ /** * A future that will resume the underling strand when completed. * - * @since 2.0.0 + * @since 2201.6.0 */ public class BalFuture implements Future { private final Strand strand; diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java index fd5d7e81eb98..15cfb8558dbf 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java @@ -1,16 +1,17 @@ /* - * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * WSO2 Inc. licenses this file to you under the Apache License, + * Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com) All Rights Reserved. + * + * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

+ * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ @@ -41,9 +42,9 @@ import java.util.function.Function; /** - * External API to be used by the interop users to control Ballerina runtime behavior. + * Internal implementation of the API used by the interop users to control Ballerina runtime behavior. * - * @since 1.0.0 + * @since 2201.6.0 */ public class BalRuntime implements Runtime { @@ -244,7 +245,7 @@ public Object invokeMethodAsync(BObject object, String methodName, String strand } private void validateArgs(BObject object, String methodName) { - if (object instanceof ObjectValue) { + if (object == null) { throw ErrorCreator.createError(StringUtils.fromString("invalid object")); } if (methodName == null) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmConstants.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmConstants.java index 78dc81af3c9f..bfefd0ade9d9 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmConstants.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmConstants.java @@ -189,6 +189,7 @@ public class JvmConstants { public static final String JSON_UTILS = "io/ballerina/runtime/internal/JsonInternalUtils"; public static final String STRAND_CLASS = "io/ballerina/runtime/internal/scheduling/Strand"; public static final String STRAND_METADATA = "io/ballerina/runtime/api/async/StrandMetadata"; + public static final String BAL_ENV_CLASS = "io/ballerina/runtime/internal/BalEnvironment"; public static final String BAL_ENV = "io/ballerina/runtime/api/Environment"; public static final String BAL_FUTURE = "io/ballerina/runtime/api/Future"; public static final String TYPE_CONVERTER = "io/ballerina/runtime/internal/TypeConverter"; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java index 9307002afd1f..c7a3f5408bc9 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java @@ -126,7 +126,7 @@ import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.ANNOTATION_UTILS; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.ARRAY_VALUE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.ARRAY_VALUE_IMPL; -import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BAL_ENV; +import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BAL_ENV_CLASS; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BYTE_VALUE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.B_MAPPING_INITIAL_VALUE_ENTRY; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.B_OBJECT; @@ -611,13 +611,13 @@ void generatePlatformIns(JInstruction ins, int localVarOffset) { String jMethodVMSig = callIns.jMethodVMSig; boolean hasBalEnvParam = jMethodVMSig.startsWith(BAL_ENV_PARAM); if (hasBalEnvParam) { - mv.visitTypeInsn(NEW, BAL_ENV); + mv.visitTypeInsn(NEW, BAL_ENV_CLASS); mv.visitInsn(DUP); // load the strand this.mv.visitVarInsn(ALOAD, localVarOffset); // load the current Module mv.visitFieldInsn(GETSTATIC, this.moduleInitClass, CURRENT_MODULE_VAR_NAME, GET_MODULE); - mv.visitMethodInsn(INVOKESPECIAL, BAL_ENV, JVM_INIT_METHOD, + mv.visitMethodInsn(INVOKESPECIAL, BAL_ENV_CLASS, JVM_INIT_METHOD, INIT_BAL_ENV, false); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmTerminatorGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmTerminatorGen.java index 06fafb76d61e..cc00b255fcc3 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmTerminatorGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmTerminatorGen.java @@ -98,7 +98,7 @@ import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.ANNOTATION_UTILS; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.ARRAY_LIST; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.ARRAY_VALUE_IMPL; -import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BAL_ENV; +import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BAL_ENV_CLASS; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BAL_ERROR_REASONS; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BAL_EXTENSION; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BIG_DECIMAL; @@ -611,7 +611,7 @@ private void genJICallTerm(JIMethodCall callIns, int localVarOffset, BIRNode.BIR boolean hasBalEnvParam = jMethodVMSig.startsWith(BAL_ENV_PARAM); if (hasBalEnvParam) { - mv.visitTypeInsn(NEW, BAL_ENV); + mv.visitTypeInsn(NEW, BAL_ENV_CLASS); mv.visitInsn(DUP); this.mv.visitVarInsn(ALOAD, localVarOffset); // load the strand // load the current Module @@ -619,7 +619,7 @@ private void genJICallTerm(JIMethodCall callIns, int localVarOffset, BIRNode.BIR // load function name mv.visitLdcInsn(func.name.getValue()); this.jvmTypeGen.loadFunctionPathParameters(mv, (BInvokableTypeSymbol) func.type.tsymbol); - mv.visitMethodInsn(INVOKESPECIAL, BAL_ENV, JVM_INIT_METHOD, INIT_BAL_ENV_WITH_FUNC_NAME, false); + mv.visitMethodInsn(INVOKESPECIAL, BAL_ENV_CLASS, JVM_INIT_METHOD, INIT_BAL_ENV_WITH_FUNC_NAME, false); } if (callIns.receiver != null) { From 4c1484602edca1f9197c7d0ece22f3ff739cef39 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Tue, 2 May 2023 12:57:50 +0530 Subject: [PATCH 17/28] Remove deprecated getCurrentRuntime API --- .../io/ballerina/runtime/internal/BalRuntime.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java index 15cfb8558dbf..2fb4c342c6a1 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java @@ -18,7 +18,6 @@ package io.ballerina.runtime.internal; -import io.ballerina.runtime.api.Environment; import io.ballerina.runtime.api.PredefinedTypes; import io.ballerina.runtime.api.Runtime; import io.ballerina.runtime.api.async.Callback; @@ -54,18 +53,6 @@ public BalRuntime(Scheduler scheduler) { this.scheduler = scheduler; } - /** - * Gets the instance of ballerina runtime. - * - * @return Ballerina runtime instance. - * @deprecated use {@link Environment#getRuntime()} instead. - */ - @Deprecated - public static BalRuntime getCurrentRuntime() { - Strand strand = Scheduler.getStrand(); - return new BalRuntime(strand.scheduler); - } - /** * Invoke Object method asynchronously and sequentially. This method will ensure that the object methods are * invoked in the same thread where other object methods are executed. So, the methods will be executed From 520372f6df06ba97075ba7b36ebf34f227d4e685 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Tue, 2 May 2023 15:40:42 +0530 Subject: [PATCH 18/28] Fix failing test --- .../src/main/java/io/ballerina/runtime/internal/BalRuntime.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java index 2fb4c342c6a1..8668441cc838 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java @@ -233,7 +233,7 @@ public Object invokeMethodAsync(BObject object, String methodName, String strand private void validateArgs(BObject object, String methodName) { if (object == null) { - throw ErrorCreator.createError(StringUtils.fromString("invalid object")); + throw ErrorCreator.createError(StringUtils.fromString("object cannot be null")); } if (methodName == null) { throw ErrorCreator.createError(StringUtils.fromString("method name cannot be null")); From e571262ae59806b796e360f4aff1d8e5a1ea3464 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Tue, 2 May 2023 15:58:30 +0530 Subject: [PATCH 19/28] Change interfaces to abstract classes --- .../io/ballerina/runtime/api/Environment.java | 22 +++++++++---------- .../java/io/ballerina/runtime/api/Future.java | 4 ++-- .../io/ballerina/runtime/api/Runtime.java | 21 +++++++++--------- .../runtime/internal/BalEnvironment.java | 2 +- .../ballerina/runtime/internal/BalFuture.java | 2 +- .../runtime/internal/BalRuntime.java | 2 +- 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java index 615e51320089..41f4a13f05f7 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java @@ -28,21 +28,21 @@ * * @since 2.0.0 */ -public interface Environment { +public abstract class Environment { /** * Returns the Ballerina function name for the corresponding external interop method. * * @return function name */ - String getFunctionName(); + public abstract String getFunctionName(); /** * Returns an array consisting of the path parameters of the resource function defined as external. * * @return array of {@link Parameter} */ - Parameter[] getFunctionPathParameters(); + public abstract Parameter[] getFunctionPathParameters(); /** * Mark the current executing strand as async. Execution of Ballerina code after the current @@ -52,23 +52,23 @@ public interface Environment { * * @return {@link Future} which will resume the current strand when completed. */ - Future markAsync(); + public abstract Future markAsync(); - Runtime getRuntime(); + public abstract Runtime getRuntime(); /** * Gets current module {@link Module}. * * @return module of the environment. */ - Module getCurrentModule(); + public abstract Module getCurrentModule(); /** * Gets the strand id. This will be generated on strand initialization. * * @return Strand id. */ - int getStrandId(); + public abstract int getStrandId(); /** * Gets the strand name. This will be optional. Strand name can be either name given in strand annotation or async @@ -76,14 +76,14 @@ public interface Environment { * * @return Optional strand name. */ - Optional getStrandName(); + public abstract Optional getStrandName(); /** * Gets {@link StrandMetadata}. * * @return metadata of the strand. */ - StrandMetadata getStrandMetadata(); + public abstract StrandMetadata getStrandMetadata(); /** * Sets given local key value pair in strand. @@ -91,7 +91,7 @@ public interface Environment { * @param key string key * @param value value to be store in the strand */ - void setStrandLocal(String key, Object value); + public abstract void setStrandLocal(String key, Object value); /** * Gets the value stored in the strand on given key. @@ -99,5 +99,5 @@ public interface Environment { * @param key key * @return value stored in the strand. */ - Object getStrandLocal(String key); + public abstract Object getStrandLocal(String key); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Future.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Future.java index b03eb808ffde..c853eba37eaf 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Future.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Future.java @@ -23,7 +23,7 @@ * * @since 2.0.0 */ -public interface Future { +public abstract class Future { - void complete(Object returnValue); + public abstract void complete(Object returnValue); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java index 540602d1a6b0..ea7f2b286708 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java @@ -31,7 +31,7 @@ * * @since 1.0.0 */ -public interface Runtime { +public abstract class Runtime { /** * Invoke Object method asynchronously and sequentially. This method will ensure that the object methods are @@ -52,7 +52,7 @@ public interface Runtime { * This method needs to be called if object.getType().isIsolated() or * object.getType().isIsolated(methodName) returns false. */ - BFuture invokeMethodAsyncSequentially(BObject object, String methodName, String strandName, + public abstract BFuture invokeMethodAsyncSequentially(BObject object, String methodName, String strandName, StrandMetadata metadata, Callback callback, Map properties, Type returnType, Object... args); @@ -75,7 +75,7 @@ BFuture invokeMethodAsyncSequentially(BObject object, String methodName, String * This method needs to be called if both object.getType().isIsolated() and * object.getType().isIsolated(methodName) returns true. */ - BFuture invokeMethodAsyncConcurrently(BObject object, String methodName, String strandName, + public abstract BFuture invokeMethodAsyncConcurrently(BObject object, String methodName, String strandName, StrandMetadata metadata, Callback callback, Map properties, Type returnType, Object... args); @@ -105,8 +105,9 @@ BFuture invokeMethodAsyncConcurrently(BObject object, String methodName, String * object.getType().isIsolated(methodName) returns true. */ @Deprecated - BFuture invokeMethodAsync(BObject object, String methodName, String strandName, StrandMetadata metadata, - Callback callback, Map properties, Type returnType, Object... args); + public abstract BFuture invokeMethodAsync(BObject object, String methodName, String strandName, + StrandMetadata metadata, Callback callback, + Map properties, Type returnType, Object... args); /** * Invoke Object method asynchronously. This will schedule the function and block the strand. @@ -127,12 +128,12 @@ BFuture invokeMethodAsync(BObject object, String methodName, String strandName, * object.getType().isIsolated(methodName) returns true. */ @Deprecated - Object invokeMethodAsync(BObject object, String methodName, String strandName, StrandMetadata metadata, - Callback callback, Object... args); + public abstract Object invokeMethodAsync(BObject object, String methodName, String strandName, + StrandMetadata metadata, Callback callback, Object... args); - void registerListener(BObject listener); + public abstract void registerListener(BObject listener); - void deregisterListener(BObject listener); + public abstract void deregisterListener(BObject listener); - void registerStopHandler(BFunctionPointer stopHandler); + public abstract void registerStopHandler(BFunctionPointer stopHandler); } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java index 4718728da469..8df2c36e967e 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java @@ -33,7 +33,7 @@ * * @since 2201.6.0 */ -public class BalEnvironment implements Environment { +public class BalEnvironment extends Environment { private final Strand strand; private BalFuture future; diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalFuture.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalFuture.java index 265f027767f6..518b0fe5c361 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalFuture.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalFuture.java @@ -32,7 +32,7 @@ * * @since 2201.6.0 */ -public class BalFuture implements Future { +public class BalFuture extends Future { private final Strand strand; private final AtomicBoolean visited = new AtomicBoolean(); diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java index 8668441cc838..e04ffec9d323 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalRuntime.java @@ -45,7 +45,7 @@ * * @since 2201.6.0 */ -public class BalRuntime implements Runtime { +public class BalRuntime extends Runtime { private final Scheduler scheduler; From 8b6aedd4fd02990c2b8928b098692f55deeacf9b Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Tue, 2 May 2023 16:38:54 +0530 Subject: [PATCH 20/28] Add deprecated getCurrentRuntime method back --- .../io/ballerina/runtime/api/Environment.java | 5 +++++ .../java/io/ballerina/runtime/api/Runtime.java | 16 ++++++++++++++++ .../runtime/internal/BalEnvironment.java | 5 +++++ 3 files changed, 26 insertions(+) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java index 41f4a13f05f7..1e8c00269f43 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java @@ -54,6 +54,11 @@ public abstract class Environment { */ public abstract Future markAsync(); + /** + * Gets the instance of Ballerina runtime. + * + * @return Ballerina runtime instance. + */ public abstract Runtime getRuntime(); /** diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java index ea7f2b286708..8991069d53b6 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Runtime.java @@ -23,6 +23,9 @@ import io.ballerina.runtime.api.values.BFunctionPointer; import io.ballerina.runtime.api.values.BFuture; import io.ballerina.runtime.api.values.BObject; +import io.ballerina.runtime.internal.BalRuntime; +import io.ballerina.runtime.internal.scheduling.Scheduler; +import io.ballerina.runtime.internal.scheduling.Strand; import java.util.Map; @@ -33,6 +36,19 @@ */ public abstract class Runtime { + // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 + /** + * Gets the instance of Ballerina runtime. + * + * @return Ballerina runtime instance. + * @deprecated use {@link Environment#getRuntime()} instead. + */ + @Deprecated(forRemoval = true) + public static Runtime getCurrentRuntime() { + Strand strand = Scheduler.getStrand(); + return new BalRuntime(strand.scheduler); + } + /** * Invoke Object method asynchronously and sequentially. This method will ensure that the object methods are * invoked in the same thread where other object methods are executed. So, the methods will be executed diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java index 8df2c36e967e..e56e48422999 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java @@ -89,6 +89,11 @@ public BalFuture markAsync() { return future; } + /** + * Gets the instance of Ballerina runtime. + * + * @return Ballerina runtime instance. + */ public BalRuntime getRuntime() { return new BalRuntime(strand.scheduler); } From fb65e40b3e1300337524375cdece55a70d91c234 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Mon, 8 May 2023 10:41:05 +0530 Subject: [PATCH 21/28] Address PR review comments --- .../src/main/java/io/ballerina/runtime/api/Environment.java | 2 +- .../main/java/io/ballerina/runtime/internal/BalEnvironment.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java index 1e8c00269f43..e2d5bcd31488 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java @@ -55,7 +55,7 @@ public abstract class Environment { public abstract Future markAsync(); /** - * Gets the instance of Ballerina runtime. + * Gets an instance of Ballerina runtime. * * @return Ballerina runtime instance. */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java index e56e48422999..8ccda843b3ad 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/BalEnvironment.java @@ -90,7 +90,7 @@ public BalFuture markAsync() { } /** - * Gets the instance of Ballerina runtime. + * Gets an instance of Ballerina runtime. * * @return Ballerina runtime instance. */ From fc68a1591a3c12a8b7333c9b3666a0bcdb3f3e54 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Mon, 8 May 2023 10:44:32 +0530 Subject: [PATCH 22/28] Extend BObject with BRefValue --- .../src/main/java/io/ballerina/runtime/api/values/BObject.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BObject.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BObject.java index 0e15e00629c1..1c4ab38c473d 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BObject.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/values/BObject.java @@ -21,7 +21,6 @@ import io.ballerina.runtime.api.types.Type; import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.internal.scheduling.Strand; -import io.ballerina.runtime.internal.values.RefValue; import java.util.HashMap; @@ -32,7 +31,7 @@ * * @since 1.1.0 */ -public interface BObject extends RefValue { +public interface BObject extends BRefValue { // TODO: remove this with https://github.com/ballerina-platform/ballerina-lang/issues/40175 @Deprecated(since = "2201.6.0", forRemoval = true) From d13db2539ad6413cf5f2da2420f1533d09dd52d2 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Tue, 9 May 2023 12:32:53 +0530 Subject: [PATCH 23/28] Change runtime casts of RefValue to BRefValue --- .../ballerina/runtime/internal/util/StringUtils.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java index 5cddb6daca7a..cbb104e18bd4 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java @@ -25,7 +25,7 @@ import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BLink; -import io.ballerina.runtime.api.values.BObject; +import io.ballerina.runtime.api.values.BRefValue; import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.api.values.BValue; import io.ballerina.runtime.internal.BalStringUtils; @@ -38,7 +38,7 @@ import io.ballerina.runtime.internal.values.ArrayValue; import io.ballerina.runtime.internal.values.DecimalValue; import io.ballerina.runtime.internal.values.MapValueImpl; -import io.ballerina.runtime.internal.values.RefValue; +import io.ballerina.runtime.internal.values.ObjectValue; /** * Common utility methods used for String manipulation. @@ -91,11 +91,11 @@ public static String getStringVal(Object value, BLink parent) { } if (type.getTag() == TypeTags.TABLE_TAG) { - return ((RefValue) value).informalStringValue(parent); + return ((BRefValue) value).informalStringValue(parent); } if (type.getTag() == TypeTags.OBJECT_TYPE_TAG) { - BObject objectValue = (BObject) value; + ObjectValue objectValue = (ObjectValue) value; ObjectType objectType = (ObjectType) TypeUtils.getReferredType(objectValue.getType()); for (MethodType func : objectType.getMethods()) { if (func.getName().equals(TO_STRING) && func.getParameters().length == 0 && @@ -162,7 +162,7 @@ public static String getExpressionStringVal(Object value, BLink parent) { } if (type.getTag() == TypeTags.TABLE_TAG) { - return ((RefValue) value).expressionStringValue(parent); + return ((BRefValue) value).expressionStringValue(parent); } if (type.getTag() == TypeTags.OBJECT_TYPE_TAG) { @@ -176,7 +176,7 @@ public static String getExpressionStringVal(Object value, BLink parent) { } } - RefValue refValue = (RefValue) value; + BRefValue refValue = (BRefValue) value; return refValue.expressionStringValue(parent); } From c8d90937aed0b90242a2f7a8d0171d1af0054f7d Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Tue, 9 May 2023 14:52:00 +0530 Subject: [PATCH 24/28] Change codegen instance of check to BRefValue --- .../wso2/ballerinalang/compiler/bir/codegen/JvmCastGen.java | 4 ++-- .../wso2/ballerinalang/compiler/bir/codegen/JvmConstants.java | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCastGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCastGen.java index 1ccc31cd2f09..c9fda98d52fc 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCastGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmCastGen.java @@ -68,6 +68,7 @@ import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BERROR; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BMP_STRING_VALUE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BOOLEAN_VALUE; +import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BREF_VALUE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BYTE_VALUE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.B_HANDLE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.B_OBJECT; @@ -89,7 +90,6 @@ import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.MAP_VALUE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.NUMBER; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.OBJECT; -import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.REF_VALUE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.REG_EXP_VALUE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.SIMPLE_VALUE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.STREAM_VALUE; @@ -727,7 +727,7 @@ private void generateJCastToBAny(MethodVisitor mv, BIRVarToJVMIndexMap indexMap, } mv.visitInsn(DUP); - mv.visitTypeInsn(INSTANCEOF, REF_VALUE); + mv.visitTypeInsn(INSTANCEOF, BREF_VALUE); mv.visitJumpInsn(IFNE, afterHandle); int returnJObjectVarRefIndex = indexMap.addIfNotExists("$_ret_jobject_val_$", symbolTable.anyType); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmConstants.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmConstants.java index bfefd0ade9d9..bb8c733dbffe 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmConstants.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmConstants.java @@ -46,6 +46,7 @@ public class JvmConstants { public static final String ARRAY_VALUE = "io/ballerina/runtime/internal/values/ArrayValue"; public static final String OBJECT_VALUE = "io/ballerina/runtime/internal/values/ObjectValue"; public static final String ABSTRACT_OBJECT_VALUE = "io/ballerina/runtime/internal/values/AbstractObjectValue"; + public static final String BREF_VALUE = "io/ballerina/runtime/api/values/BRefValue"; public static final String REF_VALUE = "io/ballerina/runtime/internal/values/RefValue"; public static final String ERROR_VALUE = "io/ballerina/runtime/internal/values/ErrorValue"; public static final String BERROR = "io/ballerina/runtime/api/values/BError"; From 879282d2d5d32d87c1b7f040bd7dc9b1356328b7 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Tue, 9 May 2023 15:39:13 +0530 Subject: [PATCH 25/28] Change RefValue usages to BRefValue --- .../ballerina/runtime/internal/JsonInternalUtils.java | 10 +++++----- .../java/io/ballerina/runtime/internal/TableUtils.java | 6 +++--- .../io/ballerina/runtime/internal/TypeChecker.java | 8 ++++---- .../runtime/internal/scheduling/WorkerUtils.java | 1 + .../runtime/internal/values/ArrayValueImpl.java | 9 +++++---- .../ballerina/runtime/internal/values/ErrorValue.java | 5 +++-- .../runtime/internal/values/MapValueImpl.java | 7 ++++--- .../runtime/internal/values/TableValueImpl.java | 7 ++++--- .../runtime/internal/values/TupleValueImpl.java | 9 +++++---- 9 files changed, 34 insertions(+), 28 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/JsonInternalUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/JsonInternalUtils.java index 6d848baa7584..acf0f51b6ecf 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/JsonInternalUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/JsonInternalUtils.java @@ -32,6 +32,7 @@ import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BMap; +import io.ballerina.runtime.api.values.BRefValue; import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.api.values.BTable; import io.ballerina.runtime.internal.types.BArrayType; @@ -52,7 +53,6 @@ import io.ballerina.runtime.internal.values.MapValue; import io.ballerina.runtime.internal.values.MapValueImpl; import io.ballerina.runtime.internal.values.MappingInitialValueEntry; -import io.ballerina.runtime.internal.values.RefValue; import java.math.BigDecimal; import java.util.ArrayList; @@ -232,10 +232,10 @@ public static void setElement(Object json, String elementName, Object element) { * @return returns true if provided JSON is a JSON Array. */ public static boolean isJSONArray(Object json) { - if (!(json instanceof RefValue)) { + if (!(json instanceof BRefValue)) { return false; } - return ((RefValue) json).getType().getTag() == TypeTags.ARRAY_TAG; + return ((BRefValue) json).getType().getTag() == TypeTags.ARRAY_TAG; } /** @@ -245,11 +245,11 @@ public static boolean isJSONArray(Object json) { * @return returns true if provided JSON is a JSON Object. */ public static boolean isJSONObject(Object json) { - if (!(json instanceof RefValue)) { + if (!(json instanceof BRefValue)) { return false; } - Type type = TypeUtils.getReferredType(((RefValue) json).getType()); + Type type = TypeUtils.getReferredType(((BRefValue) json).getType()); int typeTag = type.getTag(); return typeTag == TypeTags.MAP_TAG || typeTag == TypeTags.RECORD_TYPE_TAG; } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TableUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TableUtils.java index 873b37168b60..dee3468a87e7 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TableUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TableUtils.java @@ -20,12 +20,12 @@ import io.ballerina.runtime.api.TypeTags; import io.ballerina.runtime.api.creators.ErrorCreator; import io.ballerina.runtime.api.types.Type; +import io.ballerina.runtime.api.values.BRefValue; import io.ballerina.runtime.internal.util.exceptions.BLangExceptionHelper; import io.ballerina.runtime.internal.util.exceptions.RuntimeErrors; import io.ballerina.runtime.internal.values.ArrayValue; import io.ballerina.runtime.internal.values.IteratorValue; import io.ballerina.runtime.internal.values.MapValue; -import io.ballerina.runtime.internal.values.RefValue; import io.ballerina.runtime.internal.values.RegExpValue; import io.ballerina.runtime.internal.values.TableValue; @@ -56,7 +56,7 @@ public static Long hash(Object obj, Node parent) { return 0L; } - if (obj instanceof RefValue) { + if (obj instanceof BRefValue) { Node node = new Node(obj, parent); @@ -65,7 +65,7 @@ public static Long hash(Object obj, Node parent) { .getErrorDetails(RuntimeErrors.CYCLIC_VALUE_REFERENCE, TypeChecker.getType(obj))); } - RefValue refValue = (RefValue) obj; + BRefValue refValue = (BRefValue) obj; Type refType = refValue.getType(); if (refType.getTag() == TypeTags.MAP_TAG || refType.getTag() == TypeTags.RECORD_TYPE_TAG) { MapValue mapValue = (MapValue) refValue; diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java index 72554cc107d8..19bc44834a2f 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java @@ -35,6 +35,7 @@ import io.ballerina.runtime.api.values.BDecimal; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BObject; +import io.ballerina.runtime.api.values.BRefValue; import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.api.values.BValue; import io.ballerina.runtime.api.values.BXml; @@ -69,7 +70,6 @@ import io.ballerina.runtime.internal.values.HandleValue; import io.ballerina.runtime.internal.values.MapValue; import io.ballerina.runtime.internal.values.MapValueImpl; -import io.ballerina.runtime.internal.values.RefValue; import io.ballerina.runtime.internal.values.RegExpValue; import io.ballerina.runtime.internal.values.StreamValue; import io.ballerina.runtime.internal.values.TableValueImpl; @@ -527,8 +527,8 @@ public static TypedescValue getTypedesc(Object value) { if (isSimpleBasicType(type)) { return new TypedescValueImpl(new BFiniteType(value.toString(), Set.of(value), 0)); } - if (value instanceof RefValue) { - return (TypedescValue) ((RefValue) value).getTypedesc(); + if (value instanceof BRefValue) { + return (TypedescValue) ((BRefValue) value).getTypedesc(); } return new TypedescValueImpl(type); } @@ -2061,7 +2061,7 @@ private static boolean isMutable(Object value, Type sourceType) { return false; } - return !((RefValue) value).isFrozen(); + return !((BRefValue) value).isFrozen(); } private static boolean checkIsNeverTypeOrStructureTypeWithARequiredNeverMember(Type type) { diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/WorkerUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/WorkerUtils.java index 87e68822a4dc..0cbbf3cd5747 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/WorkerUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/WorkerUtils.java @@ -42,4 +42,5 @@ public static void handleWorkerError(RefValue value, Strand strand, ChannelDetai } } + private WorkerUtils() {} } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java index 25f06a3646b2..9af49f9460d3 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java @@ -30,6 +30,7 @@ import io.ballerina.runtime.api.values.BIterator; import io.ballerina.runtime.api.values.BLink; import io.ballerina.runtime.api.values.BListInitialValueEntry; +import io.ballerina.runtime.api.values.BRefValue; import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.api.values.BTypedesc; import io.ballerina.runtime.api.values.BValue; @@ -807,8 +808,8 @@ public Object copy(Map refs) { valueArray = new ArrayValueImpl(values, arrayType); IntStream.range(0, this.size).forEach(i -> { Object value = this.refValues[i]; - if (value instanceof RefValue) { - values[i] = ((RefValue) value).copy(refs); + if (value instanceof BRefValue) { + values[i] = ((BRefValue) value).copy(refs); } else { values[i] = value; } @@ -975,8 +976,8 @@ public void freezeDirect() { if (this.elementType == null || this.elementReferredType.getTag() > TypeTags.BOOLEAN_TAG) { for (int i = 0; i < this.size; i++) { Object value = this.getRefValue(i); - if (value instanceof RefValue) { - ((RefValue) value).freezeDirect(); + if (value instanceof BRefValue) { + ((BRefValue) value).freezeDirect(); } } } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ErrorValue.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ErrorValue.java index 2ca7b88f14b7..120259dd3a4a 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ErrorValue.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ErrorValue.java @@ -27,6 +27,7 @@ import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BLink; +import io.ballerina.runtime.api.values.BRefValue; import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.api.values.BTypedesc; import io.ballerina.runtime.api.values.BValue; @@ -258,8 +259,8 @@ public BString getErrorMessage() { * @return detail record */ public Object getDetails() { - if (details instanceof RefValue) { - return ((RefValue) details).frozenCopy(new HashMap<>()); + if (details instanceof BRefValue) { + return ((BRefValue) details).frozenCopy(new HashMap<>()); } return details; } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java index 6efeee591740..9ba2c28c1129 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java @@ -29,6 +29,7 @@ import io.ballerina.runtime.api.values.BMap; import io.ballerina.runtime.api.values.BMapInitialValueEntry; import io.ballerina.runtime.api.values.BObject; +import io.ballerina.runtime.api.values.BRefValue; import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.api.values.BTypedesc; import io.ballerina.runtime.api.values.BValue; @@ -450,7 +451,7 @@ public Object copy(Map refs) { refs.put(this, newMap); for (Map.Entry entry : this.entrySet()) { V value = entry.getValue(); - value = value instanceof RefValue ? (V) ((RefValue) value).copy(refs) : value; + value = value instanceof BRefValue ? (V) ((BRefValue) value).copy(refs) : value; newMap.put(entry.getKey(), value); } return newMap; @@ -528,8 +529,8 @@ public void freezeDirect() { this.referredType = ReadOnlyUtils.setImmutableTypeAndGetEffectiveType(this.referredType); this.values().forEach(val -> { - if (val instanceof RefValue) { - ((RefValue) val).freezeDirect(); + if (val instanceof BRefValue) { + ((BRefValue) val).freezeDirect(); } }); this.typedesc = null; diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TableValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TableValueImpl.java index 9041caee9ed8..c34bc5397ca7 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TableValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TableValueImpl.java @@ -30,6 +30,7 @@ import io.ballerina.runtime.api.values.BLink; import io.ballerina.runtime.api.values.BMap; import io.ballerina.runtime.api.values.BObject; +import io.ballerina.runtime.api.values.BRefValue; import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.api.values.BTypedesc; import io.ballerina.runtime.internal.CycleUtils; @@ -181,7 +182,7 @@ public Object copy(Map refs) { while (itr.hasNext()) { TupleValueImpl tupleValue = (TupleValueImpl) itr.next(); Object value = tupleValue.get(1); - value = value instanceof RefValue ? ((RefValue) value).copy(refs) : value; + value = value instanceof BRefValue ? ((BRefValue) value).copy(refs) : value; clone.add((V) value); } @@ -374,8 +375,8 @@ public void freezeDirect() { this.tableType = (BTableType) ReadOnlyUtils.setImmutableTypeAndGetEffectiveType(this.tableType); this.type = ReadOnlyUtils.setImmutableTypeAndGetEffectiveType(this.type); - //we know that values are always RefValues - this.values().forEach(val -> ((RefValue) val).freezeDirect()); + //we know that values are always BRefValues + this.values().forEach(val -> ((BRefValue) val).freezeDirect()); this.typedesc = null; } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TupleValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TupleValueImpl.java index bd3eda84d883..3ceecdbdfb94 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TupleValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TupleValueImpl.java @@ -26,6 +26,7 @@ import io.ballerina.runtime.api.values.BIterator; import io.ballerina.runtime.api.values.BLink; import io.ballerina.runtime.api.values.BListInitialValueEntry; +import io.ballerina.runtime.api.values.BRefValue; import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.api.values.BTypedesc; import io.ballerina.runtime.api.values.BValue; @@ -508,8 +509,8 @@ public Object copy(Map refs) { refs.put(this, refValueArray); IntStream.range(0, this.size).forEach(i -> { Object value = this.refValues[i]; - if (value instanceof RefValue) { - values[i] = ((RefValue) value).copy(refs); + if (value instanceof BRefValue) { + values[i] = ((BRefValue) value).copy(refs); } else { values[i] = value; } @@ -595,8 +596,8 @@ public void freezeDirect() { this.tupleType = (TupleType) TypeUtils.getReferredType(type); for (int i = 0; i < this.size; i++) { Object value = this.get(i); - if (value instanceof RefValue) { - ((RefValue) value).freezeDirect(); + if (value instanceof BRefValue) { + ((BRefValue) value).freezeDirect(); } } this.typedesc = null; From c24a2fbd2b99c9dd6dc76d45237874379999be35 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Thu, 11 May 2023 12:08:56 +0530 Subject: [PATCH 26/28] Remove unnecessary blank lines --- .../runtime/internal/util/StringUtils.java | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java index cbb104e18bd4..60a965c3879b 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/util/StringUtils.java @@ -63,37 +63,28 @@ public static String getStringVal(Object value, BLink parent) { if (value == null) { return ""; } - Type type = TypeUtils.getReferredType(TypeChecker.getType(value)); - if (type.getTag() == TypeTags.STRING_TAG) { return ((BString) value).getValue(); } - if (type.getTag() < TypeTags.NULL_TAG) { return String.valueOf(value); } - CycleUtils.Node node = new CycleUtils.Node(value, parent); - if (node.hasCyclesSoFar()) { return STR_CYCLE; } - if (type.getTag() == TypeTags.MAP_TAG || type.getTag() == TypeTags.RECORD_TYPE_TAG) { MapValueImpl mapValue = (MapValueImpl) value; return mapValue.stringValue(parent); } - if (type.getTag() == TypeTags.ARRAY_TAG || type.getTag() == TypeTags.TUPLE_TAG) { ArrayValue arrayValue = (ArrayValue) value; return arrayValue.stringValue(parent); } - if (type.getTag() == TypeTags.TABLE_TAG) { return ((BRefValue) value).informalStringValue(parent); } - if (type.getTag() == TypeTags.OBJECT_TYPE_TAG) { ObjectValue objectValue = (ObjectValue) value; ObjectType objectType = (ObjectType) TypeUtils.getReferredType(objectValue.getType()); @@ -104,7 +95,6 @@ public static String getStringVal(Object value, BLink parent) { } } } - BValue bValue = (BValue) value; return bValue.stringValue(parent); } @@ -120,18 +110,14 @@ public static String getExpressionStringVal(Object value, BLink parent) { if (value == null) { return "()"; } - Type type = TypeUtils.getReferredType(TypeChecker.getType(value)); - if (type.getTag() == TypeTags.STRING_TAG) { return "\"" + ((BString) value).getValue() + "\""; } - if (type.getTag() == TypeTags.DECIMAL_TAG) { DecimalValue decimalValue = (DecimalValue) value; return decimalValue.expressionStringValue(parent); } - if (type.getTag() == TypeTags.FLOAT_TAG) { if (Double.isNaN((Double) value)) { return "float:" + value; @@ -140,31 +126,24 @@ public static String getExpressionStringVal(Object value, BLink parent) { return "float:" + value; } } - if (type.getTag() < TypeTags.NULL_TAG) { return String.valueOf(value); } - CycleUtils.Node node = new CycleUtils.Node(value, parent); - if (node.hasCyclesSoFar()) { return STR_CYCLE + "[" + node.getIndex() + "]"; } - if (type.getTag() == TypeTags.MAP_TAG || type.getTag() == TypeTags.RECORD_TYPE_TAG) { MapValueImpl mapValue = (MapValueImpl) value; return mapValue.expressionStringValue(parent); } - if (type.getTag() == TypeTags.ARRAY_TAG || type.getTag() == TypeTags.TUPLE_TAG) { ArrayValue arrayValue = (ArrayValue) value; return arrayValue.expressionStringValue(parent); } - if (type.getTag() == TypeTags.TABLE_TAG) { return ((BRefValue) value).expressionStringValue(parent); } - if (type.getTag() == TypeTags.OBJECT_TYPE_TAG) { AbstractObjectValue objectValue = (AbstractObjectValue) value; ObjectType objectType = (ObjectType) TypeUtils.getReferredType(objectValue.getType()); @@ -175,7 +154,6 @@ public static String getExpressionStringVal(Object value, BLink parent) { } } } - BRefValue refValue = (BRefValue) value; return refValue.expressionStringValue(parent); } From 35bb2196a2fdbc5c7809a8859198326afe0f218a Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Mon, 15 May 2023 12:56:31 +0530 Subject: [PATCH 27/28] Address PR review comments --- .../src/main/java/io/ballerina/runtime/api/Environment.java | 2 +- .../main/java/io/ballerina/runtime/api/async/Callback.java | 4 ++-- .../main/java/io/ballerina/runtime/api/types/HandleType.java | 2 +- .../java/io/ballerina/runtime/internal/types/BHandleType.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java index e2d5bcd31488..f04d559075db 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/Environment.java @@ -23,7 +23,7 @@ import java.util.Optional; /** - * When this interface is used as the first argument of an interop method, Ballerina will inject an instance of + * When this class is used as the first argument of an interop method, Ballerina will inject an instance of * the class when calling. That instance can be used to communicate with currently executing Ballerina runtime. * * @since 2.0.0 diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/async/Callback.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/async/Callback.java index 91a959f64336..daea9f7c8a2a 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/async/Callback.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/async/Callback.java @@ -30,7 +30,7 @@ public interface Callback { /** * This should be called when you want to notify that your operation * is done successfully. - * @param result the result to be reported when the operation passed + * @param result the result to be reported when the operation succeeds */ void notifySuccess(Object result); @@ -38,7 +38,7 @@ public interface Callback { * This should be called to notify the listener that your operation * failed with a specific error. * - * @param error the error to be reported when the operation failed + * @param error the error to be reported when the operation fails */ void notifyFailure(BError error); diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/HandleType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/HandleType.java index 18ca63e1f6f5..71b3aa31023f 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/HandleType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/HandleType.java @@ -19,7 +19,7 @@ /** * {@code HandleType} represents a handle type in Ballerina. - * A handle value is a reference to storage managed externally to a Ballerina program. + * A handle value is a reference to a storage managed externally by a Ballerina program. * * @since 1.0.0 */ diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BHandleType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BHandleType.java index 7de79e92454c..89b5482936bf 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BHandleType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BHandleType.java @@ -24,7 +24,7 @@ /** * {@code BHandleType} represents a handle type in Ballerina. - * A handle value is a reference to storage managed externally to a Ballerina program. + * A handle value is a reference to a storage managed externally by a Ballerina program. * * @since 1.0.0 */ From d916107c901b00ea0d5284469eaa0cfb4b2948a2 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Tue, 16 May 2023 14:55:28 +0530 Subject: [PATCH 28/28] Move PredefinedTypes private constructor --- .../main/java/io/ballerina/runtime/api/PredefinedTypes.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/PredefinedTypes.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/PredefinedTypes.java index 90c8ea37bf75..a9635fbc87d9 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/PredefinedTypes.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/PredefinedTypes.java @@ -187,6 +187,8 @@ public class PredefinedTypes { public static final Type ANY_AND_READONLY_TYPE = ReadOnlyUtils.setImmutableTypeAndGetEffectiveType(TYPE_ANY); public static final Type ANY_AND_READONLY_OR_ERROR_TYPE; + private PredefinedTypes() {} + // type anydata = ()|boolean|int|float|decimal|string|xml|anydata[]|map|table> static { ArrayList members = new ArrayList<>(); @@ -202,9 +204,6 @@ public class PredefinedTypes { TYPE_ANYDATA_ARRAY = new BArrayType(TYPE_ANYDATA); } - private PredefinedTypes() { - } - private static BAnydataType getAnydataType(List members, String typeName, boolean readonly) { BAnydataType anydataType = new BAnydataType(new BUnionType(TypeConstants.ANYDATA_TNAME, EMPTY_MODULE, members , readonly), typeName, readonly);