Мне нужна помощь в написании класса, который может просматривать другие java-файлы и отображать имя класса, имена переменных int и комментарии.
У меня есть тестовый класс, который я пытаюсь проанализировать здесь.
public class Test {
private int x;
private int y;
private String s;
public static void main(String[] args) {
// TODO Auto-generated method stub
// more comments
int l; //local variable
l = 0;
}
}
Вывод, который я ищу получить:
The Class name is : Test
There is an int variable named: x
There is an int variable named: y
Comment contains: TODO Auto-generated method stub
Comment contains: more comments
There is an int variable named: l
Comment contains: local variable
Вот код для класса, который у меня сейчас есть:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
class ExtractJavaParts {
public static void main(String args[]){
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("src/Test.Java");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null){
// Print the content on the console
if (strLine.contains("class")){
System.out.println ("The class name is: " + strLine.substring(strLine.indexOf("class ") + 6, strLine.indexOf("{")));
}
else if (strLine.contains("int")){
System.out.println("There is an int variable named: " + strLine.substring(strLine.indexOf("int ") + 4, strLine.indexOf(";")));
}
else if (strLine.contains("//")){
System.out.println("Comment contains: " + strLine.substring(strLine.indexOf("//") + 2));
}
}
//Close the input stream
in.close();
}
catch (Exception e){
//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
Это вывод на данный момент:
The class name is: Test
There is an int variable named: x
There is an int variable named: y
Comment contains: TODO Auto-generated method stub
Comment contains: more comments
There is an int variable named: l
Программа на данный момент не будет воспринимать комментарии, которые появляются после кода. Любая помощь, которую вы можете оказать, чтобы получить желаемый результат, очень ценится. Большое спасибо!