0

I'm trying to extract all class/interface type references from a class declarations. I'm getting a weird behavior with FQ names. Here is a simple example:

CompilationUnit cu = StaticJavaParser.parse(
  "public class foo extends java.lang.String {}");
cu.stream().
  filter(ClassOrInterfaceType.class::isInstance).
  forEach(System.out::println);

the output is:

java.lang.String
java.lang
java

I was expecting the java.lang.String but the others? What does it ever mean that java.lang is a ClassOrInterfaceType? I'm wondering if this is the expected behavior, in any case: is there a way to filter out these spurious elements? Thanks.

1 Answers1

0

You can do it recursively

String s = "class Foo extends java.lang.String {}";
    CompilationUnit cu = StaticJavaParser.parse(s);
    cu.findAll(ClassOrInterfaceDeclaration.class).forEach(cid -> {
        System.out.println(cid.getNameAsString());
        cid.getExtendedTypes().forEach(extType -> System.out.println(extType.getNameAsString()));
    });
jpl
  • 347
  • 3
  • 11