What is the clearest way to deprecate a package in

2019-01-23 22:54发布

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?

5条回答
戒情不戒烟
2楼-- · 2019-01-23 23:33

You said it yourself: You want to deprecate everything that's inside a 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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-23 23:33

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

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

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

/**
 * @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.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-23 23:45

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

public aspect DeprecationAspect{

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

}

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

查看更多
贪生不怕死
5楼-- · 2019-01-23 23:45

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

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}

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-23 23:46

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

查看更多
登录 后发表回答