ballerina/reflect package
Package overview
This package provides utility methods for obtaining reflective information about the Ballerina runtime.
Samples
Check whether two objects are equal
The reflect:equals function checks whether two objects in the Ballerina runtime, including arrays, are equal.
The samples below show how to compare primitive types.
// Compare two strings.
string s1 = "a";
string s2 = "a";
boolean equal = reflect:equals(s1,s2); // Returns `true`
// Compare two string arrays.
string[] s1 = ["a", "b"];
string[] s2 = ["a", "b"];
boolean equalArrays = reflect:equals(s1,s2); // Returns `true`
The sample below shows how to compare complex types.
// Compare two JSON objects.
json jObj1 = { name:"Target",
location:{
address1:"19, sample road",
postalCode: 6789
},
products:[{price: 40.50, isNew: true, name:"apple"},
{name:"orange", price: 30.50}],
manager: ()
};
json jObj2 = { name:"Target",
location:{
address1:"19, sample road",
postalCode: 6789
},
products:[{price: 40.50, isNew: true, name:"apple"},
{name:"orange", price: 30.50}],
manager: ()
};
boolean equalJsons = reflect:equals(jObj1,jObj2);
Get service annotations
The sample below shows how to retrieve all the annotations of a service:
@http:ServiceConfig { basePath: "/helloWorld" }
service<http:Service> hello bind { port: 9090 } {
hello(endpoint caller, http:Request req) {
http:Response res = new;
res.setTextPayload("hello world");
var result = caller -> respond(res);
}
}
annotationData[] annotations= reflect:getServiceAnnotations(hello);
string annoName = annotations[0].name; //Eg. “ServiceConfig”
string annoPkg = annotations[0].package; //Eg/ “ballerina.http”
Package contents
Records Summary
| Record | Description | ||
|---|---|---|---|
| annotationData | |||
| anyStruct |
Functions Summary
| Return Type | Function and Description | ||
|---|---|---|---|
| boolean | equals(any value1, any value2) Check whether 2 values are deeply equal. Supports string, int, float, boolean, type, structs, maps, arrays, any, JSON. Any other type returns FALSE. |
||
| annotationData[] | getFunctionAnnotations(any functionPointer) |
||
| annotationData[] | getResourceAnnotations(typedesc serviceType, string resourceName) |
||
| annotationData[] | getServiceAnnotations(typedesc serviceType) |
||
| annotationData[] | getStructAnnotations(typedesc structType) |
||
| annotationData[] | getStructFieldAnnotations(typedesc structType, string fieldName) |
public type annotationData
| Field Name | Data Type | Default Value | Description |
|---|---|---|---|
| name | string | ||
| pkgName | string | ||
| value | reflect:anyStruct |
public type anyStruct
public function equals(any value1, any value2) returns (boolean)
Check whether 2 values are deeply equal. Supports string, int, float, boolean, type, structs, maps, arrays, any, JSON. Any other type returns FALSE.
| Parameter Name | Data Type | Default Value | Description |
|---|---|---|---|
| value1 | any | The first value for equality. |
|
| value2 | any | The second value for equality. |
| Return Type | Description | ||
|---|---|---|---|
| boolean | TRUE if values are deeply equal, else FALSE. |
public function getFunctionAnnotations(any functionPointer) returns (annotationData[])
| Parameter Name | Data Type | Default Value | Description |
|---|---|---|---|
| functionPointer | any |
| Return Type | Description | ||
|---|---|---|---|
| annotationData[] |
public function getResourceAnnotations(typedesc serviceType, string resourceName) returns (annotationData[])
| Parameter Name | Data Type | Default Value | Description |
|---|---|---|---|
| serviceType | typedesc | ||
| resourceName | string |
| Return Type | Description | ||
|---|---|---|---|
| annotationData[] |
public function getServiceAnnotations(typedesc serviceType) returns (annotationData[])
| Parameter Name | Data Type | Default Value | Description |
|---|---|---|---|
| serviceType | typedesc |
| Return Type | Description | ||
|---|---|---|---|
| annotationData[] |
public function getStructAnnotations(typedesc structType) returns (annotationData[])
| Parameter Name | Data Type | Default Value | Description |
|---|---|---|---|
| structType | typedesc |
| Return Type | Description | ||
|---|---|---|---|
| annotationData[] |
public function getStructFieldAnnotations(typedesc structType, string fieldName) returns (annotationData[])
| Parameter Name | Data Type | Default Value | Description |
|---|---|---|---|
| structType | typedesc | ||
| fieldName | string |
| Return Type | Description | ||
|---|---|---|---|
| annotationData[] |