Java:“尚未实现”的通用注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9984335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:08:48  来源:igfitidea点击:

Java: Common Annotation for "Not Yet Implemented"

javaannotations

提问by rampion

Is there a common or standard annotation in Java for methods that, while defined, have yet to be implemented?

Java 中是否有针对已定义但尚未实现的方法的通用或标准注释?

So that, for instance, if I were using a pre-alpha version of a library that contained something like

因此,例如,如果我使用包含类似内容的库的 pre-alpha 版本

@NotImplementedYet
public void awesomeMethodThatTotallyDoesExactlyWhatYouNeed(){ /* TODO */ }

I'd get a compile-time warning when trying to call awesomeMethodThatTotallyDoesExactlyWhatYouNeed?

尝试调用时我会收到编译时警告awesomeMethodThatTotallyDoesExactlyWhatYouNeed

回答by Alexander

You might want to use UnsupportedOperationExceptionand detect calls to-yet-to-be-implemented methods when running your tests.

您可能希望在运行测试时使用UnsupportedOperationException并检测对尚未实现的方法的调用。

回答by mhradek

You could create your own annotation. With the runtime retention policy you can then configure target builds to know to look for this annotation, if necessary.

您可以创建自己的注释。使用运行时保留策略,您可以配置目标构建,以便在必要时查找此注释。

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({
    ElementType.ANNOTATION_TYPE, 
    ElementType.METHOD, 
    ElementType.CONSTRUCTOR,
    ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Unimplemented {

    boolean value() default true;
}

回答by Jens Bannmann

No, there is no standard annotation specifically for methods that have yet to be implemented.

不,没有专门针对尚未实现的方法的标准注释。

However, there is a more general annotation in the JDK that marks an API which developers are discouraged from using, and the Java compiler itself can issue warnings when it is used. I am talking about @Deprecated, which many developers only think of as "announcing removal". However, the relevant articles in the JDK docs (e.g. for Java 7and Java 9) list several example use cases, only one of them being about removal:

但是,JDK 中有一个更通用的注释,用于标记开发人员不鼓励使用的 API,并且 Java 编译器本身可以在使用时发出警告。我说的是@Deprecated,许多开发人员只将其视为“宣布删除”。但是,JDK 文档中的相关文章(例如Java 7Java 9)列出了几个示例用例,其中只有一个是关于删除的:

  • The API is dangerous (for example, the Thread.stopmethod).

  • There is a simple rename (for example, AWT Component.show/hidereplaced by setVisible).

  • A newer, better API can be used instead.

  • The deprecated API is going to be removed.

  • API 是危险的(例如,Thread.stop方法)。

  • 有一个简单的重命名(例如,将 AWTComponent.show/hide替换为setVisible)。

  • 可以改用更新、更好的 API。

  • 已弃用的 API 将被删除。

I think your case "not implemented yet" certainly goes in the same direction as those. Further, if the method would always throw a NotYetImplementedException, it even fits the example "The API is dangerous".

我认为您的案例“尚未实施”肯定与那些相同。此外,如果该方法总是抛出NotYetImplementedException,它甚至适合“API 是危险的”示例。

So all you need to do is the following:

所以你需要做的就是以下几点:

  1. Add @Deprecatedto the method
  2. Add @deprecatedJavadoc to explain the background
  3. Configure your build to invoke the compiler with -Xlint:deprecationso that it issues warnings.
  1. 添加@Deprecated到方法中
  2. 添加@deprecatedJavadoc解释背景
  3. 配置您的构建以调用编译器,-Xlint:deprecation以便它发出警告。

回答by Andrejs

Google guavalibraries use the @Betaannotations for API that is likely to change but the methods are implemented though

Google guava库对 API使用@Beta注释,该注释可能会更改,但方法已实现