Is there any way to make it mandatory to use some specific annotation along with some particular method in Java

Viewed 54

A class implementing interface ensures that all the methods in the interface are defined in the class, likewise, is there any way to tell the JVM that some specific annotations should be used in implementing class. Consider the following interface

public interface TestClass{
    @BeforeClass
    public void setup();
    @AfterClass
    public void tearDown();
}

and following is the implementing class

public class TestScripts implements TestClass{
    @BeforeClass
    public void setup(){
        /*method body*/
    }
    @AfterClass
    public void tearDown(){
       /*method body*/
   }
}

All I want is that the compiler should show error if @BeforeClass annotation is not used with the method public void setup() and @AfterClass annotation is not used with the method public void tearDown(). Any way to achieve this?

1 Answers
Related