我正在使用 java 解析器来读取 java 文件。然后我对如何在每种方法中访问变量,然后在每种方法中修改变量名称和类型有疑问。
public static void main(String[] args) throws Exception {
// creates an input stream for the file to be parsed
FileInputStream in = new FileInputStream("test.java");
CompilationUnit cu;
try {
// parse the file
cu = JavaParser.parse(in);
} finally {
in.close();
}
// change the methods names and parameters
changeMethods(cu);
// prints the changed compilation unit
System.out.println(cu.toString());
}
private static void changeMethods(CompilationUnit cu) {
List<TypeDeclaration> types = cu.getTypes();
for (TypeDeclaration type : types) {
List<BodyDeclaration> members = type.getMembers();
for (BodyDeclaration member : members) {
if (member instanceof MethodDeclaration) {
// what must i do?
}
}
}
}
[更新]
有关更多详细信息,我有如下方法:
private double[] getExtremeValues(double[] d) {
double min = Double.MAX_VALUE;
double max = -min;
}
在那种方法中,我只想用'double max1'修改'double max'。第二个问题,方法参数中如何获取'double[] d'?请帮忙!谢谢