在我的例子中,我有类似的东西:
public class Example extends Random, Math {...}
使用JavaParser我想获得在'扩展'之后的类的名称。关键词。
我该怎么办?
在JavaParser的网站上我找到了类似的东西:
compilationUnit.getNodesByType(FieldDeclaration.class).stream().
filter(f -> f.getModifiers().contains(PUBLIC) &&
!f.getModifiers().contains(STATIC)).
forEach(f -> System.out.println("Check field at line " + f.getBegin().get().line));
或者也许应该在visit()方法中完成?
感谢您的建议。
答案 0 :(得分:1)
您只需要查找getExtends方法
来源:我是JavaParser提交者
答案 1 :(得分:0)
您可以使用此代码非常有用:
public class inheritedClasses extends VoidVisitorAdapter<Void> {
//If you use a file which contained java code source
private static final String FILE_PATH ="//put the path of your file here;if your file is in the same package use just src/main.... but if it is in another package use the whole path ";
public void visit(ClassOrInterfaceDeclaration cid , Void arg){
super.visit(cid,arg);
//I guess you just want to print the inherited class's name at the console
System.out.println(""+cid.getExtendedTypes());
}
public static void main(String[] args){
CompilationUnit cu = StaticJavaParser(new File(FILE_PATH));
VoidVisitor<Void> methodinherts = new inheritedClasses();
methodinherts.visit(cu,null);
}
}