使用批注创建一个API的多个版本 [英] Using annotations to create multiple versions of an API

查看:137
本文介绍了使用批注创建一个API的多个版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用注解来创建一个API的多个版本?

Is it possible to use annotations to create multiple versions of an API?

我有一个目标API需要实施的,但它已经成长的时候,我想支持多层次 - 如果可能的话在同一个分支。我已经想过分手的API和代理它来实现库,但是仍然需要大量的code,以保持同步。

I've got a target API that needs implementing, but it has grown in time, and I would like to support multiple levels - if possible in the same branch. I've already thought about splitting up the API and proxy it to implementation library, but that still requires a lot of code to keep synchronized.

所以,你会创建code,如:

So you would create code like:

@Since("1.2") <- only created when 1.2 or higher
public interface InNewerAPI {

    @Since("1.3") <- only compiled when 1.3 or higher
    public void methodInEvenNewerAPI();

}

当然,我也可以使用这样的preprocessor,但我想避免pre-处理器的Java code。

Of course I could also use a preprocessor for this, but I would like to avoid pre-processors for Java code.

有什么缺点,以这种方式(如1.8之前失去的参数名称)?

Are there any drawbacks to this approach (such as loosing argument names prior to 1.8)?

请注意,我需要接口和类,包括抽象和内部类的支持。方法和常量需要进行版本为好。

Note that I need support for interfaces and classes, including abstract and inner classes. Methods and constants need to be versioned as well.

推荐答案

OK,我觉得你真的可以使用标注来标记应露出API版本范围。例如,如果方法富()在1.1版本中引入的。在1.5版本pcated德$ P $您可以将其标记为以下内容:

OK, I think that you can really use annotations to mark ranges of versions that should expose the API. For example if method foo() was introduced in version 1.1. and deprecated in version 1.5 you can mark it as following:

类MyApiImpl {
    @Since(1.2)
    @德precateAt(1.4)
    @Until(1.5)
    富();
}

class MyApiImpl { @Since("1.2") @DeprecateAt("1.4") @Until("1.5") foo(); }

然后就可以根据客户端版本自动生成的界面接口 MyApi

Then you can automatically generate interface interface MyApi according to client version:


  1. 客户端之前1.2版本这个方法将不会被纳入接口。

  2. 1.2版本和1.4之间的客户端将被列入

  3. 它会被包括在内,但标记为 @ pcated德$ P $
  4. 客户端版本1.4
  5. 也不会在1.5
  6. 包含客户端
  1. for client prior version 1.2 this method will not be included into the interface.
  2. for client between versions 1.2 and 1.4 it will be included
  3. for client version 1.4 it will be included but marked as @Deprecated
  4. it will not be included for clients after 1.5

这篇关于使用批注创建一个API的多个版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆