1

In my example I have something like this:

public class Example extends Random, Math {...} 

Using JavaParser I want to get the names of classes that are after 'extends' keyword.

How can I do it?

On the site of JavaParser I have found something like this:

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));

Or maybe it should be done in visit() method?

Thanks for your advices.

mexicoman
  • 23
  • 6

2 Answers2

1

You just need to look for the getExtends method

Source: I am a JavaParser committer

Federico Tomassetti
  • 2,100
  • 1
  • 19
  • 26
0

you can use this code is very useful :

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);  




}



}