Java custom annotations

      No Comments on Java custom annotations

We use a lot of java annotations in our day to day coding. Ever thought how these annotations works? How you can leverage their power by creating your own annotations? This article will explain how these java annotations works and how you can define your own java annotations.

Some java annotations

If you do TDD in java, you would have used @Test annotation on each and every test that you write. Also if you use ORMs like Hibernet or Libs to serialize and deserialize java objects e.g GSON or Jacson, you would have used annotations like @Expose or @JacsonXmlProperty etc. Libraries like junit, gson, jacson heavily use java annotations. If you have worked on spring apps there also you would see a lot of java annotations like @Autowired etc.

How to create custom java annotations

Java annotation is nothing but an interface and we all know that creating an interface just takes few lines of code and its very simple. The whole point is if you have clarity on the purpose of your custom annotation, then its damn simple to create one.

Lets take an example of Junit test runner. If you had to design your own test runner, how would you implement it?

To explain java custom annotations better I have created this small project on github which is nothing but a custom test runner. I have tests which are marked with @MyTest annotation. TestRunner will execute all the tests marked with @MyTest annotation. Lets see the code.

@MyTest

The above code creates @MyTest annotation. @Retention(RUNTIME) tells java compiler that this annotation should be retained at RUNTIME.

Test Cases

Test class contains all the tests. It has three tests but only two of them is marked with @MyTest annotation. Which means our test runner should execute only first two tests. Now lets take a look at our test runner which will run these tests.

TestRunner

This test runner takes an object and runs tests present in that object. Tests are nothing but methods. So to run the tests we would need to get all the methods present in this object. To achieve that we will use reflection.

test.getClass().getDeclaredMethods() returnsĀ all the methods present in test class. Then iterate over each method and check for MyTest annotation. If a methodĀ has @MyTest annotation then it invokes that method. And ultimately it runs your tests. Now lets see how to use this TestRunner.

TestRunner Usage

In this Main class I create the new object of Test and pass it to testRunner. Thats all you need to do and your tests will be executed.

Output

As you can see above, our test runner successfully identified the tests marked with @MyTest annotation and executed them.

Thats all folks, I hope you liked this article. If you have any comments, feedback or suggestions, please leave a comment below and also subscribe to my blog. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *