I use the https://github.com/javaparser/javaparser to parse the java source code
I tried a lot of methods to parse inner class ; like this :
class A {
int x;
public void method2() {...}
class B {
int number;
public void methods() {...}
}
}
I try to parse class B and it's variables and methods, but I failed.
Is there any example to show how to get the B class?
I can parse class A methods name and content or variables content, like this:
CompilationUnit cu = JavaParser.parse(in);
ClassVisitor classVisitor = new ClassVisitor();
classVisitor.visit(cu, null);
class ClassVisitor extends VoidVisitorAdapter<Void> {
@Override
public void visit(ClassOrInterfaceDeclaration n, Void arg) {
System.out.println(n.getFields());
// get class methods
for(MethodDeclaration method : n.getMethods()) {
System.out.println("Name :" + method.getName());
System.out.println("Body :" + method.getBody().get());
}
}
}
}
But I try to parse class B varible and method ,failed!
try the CompilationUnit.getTypes(), like this :
CompilationUnit cu = JavaParser.parse(in);
for(TypeDeclaration<?> type : cu.getTypes()) {
log.info("Type Name :{}", type.getName());
}
result : n.s.dictionary.parse.JavaCodeParse : Type Name :A
Not resolved to B class