• Post Reply Bookmark Topic Watch Topic
  • New Topic

Way to get classes used within a class?  RSS feed

 
Author
Ranch Hand
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forum:
I was wondering if it is possible to use Reflection to get a list of all the classes used within a class, i.e. all the classes an import statement needs to be added for. I've tried Class.getClasses() and it's other varients but without success.
What I'm really looking is to be able to run something on a class and have it give me a list that says, like, the following classes are use: {String, Double, ArrayList, Collection, MyCategory, MyGroup, etc.} My idea is that if I can get this list, I can use it to build a database of dependances. So if I want to edit the MyCategory class, I can get from the database all of my classes that use it.
Thanks,
Michael.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you cannot do this directly, but with some work, you can probably assemble the list yourself. As you probably know, the reflection mechanism has methods to obtain member data fields and member methods. You can use these to obtain the types used for the data fields, method parameters and method return types.
The only other problem is local variables, as far as I can come up with. I'm not sure how you can get the types for these.
Layne
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
I've been working in this area lately, and as was pointed out, the reflection members can get the class fields, methods and constructors, their parameters and return classes. But, I have not been able to find the "temp" fields declared inside the body of the methods.
I wrote a parser to find the classes that were
imported or instantiated by a source file. The instantiated items were found by looking for "new <whatever>" constructs.
Dave Patterson
patterd1@comcast.net
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
just a note on this.
as u r working on this how about detecting classes loaded by Class.forName()?? do we need to know such classes in ur type of app?
well, thats not gonna be so hard i guess as Class.forName() has direct string presenting the fully qualified classname...
thanks
maulin.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another situation occured to me: inner classes. Do you use these often? Maybe reflection provides methods to obtain, these, too, but I'm not sure. Even then, you will have a problem with anonymous inner classes and their base classes.
Also, are you interested in EVERY class that is used, or only classes that are not from the Java API?
 
Michael Remijan
Author
Ranch Hand
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really only interested in the classes that I I've produced. I'm not worried about inner classes since I never use an inner class outside the class it's created in.
The responses so far about using Reflection have been good, but I've already investigated them. They didn't really get me what I needed.
I do have a very brute-force idea about how to do this. Basically, recurse through all the files in a directory structure. That'll give me a list of all my class names. Then grep through each file searching for each class name. This, though, has it's own problems like comments and classes that are named the same, i.e. Main.java that holds main(String []) for an application.
Originally posted by Layne Lund:
Another situation occured to me: inner classes. Do you use these often? Maybe reflection provides methods to obtain, these, too, but I'm not sure. Even then, you will have a problem with anonymous inner classes and their base classes.
Also, are you interested in EVERY class that is used, or only classes that are not from the Java API?
 
David Patterson
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add a bit more rain to your parade, one difficulty in doing the grep approach is that you need to worry about comments (single line "//" as well as multi-line ( "/*" and "*/") as well as quoted strings. Things in the comments and quoted strings need to be avoided.
I'm doing a similar thing. I've got a cluster of directories for packages. One source directory corresponds to a package. I've got program to run on a single directory to produce a "report" on the files in the package.
Dave Patterson
 
David Patterson
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not figured out how to get the reflection classes to show the inner classes. One way they can show up is when an inner class references an entry that is "private" from the outer class. The compiler generates extra methods to access the data. These methods show up with names like "Access$1". I asked a question in this forum on January 7, 2003 and one of the responses described what was going on.
Dave Patterson
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!