@SanzidaSultana, I've provided the code to extract the methods from a class...This uses regex to extract method from class file. But, this implementation has limitation. Anyway, it'll help you solve your problems. I am just providing the code below(with example).
The parser I've provided, it's very easy to use. All you need is the JavaMethodParser
class. Just use it to extract method(with it's name!). See below:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Note: it is very simple parser
* as simple as possible
* it's assuming Foo.java is correctly written
* it can only track a few errors in Foo.java
* is not written in proper format
* so, keep that in mind
*
*
*/
class JavaMethodParser {
private List<String> methodList;
private List<String> methodNameList;
private int cnt;
public JavaMethodParser(String classContents) {
Pattern classPattern = Pattern.compile("[a-zA-Z]*\\s*class\\s+([_a-zA-Z]+)\\s*\\{(.*)}$", Pattern.DOTALL);
// now match
Matcher classMatcher = classPattern.matcher(classContents);
if(classMatcher.find()) {
String methodContents = classMatcher.group(2);
// now parse the methods
Pattern methodPattern = Pattern.compile("[a-zA-Z]*\\s*[a-zA-Z]+\\s+([_a-zA-Z]+)\\s*?\\(.*?\\)\\s*?\\{", Pattern.DOTALL);
Matcher methodMatcher = methodPattern.matcher(methodContents);
List<String> methodStartList = new ArrayList<>();
// creating method list and methodName list
methodList = new ArrayList<>();
methodNameList = new ArrayList<>();
while(methodMatcher.find()) {
String methodName = methodMatcher.group(1);
String methodStart = methodMatcher.group();
methodStartList.add(methodStart);
methodNameList.add(methodName);
}
// reversing, cause it'll be easier to split
// methods from the end of methodContents
Collections.reverse(methodStartList);
Collections.reverse(methodNameList);
String buf = methodContents;
int i=0;
for(String methodStart: methodStartList) {
String[] t = buf.split(Pattern.quote(methodStart));
String method = methodStart + t[1];
methodList.add(method);
buf = t[0];
i++;
}
} else {
System.out.println("error: class not found");
// throw error, cause not even a class
// or do whatever you think necessary
}
// initializing cnt
cnt = -1;
}
public boolean hasMoreMethods() {
cnt += 1; // cause, cnt is initialized with -1
return cnt < methodList.size();
}
public String getMethodName() {
return methodNameList.get(cnt);
}
public String getMethod() {
return methodList.get(cnt);
}
public int countMethods() {
return methodList.size();
}
}
public class SOTest {
public static void main(String[] args) {
try {
Scanner in = new Scanner(new File("Foo.java"));
String classContents = in.useDelimiter("\\Z").next().trim();
JavaMethodParser jmp = new JavaMethodParser(classContents);
while(jmp.hasMoreMethods()) {
System.out.println("name: " + jmp.getMethodName());
System.out.println("definition:\n" + jmp.getMethod());
System.out.println();
}
in.close();
} catch(FileNotFoundException e) {
e.printStackTrace();
}
}
}
The input to this program is Foo.java which is written as below:
public class Foo {
private int u, v;
private String x;
public int add(int a, int b) {
if(a + b < 0) {
return 0;
}
return a + b;
}
public int sub(int a, int b) {
return a - b;
}
}
And the output is:
name: sub
definition:
public int sub(int a, int b) {
return a - b;
}
name: add
definition:
public int add(int a, int b) {
if(a + b < 0) {
return 0;
}
return a + b;
}
I guess, you know how to write something in file using java. So, I will left that part to you...
[P.S.]: If anything is unclear to you, let me know in the comment section...also provide feedback if its working or not for you...