在 Java 中弃用包的最清晰方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5074454/
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 09:17:43  来源:igfitidea点击:

What is the clearest way to deprecate a package in Java?

javapackagedeprecated

提问by Daniel Alexiuc

I'm working on a new codebase and migrating the system to a new framework.

我正在开发一个新的代码库并将系统迁移到一个新的框架。

There are a number of packages that I would like to deprecate, just to make it very clear to other developers that everything inside this package should not be used anymore for new development.

有许多包我想弃用,只是为了让其他开发人员非常清楚,这个包中的所有东西都不应再用于新的开发。

What is the best way to indicate that an entire package has been deprecated?

指示整个软件包已被弃用的最佳方法是什么?

采纳答案by Andreas Dolk

You said it yourself: You want to deprecate everything that's insidea package, not the package itself. The package is nothing but a namespace and deprecating a namespace would have a different meaning - like don't use this namespace anymore. Like don't add any new items to that namespace.

你自己说的:要弃用一切的包,而不是包本身。包只不过是一个命名空间,弃用命名空间会有不同的含义——比如不再使用这个命名空间。就像不要向该命名空间添加任何新项目一样。

In your case, I suggest you deprecate each public method (and field) of each class that shouldn't be used anymore. This becomes visible in modern IDE's and developers are warned when they want to use the old classes and methods. And you can look through your code and refactor it step by step to eliminate dependencies of those classes and methods.

在您的情况下,我建议您弃用不应再使用的每个类的每个公共方法(和字段)。这在现代 IDE 中变得可见,并且在开发人员想要使用旧的类和方法时会收到警告。您可以查看您的代码并逐步对其进行重构,以消除这些类和方法的依赖关系。

回答by muymoo

I had to do this recently and used the package-info.javafile to deprecate the package.

我最近不得不这样做并使用该package-info.java文件来弃用该包。

http://www.intertech.com/Blog/whats-package-info-java-for/

http://www.intertech.com/Blog/whats-package-info-java-for/

Add a package-info.javafile to your package with only the package declaration:

package-info.java仅使用包声明将文件添加到包中:

/**
 * @deprecated As of release 2.0, replaced by {@link com.acme.new.package}
 */
@Deprecated
package com.acme.old.package;

In Eclipse, all places where the user imports a class from this package will be underlined with a deprecation warning.

在 Eclipse 中,用户从该包中导入类的所有位置都将带有下划线并带有弃用警告。

回答by Sean Patrick Floyd

Use AspectJ. Create an Aspect that issues a compiler warning when the package is used:

使用AspectJ。创建一个在使用包时发出编译器警告的方面:

public aspect DeprecationAspect{

    declare warning :
        call(* foo.bar..*(..)) : "Package foo.bar is deprecated";

}

(You can easily add AspectJ to your build if you use antor maven)

(如果您使用antmaven ,您可以轻松地将 AspectJ 添加到您的构建中)

回答by Pavel Vyazankin

For those who came later...
My solution with IDEA "Replace in Path"

对于那些后来的人......
我的IDEA“替换路径”解决方案

Text to find:(public|protected)+(\s)(abstract)(\s)(static)(\s)(final)(\s)*(class|interface|enum|Enum)
Replace with:@Deprecated\n\$1 \$3 \$5 \$7 \$9
Options:(select) Regular expressions
Directory:{Choose dir}

要查找的文本:(public|protected)+(\s) (abstract)(\s) (static)(\s) (final)(\s)*(class|interface|enum|Enum)
替换为:@Deprecated \n\$1 \$3 \$5 \$7 \$9
选项:(选择)正则表达式
目录:{选择目录}

回答by Sergey Vedernikov

You can create Package annotationand deprecate any package with @Deprecated:)

您可以创建包注释并使用@Deprecated:)弃用任何包

回答by Stefan Haberl

As mentioned by @muymoo before, you could use a package-info.javafile. But all you can do is add a @deprecatedwarning in the JavaDocof your file:

正如@muymoo 之前提到的,您可以使用package-info.java文件。但您所能做的就是@deprecated在文件的JavaDoc中添加警告:

/**
 * @deprecated This package is deprecated, use {@link my.new.pkg} instead.
 */
package my.old.pkg;

JavaDoc is really your only option here, you cannot@Deprecatethe package using a "proper" in-code annotation

JavaDoc 真的是你唯一的选择,你不能@Deprecate使用“正确的”代码注释来打包

@Deprecated
package my.old.pkg;

will lead to a compilation error in Java 8

将导致 Java 8 中的编译错误

$ java -version
java version "1.8.0_191"

$ javac package-info.java
package-info.java:6: error: modifier deprecated not allowed here

The only real, clean option here is to really deprecate all the classes in your package. Which does make sense, if you think about it, because a package in Java is nothing more then a namespace.

这里唯一真正、干净的选择是真正弃用包中的所有类。如果你仔细想想,这确实是有道理的,因为 Java 中的包只不过是一个命名空间。