intelliJ get classname of variable at carret

Answered

Hi,

anyone knows how to get the classname of a variable under the mouse caret ? 

 

I started something like that

 

final PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
Project project = CommonDataKeys.PROJECT.getData(dataContext);
PsiElement elementAt = file.getViewProvider().findElementAt(editor.getCaretModel().getOffset(), JavaLanguage.INSTANCE);


i guess there is some whay of getting the classname of my elementAt ?

 

 

code ex

public class OneClass{
private static final MyObject ONE_CONSTANT = ....


private Object createToolbar() {
Object appGroup = new MyClass(ONE_CONSTANT[caretIsOnThisWord]);
....
}
}

So i don't want the method to tell me OneClass (this I managed to do :P )
but I want the method to tell me that I'm on a MyObject element

0
2 comments

Use PsiViewer to see what the underlying PSI is. Use PsiTreeUtil to walk up to containing PsiReferenceExpression and then call resolve(), in this case it should resolve to PsiField which has getType()

0

Thanks I didn't knew this plugin.

needed to search a bit but found the solution (null checks are needed I just give it in case someone else needs it)  :

PsiTreeUtil.getParentOfType(elementAt, PsiReferenceExpression.class).getType().getPresentableText()
0

Please sign in to leave a comment.