Is it possible to determine which obsolete classes are actively used?

I have a relatively complex Java project, with many classes that are probably not used.

Static analysis of the code base is applicable for some classes, but some of them are loaded dynamically (network services, stored data, etc.).

Is there a way to get a list of obsolete classes that were actively used in jvm, so I can find out if these classes are used?

[I know there may be “sleeping classes” that are rarely used, but I can take a chance]

+5
source share
5 answers

The JVM may be able to print information about all the classes used. For instance:

java -verbose:class ... 

You still have to filter out the obsolete ones in other ways.

+1
source

You can try to compile with -deprecation (or -Xlint: deprecation) to see the use of deprecated APIs if you have the source code (I think you have)

+1
source

I could think of the following:

Add a static initializer to each of your classes in the form

static {

// here you can get the announcement:

// either write to the file with each class printing its name

// or force System.err to output the class name, and then retrieve all the output.

}

If you are too lazy to manually add a piece of code, you could write a simple program to add this initializer to all files ending in .java.

This is an easy way to get a list of all the classes used (I think). Good luck

+1
source

There are tools like the UCDetector that I have used in the past. But this requires manual evaluations, which can be painful for large projects. You can perform a text analysis as shown below:

List of Active Deprecation Methods

To analyze static code, list obsolete methods whose source is already available using the IDE.

  • In Eclipse, goto Window -> Settings -> Java -> Compiler -> Errors / Warnings -> Deprecated and restricted API : set the WARNING level and check the elements.
  • In the Problems window, click the Down button next to the tab → Group Mode → Java Problem Type
  • You can see a list of Deprecation grouped together, copy the contents as text, which you can use to further prepare your scripts / excel for analysis.
  • Use a simple text editor to search for and replace " from the type " and " is deprecated " with tabs
  • Copy content to a spreadsheet. You will have a list of classes that contain deprecated methods but have active use.

List of Inactive Usage Methods

  • List the names of classes and methods using the same approach above (for the method name, replace the text " The method " and " from the type " with tabs).
  • This list minus the previous list is a list of inactive methods.

List of obsolete dynamic methods

For dynamic code using reflections, etc. there is no single approach to the pool. You can filter out basic things using method names.

  • List the method names using the same approach above (replace the text " The method " and " from the type " with tabs).
  • For a unique set of method names, run the grep script loop. This type of search can be slow even for small projects. But just in case you want to spend time.
+1
source

Delete them all and try to run the application. Every time you get a ClassNotFoundException for one of them, write it down and add the class back. Rinse and repeat.

0
source

Source: https://habr.com/ru/post/1203596/


All Articles