• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get the list of class names accessed  RSS feed

 
DharmendraKumar Sharma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there way to get the list of class names(with package name) and method names invoked for a given class(Source).
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not aware of any way to get the list of class names by giving the package names. If you can get the list of "fully qualified class names" in a jvm, then you can filter by the package name.

In a previous project we accomplished the above by creating a map betwen package name and class names.

If you can get a class you can get the list of Methods using reflection api.
 
DharmendraKumar Sharma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Class(X.java). I need to get all the classes accessed from X.java with package name and also the method invoked. Hope now it wud be clear.
 
Paul Clapham
Sheriff
Posts: 22708
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have the compiled version of the class (X.class) then I believe the "javap" command displays something that contains that information.
 
DharmendraKumar Sharma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a <b>Java file</b>(X.java). I need to get all the classes accessed from X.java with package name and also the method invoked.

For ex I have a Java file (X.java). It invokes couple of DAOs and Utility classes. I just need to write a tool to fetch all the class names accessed from the java source file(X.java).

X.java invokes
abc() method of Xyz.java (Package - pkg.sample)
cde() method of Jke.java (Package - pkg.sample)

I need to write a tool which fetches the list of classes(Xyz & Jke) invoked with Package name(pkg.sample) and method name (abc() & cde()).
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by DharmendraKumar Sharma:
Is there way to get the list of class names(with package name) and method names invoked for a given class(Source).


There must be a way to get such a list, because the compiler needs to do such things.
You might look at tools.jar to find out, how they do it.
You could parse all import statements for imported packages + java.lang (because of "Foobar".replaceAll ("o", "i"); ).
Parse your file for all Class-invokations and methodcalls.
Study the JLS to find out, how nameconflicts are avoided.
...
Another approach would be to look at Open-Source-Software, how it is managed there (eclipse for example).
...
Compiling the class and using reflection would be the fastest solution I guess. Dynamical compiling is again possible with tools.jar, but you have to take care about the special license for that jar (it isn't part of the jre).
[ December 16, 2005: Message edited by: Stefan Wagner ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This information is in the class file in a well-documented and readable format. Surely easier than keeping track of imports and class references in raw source. This will only help you find "static" class references, and won't spot any class.forName( name ).newInstance() style creation.

We use a tool with an interesting approach ... they insert a custom class loader immediately after the boot loader and keep track of all classes loaded. It works as long as all other custom class loaders delegate to their parents correctly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!