java - 如何从Java文件中获取变量名列表?
问题描述
如何从 Java 文件中获取变量列表?到目前为止,我已经开始阅读文件并使用空格分隔每个单词。不幸的是,这将返回所有导入语句、注释..等
public ArrayList<String> getVariables(String javaFilePath) {
ArrayList<String> variableList = new ArrayList<String>();
BufferedReader br;
try {
// creating a buffer reader from the file path
br = new BufferedReader(new FileReader(new File(javaFilePath)));
String line;
while ((line = br.readLine()) != null) {
String[] variables = line.split("\\s+");
for (String variable : variables) {
variableList.add(variable);
}
}
br.close();
} catch (FileNotFoundException e) {
logger.error("This is FileNotFoundException error : " + e.getMessage());
} catch (IOException e) {
logger.error("This is IOException error : " + e.getMessage());
}
return variableList;
}
例如:我在 C:\Sample.java 上保存了 java 文件。它的代码如下所示:
package com.test;
import java.io.File;
import java.io.FileInputStream;
public class Sample {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
使用上述方法的输出返回以下内容:
package
com.test;
import
java.io.File;
import
java.io.FileInputStream;
public
class
Sample
{
String
name;
public
String
getName()
{
return
name;
}
public
void
setName(String
name)
{
this.name
=
name;
}
}
问题:如何修改上面显示的方法以便仅获取变量。例如:我只想要上述类的“名称”和“样本”。
解决方案
推荐阅读
- html - 如何影响所选卡的部分
- firebase - 如何将 react-native-leaderboard 与 firestore 中的值一起使用
- r - 更改ggplot中某些类别的线型?
- opencv - CMake 错误:CUDA_cublas_LIBRARY(高级)
- javascript - 可编辑的 div 仅显示 foreach 循环中的第一个 div 元素
- linux - /usr/bin/nice -n -19: 没有这样的文件或目录但是 nice 存在
- r - filter() 中的 str_split()
- node.js - Docker/Reactjs/Nodejs - 如何在 docker-compose up 后安装依赖项并运行应用程序
- angular - 递归组件数据引用问题
- python - 使用带有模型字段 django 的 if 语句