2

I have a List of classes which I can iterate through. Using Java is there a way of finding out where these classes are used so that I can write it out to a report?

I know that I can find out using 'References' in Eclipse but there are too many to be able to do this manually. So I need to be able to do this programmatically. Can anyone give me any pointers please?

Edit: This is static analysis and part of creating a bigger traceability report for non-technical people. I have comprehensive Javadocs but they are not 'friendly' and also work in the opposite direction to how I need the report. Javadocs start from package and work downwards, whereas I need to start a variable level and work upwards. If that makes any sense.

Gazen Ganados
  • 665
  • 1
  • 7
  • 17

4 Answers4

0

You could try to add a stacktrace dump somewhere in the class that isolates the specific case you are looking for.

public void someMethodInMyClass()
{
    if (conditions_are_met_to_identify)
    {
        Thread.dumpStack();
    }
    // ... original code here
}
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

You may have to scan all the sources, and check the import statements. (Taking care of the * imports.. having to setup your scanner for both the fully Qualified class name and its packagename.*)

EDIT: It would be great to use the eclipse search engine for this. Perhaps here is the answer

Community
  • 1
  • 1
DNax
  • 1,413
  • 1
  • 19
  • 29
0

Still another approach (probably not complete):

Search Google for 'java recursively list directories and files' and get source code that will recursively list all the *.java file path/names in a project. For each file in the list:

1: See if the file path/name is in the list of fully qualified file names you are interested in. If so, record is path/name as a match.

2: Regardless if its a match or not, open the file and copy its content to a List collection. Iterate through the content list and see if the class name is present. If found, determine its path by seeing if its in the same package as the current file you are examining. If so, you have a match. If not, you need to extract the paths from the *.import statements, add it to the class name, and see if it exists in your recursive list of file path/names. If still not found, add it to a 'not found' list (including what line number it was found on) so you can manually see why it was not identified.

3: Add all matches to a 'found match' list. Examine the list to ensure it looks correct.

user2810910
  • 279
  • 1
  • 2
0

Not sure what you are trying to do, but in case you want to analyse code during runtime, I would use an out-of-the box profiler that shows you what is loaded and what allocated.

@Open source profilers: Open Source Java Profilers

On the other hand, if you want to do this yourself (During runtime) you can write your own custom profiler:

How to write a profiler?

You might also find this one useful (Although not exactly what you want):

How can I list all classes loaded in a specific class loader http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html

If what you are looking is just to examine your code base, there are really good tools out there as well. @see http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis

Community
  • 1
  • 1
Ioannis Deligiannis
  • 2,679
  • 5
  • 25
  • 48