英文:
Get method as a file from .java file
问题
我有一个位于文件夹中的.java文件。我想获取所有的方法,每个方法都作为单独的文件。
class Foo{
public void add(int a, int b){ System.out.println(a+b);}
private int sub(int a, int b,int c){ return a-b;}}
在我的程序中,我想将所有的方法add和sub分别作为两个单独的文件。我知道对于.class
文件,我们可以使用反射来实现这一点,但对于.java文件是否可能呢?
英文:
I have a .java file in a folder. I want to get all the methods and every method as a separate file.
class Foo{
public void add(int a, int b){ System.out.println(a+b);}
private int sub(int a, int b,int c){ return a-b;}}
In my program, I want to get all the methods add , sub as separate two files. I know this for .class
file.we can achieve it using Reflection, but is it possible for .java file?
答案1
得分: 1
@SanzidaSultana,我提供了从类中提取方法的代码...这使用正则表达式从类文件中提取方法。但是,此实现有局限性。无论如何,它将帮助您解决问题。我只是在下面提供代码(附带示例)。
我提供的解析器非常容易使用。您只需要使用JavaMethodParser
类来提取方法(以及它的名称!)。请参考以下代码:
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;
/**
* 注意:这是一个非常简单的解析器
* 尽可能简单
* 它假定Foo.java正确编写
* 它只能追踪Foo.java中的一些错误
* 如果没有按正确格式编写
* 所以请记住这一点
*
*
*/
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);
// 现在匹配
Matcher classMatcher = classPattern.matcher(classContents);
if(classMatcher.find()) {
String methodContents = classMatcher.group(2);
// 现在解析方法
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<>();
// 创建方法列表和方法名列表
methodList = new ArrayList<>();
methodNameList = new ArrayList<>();
while(methodMatcher.find()) {
String methodName = methodMatcher.group(1);
String methodStart = methodMatcher.group();
methodStartList.add(methodStart);
methodNameList.add(methodName);
}
// 反转,因为从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");
// 抛出错误,因为连一个类都没有
// 或者执行您认为必要的操作
}
// 初始化计数器
cnt = -1;
}
public boolean hasMoreMethods() {
cnt += 1; // 因为计数器初始化为-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();
}
}
}
此程序的输入是Foo.java,其内容如下:
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;
}
}
输出是:
name: sub
definition:
public int sub(int a, int b) {
return a - b;
}
name: add
definition:
public int add(int a, b) {
if(a + b < 0) {
return 0;
}
return a + b;
}
我猜您知道如何使用Java将内容写入文件。所以,我会把那部分留给您...
英文:
@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...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论