3

我需要清理一些遗留代码。删除未使用的代码是重要的一步。

是否有一个工具可以找到所有已弃用的代码,删除所有仍在某处使用的项目并给我一个未使用的弃用代码列表?

奖励积分:是否有工具可以找到未使用的代码非弃用代码?

我知道这从来都不是完美的,但我知道在哪些情况下需要特殊处理(如在通过 DI 引用的 DB 驱动程序或类中)。

4

5 回答 5

3

I'm not completely certain that I understand your question. Do you want a tool that un-deprecates code that is still referenced? Any IDE will help you with that. Not automatically but removing an @Deprecated annotation is easily done with a global query-and-replace. After you have removed unused code, of course:

If all you want is to remove unused code, I have used the eclipse plugin ucdetector for this purpose in a previous project. While it does not actually remove the unused code it does give you a list of the methods, classes and constants that have no references so you can remove them yourself. This is a good thing.

As you point out yourself, there are some classes/methods that may seem to be unused using static analysis. In my opinion this makes it impossible to automate this task. You the coder will have to analyze every block of code that is reported to be unused.

If you are lucky enough to have excellent test coverage another option is to use a code coverage analysis tool, like cobertura, clover or emma.

于 2010-11-30T08:54:26.963 回答
0

我认为这可以满足您的要求,但忽略了@Deprecated。我似乎记得它在项目的上下文菜单中添加了一个选项来查找未使用的方法。

http://eclipse-tools.sourceforge.net/

于 2010-11-23T13:10:46.437 回答
0

IntelliJ 在我编写它们时会识别它们。我不确定是否有自动删除它们的选项。

于 2010-11-23T13:11:15.480 回答
0

使用Spoon库转换 java 源代码:

String path = "src/main/java";
Launcher spoon = new Launcher();
spoon.addInputResource(path);
spoon.setSourceOutputDirectory(path);
spoon.addProcessor(new AbstractProcessor<CtMethod>() {
        @Override
        public void process(CtMethod method) {
                if (method.hasAnnotation(Deprecated.class)) {
                        method.delete();
                }
        }
});
spoon.getEnvironment().setPrettyPrinterCreator(() -> {
                        return new SniperJavaPrettyPrinter(spoon.getEnvironment());
                }
);
spoon.run();

方法removeDeprecatedMethods

于 2019-12-09T18:04:22.477 回答
0

不确定你的 Q 有点难以掌握...... StackOverflow 对我来说主要是关于代码问题,所以我假设你想要一种方法来使用 @Deprecated Annotation 获取所有方法......

所以基本上你需要研究 Java Reflection ..

因此,例如,假设您想要 Date 类 (Java.util.Date) 中的所有已弃用方法,这就是您可以做的......

Class<?> clazz = Date.class; //Getting Class Obj of the Date Class

    Method[] methods = clazz.getDeclaredMethods(); //Getting methods


    for (Method m : methods) { //Inhanced For-Loop To get them-all 
        for (Annotation a : m.getAnnotations()) {
            if (a instanceof Deprecated) {
                System.out.println(m.getName()); // gitting the Methods Names
            }
        }

    }
于 2018-05-01T16:31:45.567 回答