Results 1 to 4 of 4
  1. #1
    Marion is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default How to check a method usage in class source code

    Hi everybody I'm new to the forum and I need help - suggestions, how could be checked if a concrete method is used in class source code. For example, I could want to know, if the method System.out.println() is used in following class:

    Java Code:
    public class Factorial {
    
       public static int calculateFactorial (int number){
          if(number == 1) {
             return number;
          }
          System.out.println("Recursive call");
          return number*calculate(--number);
       }
    
    }
    I can't use the contains(...), matches(...) and generally no pattern/string serching method, because this would give me "true" even if the method would be part of a comment.
    I know, that if I use the JavaCompiler class to compile code, i could get trough URLClassLoader to the Method object of calculateFactorial(int number), but there is no method to analyze it's code in so way..

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: How to check a method usage in class source code

    1)Make two passes on the source.
    Remove the comments on the first pass.
    On the second you could use indexOf.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    rfq
    rfq is offline Member
    Join Date
    Mar 2012
    Location
    Novosibirsk, Russia
    Posts
    15
    Rep Power
    0

    Default Re: How to check a method usage in class source code

    Take some bytecode library (e.g. ASM - Home Page) and scan the constant pool to see if the method name is present. No need to scan bytecodes. This way is preferred over text browsing because in the constant pool, method names are accompanied with corresponding class names.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,006
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to check a method usage in class source code

    Quote Originally Posted by Norm View Post
    1)Make two passes on the source.
    Remove the comments on the first pass.
    On the second you could use indexOf.
    You still have to be careful with String literals then ... I'd prefer the Javac API and let the compiler to all the nitty gritty work.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  2. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  3. read source code from a class
    By argus in forum New To Java
    Replies: 6
    Last Post: 10-14-2010, 07:05 PM
  4. Replies: 1
    Last Post: 11-28-2008, 06:27 PM
  5. Organize class source code
    By Alejandro Valdez in forum Eclipse
    Replies: 0
    Last Post: 05-16-2008, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •