Getting Class name from Method Name in a Java Project
Hello All,
I am new to this forum. I am trying to get Class name in which certain method name belong in entire Java Project. Method Name will be passed as a parameter and it should return corresponding Class name in which the passed method name resides. This could be across packages.
Is there a way to achieve this in Java? Can it be done using Reflection API? If yes, then please help
Thanks,
Parikshit
Re: Getting Class name from Method Name in a Java Project
I'm not sure how you would get a list of all the classes in your project (I don't think the JVM even sees "Projects", just Classes, Libraries, and Packages).
However, if you could get a list of all of the classes in your project as Class Objects, you could loop through the Class objects, call the getDeclaredMethods() method on them to return an array of Method Objects, then loop through the method array and check each method name with the getName() method.
Re: Getting Class name from Method Name in a Java Project
Yeah... Even i tried few ways to get list of Classes in across all packages in a Project. But unfortunately couldn't succeed. So could you please help me to get all Classes available at run-time so that I can proceed in the way what you have suggested above.
Re: Getting Class name from Method Name in a Java Project
Read through this article to get an Instrumentation object: Instrumentation: querying the memory usage of a Java object
One you have the Instrumentation Object, you can call the getAllLoadedClasses() method which should, theoretically, get you a list of all of the loaded classes in the JVM.
Might be worth a try.
Re: Getting Class name from Method Name in a Java Project
Quote:
get all Classes available
Would there be classes not yet loaded?
Where are the class files for the classes?
If in a jar file, you could get names of all the class files in the jar file and use that list to search each class file.
If in folders on the classpath, you could use the File class to get a list of all the class files to be searched.