Guest User

Untitled

a guest
Nov 15th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. class A {
  2. int x;
  3. public void method2() {...}
  4. class B {
  5. int number;
  6. public void methods() {...}
  7. }
  8. }
  9.  
  10. CompilationUnit cu = JavaParser.parse(in);
  11.  
  12. ClassVisitor classVisitor = new ClassVisitor();
  13. classVisitor.visit(cu, null);
  14.  
  15. class ClassVisitor extends VoidVisitorAdapter<Void> {
  16. @Override
  17. public void visit(ClassOrInterfaceDeclaration n, Void arg) {
  18. System.out.println(n.getFields());
  19.  
  20. // get class methods
  21. for(MethodDeclaration method : n.getMethods()) {
  22. System.out.println("Name :" + method.getName());
  23. System.out.println("Body :" + method.getBody().get());
  24. }
  25. }
  26. }
  27. }
  28.  
  29. CompilationUnit cu = JavaParser.parse(in);
  30.  
  31. for(TypeDeclaration<?> type : cu.getTypes()) {
  32. log.info("Type Name :{}", type.getName());
  33. }
  34.  
  35. for(TypeDeclaration type : cu.getTypes()) {
  36. // first give all this java doc member
  37. List<BodyDeclaration> members = type.getMembers();
  38. // check all member content
  39. for(BodyDeclaration member : members) {
  40. // if member state equal ClassOrInterfaceDeclaration, and you can identify it which is inner class
  41. if(member.isClassOrInterfaceDeclaration()) {
  42. log.info("class name :{}", member.asClassOrInterfaceDeclaration().getName());
  43. // get inner class method
  44. for(MethodDeclaration method : member.asClassOrInterfaceDeclaration().getMethods()) {
  45. log.info("Method Name :{}", method.getName());
  46. }
  47. VerifyInnerClassAndParse(member.asClassOrInterfaceDeclaration());
  48. }
  49. }
  50. }
Add Comment
Please, Sign In to add comment