我想读取传递给java类的特定方法的参数。这是java文件:
package config;
public class ABC extends LineAvailabilityNew
{
public ABC(Message msg)
throws MessageHandlerExcection
{
super(msg);
}
public void doMessageDataOverrides()
throws MessageHandlerExcection
{
super.doMessageDataOverrides();
setWorkingLineQty("21");
setStoppedLineQty("10");
setworkingLineExchangeGroupCode("GOL");
setWorkingLineWithServiceID("01014013024", "24", "MPF", "N", "N", "A line in e2e", "", "NTE", "GOL", "", "Standard", "");
setOneMoreWorkingLineWithServiceID("01014013025", "25", "MPF", "N", "N", "A line in e2e", "", "NTE", "GOL", "", "Standard", "");
setOneMoreWorkingLineWithServiceID("01014013026", "26", "MPF", "N", "N", "A line in e2e", "", "NTE", "GOL", "", "Standard", "");
setOneMoreStoppedLineWithServiceID("01014013015", "15", "PSTN Single Line", "N", "N", "", "NTE", "GOL", "", "Standard", "");
}
}
这里我想要提供给setWorkingLineWithServiceID();
的参数值 So my output should be something like this:
1> "01014013024", "24", "MPF", "N", "N", "A line in e2e", "", "NTE", "GOL", "", "Standard", ""
2> "01014013026", "26", "MPF", "N", "N", "A line in e2e", "", "NTE", "GOL", "", "Standard", ""
我不知道用于从java代码中读取.java文件的API。我的项目约束也是使用Java 5
答案 0 :(得分:1)
您可以使用javaparser
Maven依赖
<dependency>
<groupId>com.google.code.javaparser</groupId>
<artifactId>javaparser</artifactId>
<version>1.0.8</version>
</dependency>
分析器:
import japa.parser.JavaParser;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.body.BodyDeclaration;
import japa.parser.ast.body.MethodDeclaration;
import japa.parser.ast.body.TypeDeclaration;
import japa.parser.ast.expr.MethodCallExpr;
import japa.parser.ast.stmt.BlockStmt;
import japa.parser.ast.visitor.GenericVisitorAdapter;
import java.io.InputStream;
import java.util.List;
public class App {
public static void main(String[] args) throws Exception {
// creates an input stream for the file to be parsed
InputStream in = App.class.getResourceAsStream("ABC.java");
// if you want to use absolute path try this
// InputStream in = FileInputStream("/path/to/file/ABC.java");
CompilationUnit cu;
try {
// parse the file
cu = JavaParser.parse(in);
} finally {
in.close();
}
// prints the resulting compilation unit to default system output
visit(cu);
}
private static void visit(CompilationUnit cu) {
List<TypeDeclaration> types = cu.getTypes();
for (TypeDeclaration type : types) {
List<BodyDeclaration> members = type.getMembers();
for (BodyDeclaration member : members) {
if (member instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) member;
if ("doMessageDataOverrides".equals(method.getName())) {
BlockStmt body = method.getBody();
body.accept(new MyVisitor(), null);
}
}
}
}
}
private static class MyVisitor extends GenericVisitorAdapter {
public Object visit(MethodCallExpr n, Object arg) {
if ("setOneMoreWorkingLineWithServiceID".equals(n.getName())){
System.out.println(n.getArgs());
}
return super.visit(n, arg);
}
}
}
输出:
---更新---
项目文件:
1)这是一个maven项目。所以ABC.java文件应该在src / main / resources文件夹中。 如果你想使用绝对路径,那么尝试使用FileInputStream,如下所示。
InputStream in = FileInputStream("/path/to/file/ABC.java");
2)n.getArgs()是一个java.util.List。你可以得到每个参数。试试这个新的MyVisitor。
private static class MyVisitor extends GenericVisitorAdapter {
public Object visit(MethodCallExpr n, Object arg) {
if ("setOneMoreWorkingLineWithServiceID".equals(n.getName())){
for (Expression expression : n.getArgs()) {
System.out.println(expression.toString());
}
}
return super.visit(n, arg);
}
}
答案 1 :(得分:0)
既然你说“我不知道用于从java代码中读取.java文件的API”我会假设你在ABC.java中的方法传递给setOneMoreWorkingLineWithServiceID而不是记录所有值的值之后也被其他程序传递给setOneMoreWorkingLineWithServiceID。
如果是这种情况,您可以使用标准方式读取java中的任何文件。以下链接提供了足够的信息以帮助您入门:
http://alvinalexander.com/blog/post/java/how-open-read-file-java-string-array-list
文件打开并读入字符串后,您可以将包含调用的行解析为setOneMoreWorkingLineWithServiceID并读取值。
答案 2 :(得分:0)
让我给你发一个简单的编码来实现你的目标:
请注意:
.java
文件已保存在名称Demo.java
import java.io.*; public class FileRead { public static int findString(String str, String findStr) { int lastIndex = 0; int count =0; while(lastIndex != -1) { lastIndex = str.indexOf(findStr,lastIndex); if( lastIndex != -1) { count ++; lastIndex+=findStr.length(); } } return count; } public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); System.out.print("Enter the name function to be searched for: "); String my_fun = in.readLine(); FileReader fileReader = new FileReader(new File("Demo.java")); BufferedReader br = new BufferedReader(fileReader); String line = null; while ((line = br.readLine()) != null) { if(findString(line, my_fun)>0) { int startIndex = line.indexOf("("); int endIndex = line.indexOf(")"); String finalString = line.substring(startIndex+1,endIndex); System.out.println(finalString); } } in.close(); } }
上面显示的程序输出如下:
C:\stackOverflowUser\myCode>javac FileRead.java
C:\stackOverflowUser\myCode>java FileRead
Enter the name function to be searched for: setOneMoreWorkingLineWithServiceID
"01014013025", "25", "MPF", "N", "N", "A line in e2e", "", "NTE", "GOL", "", "Standard", ""
"01014013026", "26", "MPF", "N", "N", "A line in e2e", "", "NTE", "GOL", "", "Standard", ""
答案 3 :(得分:0)
谢谢bhdrk
我修改了你的代码以适应我的要求并且它有效。谢谢:))
这是文件:
package test;
import japa.parser.JavaParser;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.body.BodyDeclaration;
import japa.parser.ast.body.MethodDeclaration;
import japa.parser.ast.body.TypeDeclaration;
import japa.parser.ast.expr.Expression;
import japa.parser.ast.expr.MethodCallExpr;
import japa.parser.ast.stmt.BlockStmt;
import japa.parser.ast.visitor.GenericVisitorAdapter;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class JavaFileParser {
static String strFilePath = null;
static String strMainMethod = null;
static String strMethodName = null;
private static void dataSetter(String pFilePath, String pMainMethod, String pMethodName) {
strFilePath = pFilePath;
strMainMethod = pMainMethod;
strMethodName = pMethodName;
}
public static ArrayList getMethodParameters(String pFilePath, String pMainMethod, String pMethodName) throws Exception {
// creates an input stream for the file to be parsed
ArrayList args1 = null;
dataSetter(pFilePath,pMainMethod, pMethodName);
InputStream in = new FileInputStream(strFilePath);
CompilationUnit cu;
try {
// parse the file
cu = JavaParser.parse(in);
} finally {
in.close();
}
// prints the resulting compilation unit to default system output
args1 = visit(cu);
return args1;
}
private static ArrayList visit(CompilationUnit cu) {
ArrayList paramatersList = null;
List<TypeDeclaration> types = cu.getTypes();
for (TypeDeclaration type : types) {
List<BodyDeclaration> members = type.getMembers();
for (BodyDeclaration member : members) {
if (member instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) member;
if (strMainMethod.equals(method.getName())) {
BlockStmt body = method.getBody();
MyVisitor myVisitor = new MyVisitor();
body.accept(myVisitor, null);
paramatersList = myVisitor.getParamatersList();
}
}
}
}
return paramatersList;
}
private static class MyVisitor extends GenericVisitorAdapter {
private ArrayList paramatersList= new ArrayList();
public Object visit(MethodCallExpr n, Object arg) {
if (strMethodName.equals(n.getName())){
List<Expression> expressionList = n.getArgs();
paramatersList.add(expressionList) ;
}
return super.visit(n, arg);
}
public ArrayList getParamatersList() {
return paramatersList;
}
}
}