we are using an annotation to mark in our code methods and classes that will be removed in the near future. The annotation is very similar to @deprecated, the only difference is that our annotation has a deletion date, when the code will be removed. I thought that it would be easy to write a custom sonar rule similar to CallToDeprecatedMethodCheck, that would mark all invokings of deprecated methods which will be removed in the next month. But I utterly failed. In other words, in the base.jar there is a class
public class DeprecatedMethod {
@CodeWillBeRemoved(... deletionDate = "2022-06-01")
@Deprecated
public void doIt() {
}
}
And a second Jar containing the class which will be analysed:
public class TestClass {
public void callDeprecated() {
DeprecatedMethod my = new DeprecatedMethod();
my.doIt();
}
}
What I would like to achieve is that in TestClass my.doIt(); will be flagged as a blocker if analysed after 2022-05-01 (because then the deletion date is only one month away).
In my custom rule, when examining the call doIt, I can get the fully qualified name of the class on which the method is called, but I’m neither able to get any annotations on the method doIt() nor could I load the class DeprecatedMethod to check for annotations via reflection. Is it completely futile what I’m trying to achieve or am I missing something. Any hints are greatly welcome.
Hello @schwarfl and welcome to the SonarSource community.
At first, glance, what you are trying to achieve should be possible.
You can access annotations from the metadata of a symbol. Here is what it could look like:
Thank you very much for your response. I tried something like the code you suggested, but you are right, since my JUnit test with the JavaCheckVerifier failed, I didn’t try it in sonarqube. But now I tried it on sonarqube and there it worked! So obviously my test environment is wrong.
Thank you so much for your help!