Hi, I've been trying to get Mapstruct plugin work ...
# compiler
i
Hi, I've been trying to get Mapstruct plugin work for kotlin, so far I'm stucked to getting the return type of the function. How do i get 
KtNamedFunction
 's return type in form like "`xxx.xxx.TestClass`"? I've only managed to get the short class name as "`TestClass`" with code like this:
Copy code
ktNamedFunction.getTypeReference().getText()
And here's Mapstruct plugin in Github: https://github.com/mapstruct/mapstruct-idea
s
Hey, you can use
BindingContext
to retrieve function descriptor and get the
KotlinType
from there. Then you can use it to retrieve class descriptor which contains fqName field you need :) I am away from the laptop now, but can provide more info later, if needed :)
i
@shikasd thx for your reply, how can i get
BindingContext
in
PsiReferenceProvider
? Here's example code:
Copy code
public class MappingTargetReferenceProviderKotlin extends PsiReferenceProvider {

  @NotNull
  @Override
  public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
    KtNamedFunction ktNamedFunction = PsiTreeUtil.getParentOfType(element, KtNamedFunction.class)
    KtTypeReference ktTypeReference = ktNamedFunction.getTypeReference();
    // TODO get BindingContext here
  }
}
Well... I got
UnresolvedType
from following code, what have i missed?
Copy code
KtNamedFunction ktFun = PsiTreeUtil.getParentOfType(element, KtNamedFunction.class)
BindingContext bindingContext = ResolutionUtils.analyze(ktFun, BodyResolveMode.FULL);
KotlinType type = bindingContext.get(BindingContext.FUNCTION, ktFun).getReturnType();
s
Hey, sorry, I was a bit busy couple of last days, did get to read this slack much Is your function / type correct from syntax perspective? Also I didn't realize you are working from IDE side, I think there should be an easier way of doing these things, let me check
Hm, seems like you've done everything right 🙂 Also a tip regarding performance, you maybe don't want to resolve the whole body, only the top declaration (see
BodyResolveMode
)
IDE also has
KtNamedFunction.resolveToDescriptorIfAny
, which seems to do similar things, but maybe it works better ¯\_(ツ)_/¯
i
Thank u for your advice, I'll continue looking into this 🤔
Well... turns out I got
UnresolvedType
because the return type isn't exactly a
KotlinType
, it's a Java type. But how can I get a Java return type from
KtNamedFunction
? Any idea @shikasd?
s
Quite interesting, Kotlin should transform those types in a similar way. Did not encounter such thing before 🙂 Are you sure plugin is aware of Java sources / classes you are resolving?
i
Uh... I can't really tell if the plugin is aware of Java classes under the circumstance. But I made Java version test of it (with
PsiMethod
approach), and either Java class or Kotlin class is resolved correctly.