如何在Java中使用注释排除方法可见性?

时间:2014-07-28 19:18:49

标签: java annotations dsl

例如,我的简单DSL有两种不同的API。

旧API:

client().userService().organization(«IBM»).user(«userId»).get();

新API:

client().newAPI().userService().organization().user(user).get();

我想在使用触发器newAPI()时从旧API中排除所有方法。标有注释的新API的方法,例如@NewAPI。

是否可以做这样的事情?

PS

是的,我知道在编译时很难做到这一点。但是谁知道它在Java中是可能的。

1 个答案:

答案 0 :(得分:0)

如果返回类型只是完整接口的子接口,则newAPI()方法可以返回方法的子集。

interface NewAPI {
  NewAPI organization();
  NewAPI user(User user);
  Result get();
}
interface ClientAPI extends NewAPI {
  @Deprecated
  ClientAPI organization(String s);
  @Deprecated
  ClientAPI user(String id);
  NewAPI newAPI();
}

方法client()将返回ClientAPI的实例,一旦调用newAPI(),弃用的方法就不再可用。