How to get parameter values of current PsiElement?


Hi there,

I'm stuck in how to get actual parameter inputs in my plugin. I know PsiMethod for now.

For example,

* Signature of update:
* update(String ckey, String statement, Object parameter)
getSqlSession().update("someKey", "updateLikeCount", params);

I want to know the input of ckey is "someKey", and of statement is "updateLikeCount".

My current codes are:

int offset = editor.getCaretModel().getOffset();
PsiElement element = GotoDeclarationAction.findTargetElement(project, editor, offset);

if (element instanceof PsiMethod) {
PsiParameter[] parameters = ((PsiMethod) element).getParameterList().getParameters();
for (PsiParameter param : parameters) {
// want to get param value here...

Look forward to any help!

Thanks :)


Official comment

You can find PsiMethodCallExpression at the caret position (e.g. using com.intellij.psi.util.PsiTreeUtil#findElementOfClassAtOffset), and its call.getArgumentList().getExpressions() will provide you all call arguments. Then you can match them with parameters by index. Please be aware of vararg calls and even plain typos, where the argument count might differ from the parameter count.

I resolved it! Thanks alot!!


Please sign in to leave a comment.