0

I am trying to search a specific string in the source code so that I can identify the string is found in which method.I tried file reader. But failed . How to identify a method and it's scope while reading a source code with a file reader? Is there any other way to get the method and it's scope / method and it's declaration?

I took another approach where I could manage to get name of methods of the file using classloader. But again failed to get the method declaration.

Any help will be appreciated. TIA.

Rubayat Jinnah
  • 414
  • 4
  • 20
  • Is your goal to have the method declaration? i.e. name, parameters, return type? – SHG Jun 03 '17 at 07:59
  • No. My goal is to read the method line by line and search a specific keyword. It is a easy process if I use file reader. But the problem is there is no way to even identify which is a method, which is a variable etc. using file reader. – Rubayat Jinnah Jun 03 '17 at 08:03
  • Can you describe what you're trying to achieve as a function? What's your input and what's your output? – SHG Jun 03 '17 at 08:05
  • There is a source code I have. I want to search a keyword in the class file. I am trying to find in which method the keyword is found. So, what I want to achieve is the name of the method where the keyword is found. – Rubayat Jinnah Jun 03 '17 at 08:10
  • 1
    The only correct and reliable way is by having a Java source code parser. All other solutions will fail with some inputs. – Holger Jun 06 '17 at 15:59

3 Answers3

1

UPDATED VERSION

Sorry for posting a second answer, but I just want to ascertain that you get notified of this answer.

This is the code for the file:

File file = new File("FilePath");
FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);
String t = "";

while((t = br.readLine()) != null)
{
    str = str + t + "\n";
}

This is the regex:

Pattern p = Pattern.compile(".*\\s+(.+\\(.*?\\))\\s*\\{(.*"+keyword+"[^\\w*].*)\\}.*\\}$", Pattern.DOTALL);
Matcher m = p.matcher(str);
boolean notFound = true;
while(m.find())
{
    System.out.println("Scope of keyword: " + m.group(1));
    notFound = false;
}

if(notFound)
{
    System.out.println("NOT FOUND");
}

Since you wanted the scope of the keyword, it is the nearest brace brackets within which it is declared, so I have assumed you have written its data type and the variable name in the keyword. I'll see if I can handle more exceptions later :)

.*\\s+ - Takes in everything until the method name

(.+\\(.*?\\))\\s*\\{ - Stores the method name and parameters (In case there are overloaded methods)

(.*"+keyword+" - Finds if the keyword is present after some other code

[^\\w*] Ensures that the keyword exists by itself (Otherwise if the keyword is double x, and double xy is present, then it would be a match)

.*)\\}.*\\}$ - Sees if there are two Braces after the keyword is found (one to end the method or block [in case of if, else if, switch etc.], one to end the class)

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
0

EDIT: I have posted my Source Code in another answer. These are just some technical errors that can go unchecked. But it's not really important now.

The problem with identifying the method the keyword is in is:

1) It might be in a constructor,

2) It might be a member variable,

3) It might be in a loop or a condition,

4) Or it might even be in an Overriden method of the initially executed method.

The easiest way is to print the location (Like an address). Example:

Location: if(a!=0), if(a < 500), main(String[] args), class FUBAR

You can use a recursive method with a regex which identifies everything within braces, and searches for the keyword. I can post the entire code in 4-5 hours, if that's fine.

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
0

While parsing your source file, first you need to know to identify when you reach a method definition (using some pattern/regex). Then, once you're inside some method you should save its name as current method.

Now you continue parsing your keyword. If you found your keyword ==> done, and you know what's the method it was found in.

You would want to know when you're exiting current method. Use a stack data structure in order to know when you leave this method, according to the curly brackets order (Read about the "balanced brackets problem" and about stack if you're not familiar with them. Example here):

  • { - means you push it to the stack.
  • } - means you pop a { from the stack (if it's not empty). If you parse } and it's empty - you did something wrong.

Every time the stack is empty (don't push the class' curly brackets), it means that you're between methods, outside. In every method you reach you repeat the same algorithm.

Few things to note:

  • This keyword might appear maybe more than one, maybe as part of a string.
  • Don't forget that the class itself has curly brackets.
  • Your source code might be "weirder" than the "classic" Java class - sub classes, inner classes, etc. Think about all the different language elements you might have.
  • Curly brackets might also appear as part of strings. You should know how to avoid these.

Good luck!

SHG
  • 2,516
  • 1
  • 14
  • 20