I am gathering class metadata using JavaParser to store in a JSON object. For each compilation unit I collect, I also collect a list of the FieldDeclarations. For each FieldDeclaration, I would like to see if the type is an interface type.
In the java doc I noticed that FieldDeclaration inherits isClassOrInterfaceDeclaration()
from BodyDeclaration, however I would like something like isInterfaceDeclaration()
.
I notice, however, that the class ClassOrInterfaceDeclaration
has a method isInterface()
.
Would it be irresponsible to take a FieldDeclaration f
and do something like:
Boolean b = f.toClassOrInterfaceDeclaration().isInterface()
Ultimately I would like to distinguish whether a FieldDeclaration is of a Class type, or Interface type.
Something I have also considered is:
Type t = f.getElementType() ;
// Dodgy code ahead
if(t == Class) { // do something...}
If someone can point in me in the right direction, any help would be greatly appreciated.
Edit: Some exploratory testing I did yielded some unexpected results. I collected a list of Field declarations from a class, the fields are:
private String s;
private String addedState ;
// Component is an interface
private Component c ;
When performing isClassOrInterfaceDeclaration()
on each of these fields, each one returned false. But when I performed:
f.forEach(n->System.out.println(n.getElementType().isClassOrInterfaceType())) ;
each one returned true.
My assumptions about what the expected output would be have been proven false.
Edit 2: I have come to realize why calling isClassOrInterfaceDeclaration() does not yield true, because the fields are literally not declaring a class or interface. I need to find a way to determine if the type is a Class or Interface.