I am trying to implement a simple annotation processor with ballerina (jBallerina-1.0.5 on MacOS Catalina). In this example, I need to:
- Iterate through available
resource
functions of a givenservice
- For each iteration, get values of a given
@annotation
as annotated in theresource
For an example:
...
service my_service on new http:Listener(8080) {
@my_annonatation {
value: "my-value-for-resource-1"
}
resource function my_resource_1() {}
@my_annonatation {
value: "my-value-for-resource-2"
}
resource function my_resource_2() {}
}
According to the above service
code snippet, I need get resource
names as ["my_resource_1", "my_resource_2"]
and @annotation
value as my-value-for-resource-1
,my-value-for-resource-2
for my_resource_1
and my_resource_2
accordingly.
My questions are:
- How can I get the
resource
s defined in a givenservice
with ballerina - I have tried with
function getServiceAnnotations(service serviceType, string annotName, string? moduleName = ()) returns any
ofballerina/reflect
module to get the@annotation
value. But it doesn't give anything in return (Apparently it returns an empty string or could be null).
I don't see any good article for ballerina annotation processing, except for old ballerina 0.x pre-releases 1. I found that the ballerina/reflect
module is now updated 2 3 since 0.x pre-releases as I compared with the ballerina source code (but it has not mentioned in ballerina release notes 4).
And also the article "Extending Ballerina" 5 has mentioned an alternative way for annotation processing through compiler extensions which delegates the operation to Java. However we have a limitation to push Java dependancies into the ballerina central
.
Please suggest me a workaround to get the expected result and it is highly appreciated the code in pure ballerina language.
References