I am using JavaParser (open source) to parse the following code.
package testfiles.simple.tricky.before;
import testfiles.simple.before.InnerClassSample;
public class InnerClassReference {
public void ref(InnerClassSample.MyInnerClass myInnerClass, java.util.List<Long> list) {
int i = 0;
}
}
Under the methodDeclaration node named ref
, I get the parameter node hierarchy as follows:
// myInnerClass
Parameter
ClassOrInterfaceType:
ClassOrInterfaceType:
SimpleName: InnerClassSample
SimpleName: MyInnerClass
SimpleName: myInnerClass
// list
Parameter
ClassOrInterfaceType:
ClassOrInterfaceType:
ClassOrInterfaceType:
SimpleName: java
SimpleName: util
SimpleName: List
SimpleName: list
I need to find the fully qualified names for each parameter. So for myInnerClass
I would get testfiles.simple.before.InnerClassSample$MyInnerClass
and for list
it would be java.util.List
.
I understand that normally one would write List<Long>
and put an import statement instead of writing java.util.List<Long>
, however, I need to handle cases where the FQN is written in the parameter.
Now my question is, is there a way statically to distinguish if such a parsed tree is a type for a nested class, or it is merely a fully qualified name of a class?
I have thought about distinguishing by checking if the SimpleName starts with a lower case letter (meaning it is a package name), but, this is only a convention so we cannot assume safely that a developer would always start a package name with a lower case letter, or start a class name with a capital letter, hence I do not think this is a good way.
Any idea or insight about this matter would be much appreciated.