开发者

Dynamically make transactional methods ignored

开发者 https://www.devze.com 2023-02-14 03:47 出处:网络
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 a

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() {
      ...
     开发者_如何学Go 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?


public @interface DealsWithMoney {}

will define the annotation for you.

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace()

gets the stack track from which you should be able to get the Method or Member reference of the caller and do

Annotation[] annotations = method.getDeclaredAnnotations();

and check to see if the annotation you are looking for is in the returned list.

0

精彩评论

暂无评论...
验证码 换一张
取 消