我试图从Java源代码中获取继承的方法和字段 但是我有这个问题:
我试图从包含Java代码源的文件中的每个类中查找继承的方法和字段,语法正确,但执行程序时出现一些错误
cu.accept(new VoidVisitorAdapter<Void>() {
public void visit(ClassOrInterfaceDeclaration cid, Void arg) {
super.visit(cid, arg);
cid.resolve().getAllFields().stream().forEach(af -> {
nmbre_attribus++;
});
cid.resolve().getDeclaredFields().stream().forEach(df -> {
Nmbre_attributs_déclarées++;
});
cid.resolve().getAllMethods().stream().forEach(am -> {
nmbre_methds++;
});
cid.resolve().getDeclaredMethods().stream().forEach(dm -> {
Nmbre_methodes_déclarées++;
});
}
}, null);
当我编译整个代码时,出现以下错误:
线程“主”中的异常java.lang.IllegalStateException:找不到此类型的数据。首先使用containsData进行检查。
at com.github.javaparser.ast.Node.getData(Node.java:453)
at com.github.javaparser.ast.Node.lambda$7(Node.java:703)
at java.util.Optional.map(Unknown Source)
at com.github.javaparser.ast.Node.getSymbolResolver(Node.java:702)
at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.resolve(ClassOrInterfaceDeclaration.java:276)
at Metriques.Heritage_Attributs$1.visit(Heritage_Attributs.java:66)
at Metriques.Heritage_Attributs$1.visit(Heritage_Attributs.java:1)
at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:103)
at com.github.javaparser.ast.visitor.VoidVisitorAdapter.lambda$31(VoidVisitorAdapter.java:154)
at java.util.ArrayList.forEach(Unknown Source)
at com.github.javaparser.ast.NodeList.forEach(NodeList.java:268)
at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:154)
at Metriques.Heritage_Attributs$1.visit(Heritage_Attributs.java:64)
at Metriques.Heritage_Attributs$1.visit(Heritage_Attributs.java:1)
at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:103)
at com.github.javaparser.ast.visitor.VoidVisitorAdapter.lambda$31(VoidVisitorAdapter.java:154)
at java.util.ArrayList.forEach(Unknown Source)
at com.github.javaparser.ast.NodeList.forEach(NodeList.java:268)
at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:154)
at Metriques.Heritage_Attributs$1.visit(Heritage_Attributs.java:64)
at Metriques.Heritage_Attributs$1.visit(Heritage_Attributs.java:1)
at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:103)
at com.github.javaparser.ast.visitor.VoidVisitorAdapter.lambda$43(VoidVisitorAdapter.java:175)
at java.util.ArrayList.forEach(Unknown Source)
at com.github.javaparser.ast.NodeList.forEach(NodeList.java:268)
at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:175)
at com.github.javaparser.ast.CompilationUnit.accept(CompilationUnit.java:130)
at Metriques.Heritage_Attributs.main(Heritage_Attributs.java:61)