简体   繁体   中英

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).

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.

I think this does what you want, but ignores @Deprecated. I seem to remember it adds an option in the project's contextual menu to find unused methods.

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

IntelliJ identifies them as I write them. 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...

so basically you need to look into 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 ...

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:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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