I think I need a program to extract java public methods at work, so I wrote it Try to implement using Java7's NewIO and Java8's Stream functions
JavaFileAnalyser.java
package triple;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class JavaFileAnalyser {
private static Pattern PATTERN ;
public static void main(String args[]) throws Exception{
PATTERN = Pattern.compile(".*public(.*)(?:\\s|\\t)(.*)\\((.*)\\).*");
List<String> list = Files.newBufferedReader(
Paths.get("C:/Java/jdk1.8.0_102/src/javax/script/AbstractScriptEngine.java"),
Charset.forName("UTF-8"))
.lines()
.filter(JavaFileAnalyser::isMethod)
.map(JavaFileAnalyser::parse)
.collect(Collectors.toList());
Files.write(Paths.get("C:/Users/****/workspace/FileOpe/result/result.txt"),
list,
Charset.forName("UTF8"),
StandardOpenOption.CREATE);
}
public static boolean isMethod(String str){
if(PATTERN.matcher(str).find()){
return true;
}
return false;
}
public static String parse(String str){
Matcher m = PATTERN.matcher(str);
if(m.find()){
return String.format("Return signature: %s method name:%s argument:%s",m.group(1),m.group(2),m.group(3));
}
return "";
}
}
Return signature:Method name:AbstractScriptEngine argument:
Return signature:AbstractScriptEngine method name:argument:Bindings n
Return signature:void method name:setContext argument:ScriptContext ctxt
Return signature:ScriptContext method name:getContext argument:
Return signature:Bindings method name:getBindings argument:int scope
Return signature:void method name:setBindings argument:Bindings bindings, int scope
Return signature:void method name:put argument:String key, Object value
Return signature:Object method name:get argument:String key
Return signature:Object method name:eval argument:Reader reader, Bindings bindings
Return signature:Object method name:eval argument:String script, Bindings bindings
Return signature:Object method name:eval argument:Reader reader
Return signature:Object method name:eval argument:String script
It seems that the constructor information has also been acquired, but I will not care about the details (hey)
Recommended Posts