Is there som simple way to get Caller's type full qualified name

When it is simple case, it’s easy() to get it by methodSelect().firstToken().parent().symbolType().fullyQualifiedName()
but when it is a call chain like new A().b().foo() how can I get the caller’s type of foo.
It seems difficult to do it in recursive way which I should deal with many kind of caller.

Hello @yeoleobun ,

What you call “easy” is already quite complicated to get the owner of the method.
The semantic layer of the API is supposed to help you get there in a much simpler way.

From a MethodInvocationTree, what about doing something like this:

private void doSomething(MethodInvocationTree mit) {
    Symbol methodSymbol = mit.symbol();
    if (methodSymbol.isUnknown()) {
      // method not resolved in the bytecode
    Symbol owner = methodSymbol.owner();
    if (owner.isUnknown()) {
      // unlikely since the method has been resolved
    // the type in which the method is defined
    Type ownerType = owner.type();
    // its fully qualified name
    String fullyQualifiedName = ownerType.fullyQualifiedName();

Hope this helps,

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.