Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Getting Class name from Method Name in a Java Project

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting Class name from Method Name in a Java Project

    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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java Class : How do I set up a toString method for arrays?
    By red7 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 11th, 2012, 05:47 PM
  2. Urgent Class Project Help !!!!!!!!!!!!!
    By thesoulpatchofBruce in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 10th, 2012, 05:14 AM
  3. Please help! Need help with class project!
    By eldeeb_hasan in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 10th, 2012, 12:30 AM
  4. JDBC method in java class
    By nrao in forum JDBC & Databases
    Replies: 1
    Last Post: November 20th, 2010, 03:37 PM
  5. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM

Tags for this Thread