How to check a method usage in class source code
Hi everybody :(hi): 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:
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..
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.
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.
Re: How to check a method usage in class source code
Quote:
Originally Posted by
Norm
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