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
Java: Common Annotation for "Not Yet Implemented"
提问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 7和Java 9)列出了几个示例用例,其中只有一个是关于删除的:
The API is dangerous (for example, the
Thread.stop
method).There is a simple rename (for example, AWT
Component.show/hide
replaced bysetVisible
).A newer, better API can be used instead.
The deprecated API is going to be removed.
API 是危险的(例如,
Thread.stop
方法)。有一个简单的重命名(例如,将 AWT
Component.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:
所以你需要做的就是以下几点:
- Add
@Deprecated
to the method - Add
@deprecated
Javadoc to explain the background - Configure your build to invoke the compiler with
-Xlint:deprecation
so that it issues warnings.
- 添加
@Deprecated
到方法中 - 添加
@deprecated
Javadoc解释背景 - 配置您的构建以调用编译器,
-Xlint:deprecation
以便它发出警告。