Detecting to which class a missing method should belong
In my plugin I'm trying to detect to which class a missing method would belong. IntelliJ does know on which class to place the method if I use the quick fix. Now I would like to know how that QuickFix 'detects' where to place the new method.
Our current implementation is getting the PsiElement from a HighLightInfo instance. Then we are using the RefactoringUtil.getTypeByExpressionWithExpectedType method to detect the type of the preceding expression of that PsiElement (often a PsiIdentifier or PsiJavaToken), but that sometimes returns a wrong type (or null which for us is better than the wrong type).
Can you please give me a hint where to look or which method to use in this case?
Several examples with the missing method underlined:
1. ---
CamelCase.upper("") --> Should give "CamelCase"
2. ---
CamelCase b = new CamelCase();
b.upper("") -> Should give "CamelCase"
3. ----
Utils.getCamelCase().upper(); --> Should give 'CamelCase' if getCamelCase() has CamelCase as return type.
4. ----
new CamelCase().upper("") --> Should give 'CamelCase'
Please sign in to leave a comment.
We have moved to making use of the method CreateFromUsageBaseFix.getTargetClasses() .
I'm still interested to hear how 'stable' this method is, because it is not on an OpenAPI class.