Reading type information on a line?

Hello,

Is there a way I can, given the line on a Document, figure out which types are being referenced in the code?

for instance,

String a = "abc"

String b = "def"

a = b; // given this line, I want to access the type information of "a"

 

I'm going to use this for a debugging program.

Thanks.

0
1 comment
Official comment

You can get PsiFile corresponding to the document (PsiDocumentManager#getPsiFile), find all PSI elements in the range corresponding to the line (e.g. by using PsiFile#findElementAt and then traversing the hierarchy), then check which of them are instanceof PsiExpression (if it's Java, otherwise some other language-specific kind of expression), call getType on it and analyze it (e.g. PsiUtil.resolveClassInType).

Please sign in to leave a comment.