ballerina/reflect module

Module overview

This module provides utility methods for obtaining reflective information about the Ballerina runtime.

Samples

Get service annotations

The sample below shows how to retrieve all the annotations of a service:

@http:ServiceConfig { basePath: "/helloWorld" }
service hello on new http:Listener(9090) {

    resource function hello(http:Caller caller, http:Request req) {
        http:Response res = new;
        res.setTextPayload("hello world");
        var result = caller->respond(res);
    }
}

reflect:annotationData[] annotations = reflect:getServiceAnnotations(hello); 
string annoName = annotations[0].name; // E.g. “ServiceConfig”
string annoPkg = annotations[0].moduleName; // E.g. “ballerina.http”

public type annotationData

Field Name Data Type Default Value Description
name string
moduleName string
moduleVersion string
value reflect:$anonType$0

public type anyStruct

public function getFunctionAnnotations(any functionPointer) returns (annotationData[])

Parameter Name Data Type Default Value Description
functionPointer any
Return Type Description
annotationData[]

public function getResourceAnnotations(service serviceType, string resourceName) returns (annotationData[])

Parameter Name Data Type Default Value Description
serviceType service
resourceName string
Return Type Description
annotationData[]

public function getServiceAnnotations(service serviceType) returns (annotationData[])

Parameter Name Data Type Default Value Description
serviceType service
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[]