Annotations in Ballerina

What are Annotations?


An annotation is a ballerina code snippet that can be attached to some ballerina code, and will hold some metadeta related to that attached code. Annotations are not executable, but can be used to alter the behavior of some executable code.

Sample:
@http:POST{}
@http:BasePath("/pizzaService")
service MyService {
   ...
   ...
}


Where Can I attach an Annotation?

Annotations can be attached to any one of the following:

  • Services
  • Resources
  • Functions
  • Connectors
  • Actions (of Connectors)
  • TypeMappers
  • Structs
  • Constants
  • Annotations

However, this differs for different types of annotations. For example, http:BasePath{} can only be attached to a Service, here as http:POST{} can only be attached to a resource. This is specified in the definition of each annotation.


What are the inbuilt ones?

Currently (in ballerina v0.85), there are inbuilt annotations in four packages, each are for HTTP, JMS, WebSockets, and Documentations. You can find the definitions of each annotations in [1], [2], [3], and [4] respectively.


Can I define my own Annotation?

You can define your own annotation and apply it anywhere you want. A definition of an annotation takes the following form:
package foo.bar;
annotation MyAnnotation attach service, resource, function {
    string status;
    int code;
}

Here the starting 'annotation' is a keyword that describes this is an annotation definition, followed by the name of your annotation. Then you can specify to where this annotation can be applied, after the keyword 'attach'. In the above sample, my 'MyAnnotation' is allowed to be attached to any service, resource, or a function (or all of them).

You can apply the above annotation in a function like below:
@bar:MyAnnotation{status:"working", code:1}
function myFunction(string str) {
    ...
}

Note that, if the function is in the same package as the annotation, then you need to ignore the preceding package name in the annotation attachment.
e.g.:
@MyAnnotation{status:"working", code:1}
function myFunction(string str) {
    ...
}

What can be the types of attributes?

Type of an attribute of an annotation can be on of the following:
  • A value type (string, int, float, boolean).
  • An Annotation.
  • Array of any of above.


Default Values for Annotation Attributes?

Indeed. Attributes of an annotation can have default values, But only for attributes of value types (string, int, float, boolean).
e.g:

annotation MyAnnotation attach service, resource, function {
    string status = "failed";
    int code = 0;
}

This means, if you have an attribute of an array type (eg: int[] a), or an attribute of another annotation (eg MyAnnotation a;), then those attributes cannot have default values.


Annotate an Annotation by itself?

You can also annotate an annotation using the same annotation itself. For example, say we write an annotations called 'Desciption' to add an dexscription to any of the ballerina construct we write. And suppose we need to add a description to that 'Description' annotation. We can do it as follows:

@Description{value:"This is an annotation that can be used to describe a construct"}
annotation Description attach service, annotation, function {
    string value;
}



References

[1] https://github.com/ballerinalang/ballerina/blob/master/modules/ballerina-native/src/main/ballerina/ballerina/net/http/annotation.bal
[2] https://github.com/ballerinalang/ballerina/blob/master/modules/ballerina-native/src/main/ballerina/ballerina/net/jms/annotations.bal
[3] https://github.com/ballerinalang/ballerina/blob/master/modules/ballerina-native/src/main/ballerina/ballerina/net/ws/annotation.bal
[4] https://github.com/ballerinalang/ballerina/blob/master/modules/ballerina-native/src/main/ballerina/ballerina/doc/annotation.bal

Share:

30 comments

  1.  office.com/setup
    -Find how to download, introduce, activate all renditions of Microsoft Office setup and initiate on  www.office.com/setup

    ReplyDelete
  2. norton.com/setup – get vigorous antivirus security by following the means for download, introduce and activation of norton setup, go to www.norton.com/setup.
     norton.com/setup

    ReplyDelete
  3. Thanks for sharing this marvelous post. I m very pleased to read this article.
    office.com/setup

    www.office.com/setup

    ReplyDelete
  4. Thanks for sharing with Us. To fix all McAfee Security errors like McAfee Activate, download, install and fore more details visit here: mcafee.com/activate | McAfee Activate | www.mcafee.com/activate

    ReplyDelete
  5. Thank you so much for sharing post with us. looking McAfee Antivirus and how to activate mcafee security on windows?
    mcafee.com/activate | McAfee Activate

    ReplyDelete
  6. To brother printer go to the solutions.brother.com/windows or own call +1-833-207-7789 for brother printer setup support solution for brother wireless printer setup.

    ReplyDelete
  7. Here is the link to get the complete set of applications of the Microsoft office and McAfee Antivirus for your PC and Mac. You will receive the step by step complete setup for the installation process of the Application.
    |office.com/setup | office.com/setup | office.com/setup |mcafee.com/activate | billy mark | office.com/setup

    ReplyDelete
  8. norton.com/setup – Download, install, and finally activate Norton setup with product activation key on norton.com/setup. For more information just visit our website or call our norton customer support number.
    kaspersky activation code
    Office.com/setup

    ReplyDelete
  9. to download or install office setup just click on the link given below.
    Office.com/setup

    ReplyDelete
  10. Nice post! Thank you for posting.Myessayhelp.co.uk is giving assignment help to students.we are already trusted by thousands of students who struggle to write their academic papers and also by those students who simply want proofreading services. to save their time and make life easy.

    ReplyDelete
  11. If you wish to stay your device safe and need to stay your knowledge secure from every type of hackers and criminal minds then norton.com/setup is with you!
    Feel Free to contact us:
    norton.com/setup|norton.com/setup|norton.com/setup

    ReplyDelete
  12. CRM Software is a strategy that helps streamline your employers qualified prospects, opportunities, appointments, earnings and service, by trying to keep buyer as centre of the process.

    Web Based CRM Software | Online CRM Software | CRM Software Development | CRM Software Company in India | CRM Software

    ReplyDelete
  13. We will purchase Download, present and dynamic Norton Setup from norton-norton-norton.com. We will do Update and Upgrade of Norton setup arrangement for you. On the off chance that it's required then we will remotely get to your structure and unravel the issues.
    norton setup

    ReplyDelete
  14. For Downloading, Installing and activating the Office product, visit office.com/setup and Get Started with Office.

    ReplyDelete
  15. Roku is a small class tool that lets you view free and paying video content over the Internet on your TV.

    www.roku.com/link

    ReplyDelete
  16. Go to webroot safe website www.webroot.com/safe or webroot.com/safe from your browser.2.Sign in Webroot account with your

    Credentials.3.Enter your Webroot Keycode have 20 digits.4.Click on Next button to register your Webroot product.5.Accept the Agree and Install button to

    proceed.6.Finally your Webroot safe is activated successfully.
    This may be a best place to introduce yourself and your website or include some credits.
    www.webroot.com/safe
    webroot.com/safe
    webroot safe

    ReplyDelete
  17. I’m Sakht. I’m a writer living in London, UK. I am a fan of technology. You can visit my store.
    office.com/setup
    Norton.com/setup
    mcafee.com/activate

    ReplyDelete
  18. Get the best & affordable Microsoft www.office.com/setup support for your business with best package. Reach out us & enjoy our fastest & satisfactory services.

    ReplyDelete
  19. Hi, I’m so glad I found your blog, I stumbled upon you when I was already viewing other content on Google, I’m here now and want to thank you for this great post.
    And the surrounding entertainment sites please continue to maintain good work.
    netlimiter pro crack
    advanced installer crack
    zemana antimalware crack
    ableton live crack
    deep freeze standard crack

    ReplyDelete
  20. Sometimes, you might need to verify the credentials that have been submitted. This could be your email address, phone number or any other information that the website requires. These codes can be sent to you via email or text message along with your submitted information as you check out. The verification codes can be sent to you via text message, voice call or through
    www.primevideo/mytv
    primevideo/mytv
    primevideo/mytv
    primevideo/mytv

    ReplyDelete
  21. After you have successfully signed in, you will receive a code on the Roku channel. Log in to Roku to receive a code.
    primevideo/mytv
    primevideo/mytv
    primevideo/mytv
    www.amazon.com/mytv enter code

    ReplyDelete
  22. After you have successfully signed in, you will receive a code on the Roku channel. Log in to Roku to receive a code.
    espn.com/activate
    netflix.com/activate
    netflix.com/activate
    Xfinity.com/authorize
    Xfinity.com/authorize

    ReplyDelete
  23. You can now generate the Prime Video verification code for your Roku TV by following the instructions in this write-up. You can also register your device viawww.amazon.com/mytv.The steps in the blog's procedures are simple. If you have any questions, please contact Amazon customer service.
    Primevideo/mytv
    amazon.co.uk/mytv
    amazon.co.uk/mytv
    amazon.co.uk/mytv
    amazon.co.uk/mytv

    ReplyDelete
  24. Amazon.com/mytv This official link will allow you to activate your Amazon Prime membership. Prime VideoAmazon MyTVUsers are required to enter 6-digit authenticationAmazon my tv codesYou can access the online portal by visiting
    amazon.co.uk/mytv
    amazon.co.uk/redeem
    amazon.com/mytv
    www.Amazon.com/mytv
    amazon.com/mytv
    amazon.com/mytv

    ReplyDelete
  25. This is wonderful information share and excellent write-up providing by your article. Much love for sharing; Its very inspiring and interesting from your blog. Thank you for sharing. visit here unimed cut off mark for physiotherapy

    ReplyDelete
  26. Amazon Prime Video allows you to access media and entertainment faster and more efficiently. Amazon launched Prime Video to meet its users' expectations of unlimited movie and video selections, which can be accessed anywhere, anytime. Prime Video is available on many devices. To activate Prime Video on a specific device, however, you must register it.www.amazon.com/mytv. We have provided the steps to generate the activation Code and register your device on Amazon.com/mytv for Prime Video.
    amazon.com/code
    lowe's gift card balance
    amazon code
    target gift card balance
    Target gift card balance

    ReplyDelete
  27. Amazon Prime Video is the best platform to view the latest web series, movies and TV shows.Amazon.com/mytv. Amazon.com/mytv supports almost all devices. This includes Android and iOS devices. Amazon.com/mytv requires a high-speed internet connection and an amazon subscription.netflix.com/activate
    amazon.com/redeem
    target.com/checkbalance
    target.com/checkbalance

    ReplyDelete