简体   繁体   English

列出不再在任何地方使用的所有已弃用类/方法的工具?

[英]Tool that lists all deprecated classes/methods that are no longer used anywhere?

I need to cleanup a some legacy code.我需要清理一些遗留代码。 Removing unused code is an important step.删除未使用的代码是重要的一步。

Is there a tool that finds all deprecated code, removes all items which are still used somewhere and gives me a list of unused deprecated code?是否有一种工具可以找到所有已弃用的代码,删除仍在某处使用的所有项目,并为我提供未使用的已弃用代码列表?

For bonus points: Is there a tool which can find unused code non-deprecated code?对于加分:是否有工具可以找到未使用的代码、未弃用的代码?

I'm aware that this is never perfect but I know for which cases I need special handling (as in DB drivers or classes that are referenced via DI).我知道这从来都不是完美的,但我知道在哪些情况下我需要特殊处理(如在通过 DI 引用的 DB 驱动程序或类中)。

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.任何 IDE 都会帮助你解决这个问题。 Not automatically but removing an @Deprecated annotation is easily done with a global query-and-replace.不会自动删除@Deprecated 注释,但可以通过全局查询和替换轻松完成。 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.如果您只想删除未使用的代码,我在之前的项目中为此使用了 eclipse 插件ucdetector 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.如果您有幸拥有出色的测试覆盖率,另一种选择是使用代码覆盖率分析工具,例如 cobertura、clover 或 emma。

I think this does what you want, but ignores @Deprecated.我认为这可以满足您的要求,但忽略了@Deprecated。 I seem to remember it adds an option in the project's contextual menu to find unused methods.我似乎记得它在项目的上下文菜单中添加了一个选项来查找未使用的方法。

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

IntelliJ identifies them as I write them. IntelliJ 在我编写它们时识别它们。 I'm not sure if there's an option to remove them automatically.我不确定是否有自动删除它们的选项。

not sure your Q is a bit hard to grasp ... StackOverflow for me it is mostly about Code problem so I assume you want a way to get all Methods with the @Deprecated Annotation...不确定你的 Q 有点难以掌握...... StackOverflow 对我来说它主要是关于代码问题所以我假设你想要一种方法来获取所有带有 @Deprecated 注释的方法......

so basically you need to look into Java Reflection ..所以基本上你需要研究 Java Reflection ..

So, for Example, let's say you want all the Deprecated Methods in the Date Class (Java.util.Date) this is what you can do ...因此,例如,假设您想要日期类(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
            }
        }

    }

Using the Spoon library for transforming java source code:使用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();

See method removeDeprecatedMethods .请参阅方法removeDeprecatedMethods

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM