I’m writing a program which talks to a SOAP API. The program I’m writing deals with money, and it would be useful to be able to determine at run time (my reasoning being that if I can deal with it at run time I can easily put together configuration files for use post-compilation) whether or not I want the methods which deal with money to be executed.
I have a make request method which all API calls go through, ideally I would like something like so:
...
@DealsWithMoney
public void myMethodWhichMakesAnAPIRequest() {
...
this.makeRequest(...);
}
public void makeRequest(...) {
if (!this.allowMoneyHandlingMethods) {
//Psuedo code
if (method_that_called_this_method has @DealsWithMoney annotation) {
//ignore
}
}
}
How can I write this custom annotation? Or is there a better method for handling this?
will define the annotation for you.
gets the stack track from which you should be able to get the
MethodorMemberreference of the caller and doand check to see if the annotation you are looking for is in the returned list.