Java 函数注解帮助,使用@Deprecated? [英] Java Function Annotation Help, use @Deprecated?

查看:22
本文介绍了Java 函数注解帮助,使用@Deprecated?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:
Java 1.6

Scenario:
Java 1.6

class Animal { 
    private String name; 
    ...  
    public String getName() { return name; }  
    ...
}

class CatDog extends Animal {
    private String dogName;
    private String catName;
    ...
    public String getDogName() { return dogName; }
    public String getCatName() { return catName; }
    public String[] getNames() { return new String[]{ catName, dogName }; }
    ...
    public String getName() { return "ERROR! DO NOT USE ME"; }
}

问题:
getName 没有意义,不应在此示例中使用.我正在阅读@Deprecated 注释.有没有更合适的注解方法?

Problem:
getName doesn't make sense and shouldn't be used in this example. I'm reading about @Deprecated annotation. Is there a more appropriate annotation method?

问题:
A) 使用此函数时(运行时之前)是否可以强制出错?
B) 有没有办法为我将使用的注释方法显示自定义警告/错误消息?理想情况下,当用户将鼠标悬停在已弃用/错误函数上时.

Questions:
A) Is it possible to force an error when this function is used (before runtime)?
B) Is there a way to display a customized warning/error message for the annotation method I will use? Ideally when the user is hovering over deprecated/error function.

推荐答案

通常,您将 @Deprecated 用于已被较新版本的软件淘汰的方法,但为了 API 与代码的兼容性而保留这些方法这取决于旧版本.我不确定它是否正是在这种情况下使用的最佳标记,因为 getName 仍在被 Animal 的其他子类积极使用,但它肯定会提醒用户 CatDog 类,他们不应该调用该方法.

Generally, you use @Deprecated for methods that have been made obsolete by a newer version of your software, but which you're keeping around for API compatibility with code that depends on the old version. I'm not sure if it's exactly the best tag to use in this scenario, because getName is still being actively used by other subclasses of Animal, but it will certainly alert users of the CatDog class that they shouldn't call that method.

如果您想在使用该函数时在编译时导致错误,您可以更改编译器选项,将@Deprecated 方法的使用视为错误而不是警告.当然,你不能保证每个使用你的库的人都会设置这个选项,我知道没有办法仅仅根据语言规范来强制编译错误.从 CatDog 中删除该方法仍将允许客户端调用它,因为客户端将只是从超类 Animal 调用默认实现(大概你仍然想包括那个方法).

If you want to cause an error at compile time when that function is used, you can change your compiler options to consider use of @Deprecated methods to be an error instead of a warning. Of course, you can't guarantee that everyone who uses your library will set this option, and there's no way I know of to force a compile error just based on the language specification. Removing the method from CatDog will still allow clients to call it, since the client will just be invoking the default implementation from the superclass Animal (which presumably you still want to include that method).

但是,当用户将鼠标悬停在已弃用的方法上时,当然可以显示自定义消息.Javadoc @deprecated 标记允许您指定方法被弃用的原因的解释,当用户将鼠标悬停在 Eclipse 等 IDE 中的方法上时,它会弹出而不是通常的方法描述.它看起来像这样:

It is certainly possible, however, to display a custom message when the user hovers over the deprecated method. The Javadoc @deprecated tag allows you to specify an explanation of why a method was deprecated, and it will pop up instead of the usual description of the method when the user hovers over the method in an IDE like Eclipse. It would look like this:

/**
 * 
 * @deprecated Do not use this method!
 */
 @Deprecated
 public String getName() {
     throw new UnsupportedOperationException();
 }

(请注意,您可以让方法的实现抛出异常,以保证如果用户在编译时没有注意到@Deprecated 标记,他们肯定会在运行时注意到它).

(Note that you can make your implementation of the method throw an exception to guarantee that if the user didn't notice the @Deprecated tag at compile time, they'll definitely notice it at runtime).

这篇关于Java 函数注解帮助,使用@Deprecated?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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