动态地忽略事务方法

发布于 2024-10-19 12:21:01 字数 584 浏览 0 评论 0原文

我正在编写一个与 SOAP API 对话的程序。我正在编写的程序涉及金钱,能够在运行时确定会很有用(我的理由是,如果我可以在运行时处理它,我可以轻松地将配置文件放在一起以供编译后使用)我是否想要执行处理金钱的方法。

我有一个所有 API 调用都要经过的 make request 方法,理想情况下我想要这样的东西:

  ...
   @DealsWithMoney
   public void myMethodWhichMakesAnAPIRequest() {
      ...
      this.makeRequest(...); 
   }

   public void makeRequest(...) {
      if (!this.allowMoneyHandlingMethods) {
          //Psuedo code
          if (method_that_called_this_method has @DealsWithMoney annotation) {
              //ignore
          }
      }
   }

如何编写这个自定义注释?或者有更好的方法来处理这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守望孤独 2024-10-26 12:21:01
public @interface DealsWithMoney {}

将为您定义注释。

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

获取堆栈跟踪,从中您应该能够获取调用者的 MethodMember 引用,并

Annotation[] annotations = method.getDeclaredAnnotations();

检查您要查找的注释是否在返回的列表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文