How to get PsiField's docComment

Answered

Hi,

I want to get a PsiJavaFile's fields doccoment, but psiField.getDocComment() return null?

Because of the source file is .class file?

It seems that PsiClassImpl can getDocComent, but It return ClsClassImpl when I call PsiClass.getFields()

Thanks

0
3 comments

Hi,

Please clarify your use case.

What is the exact code you are trying, and what are the files/classes you execute it on?

0

I want to read a Java File's methods, get the method parameters-type and return-type, get the fields-type of the parameter, get the docComment of the fields. but the docComment is null.

This is my code:


PsiFile psiFile = anActionEvent.getData(CommonDataKeys.PSI_FILE);
PsiJavaFile javaFile = psiFile instanceof PsiJavaFile ? ((PsiJavaFile) psiFile) : null;
if (javaFile == null) return null;
PsiClass firstClass = javaFile.getClasses()[0];
PsiMethod firstMethod = firstClass.getMethods()[0];
PsiParameter firstParam = firstMethod.getParameterList().getParameter(0);
PsiJavaFile firstParamPsiJavaFile = (PsiJavaFile)firstParam.getContainingFile();
PsiClass paramFirstClass = firstParamPsiJavaFile.getClasses()[0];
PsiField[] paramFirstClassFields = paramFirstClass.getFields();
PsiDocComment docComment = paramFirstClassFields[0].getDocComment();
// NPE
String docCommentText = docComment.getText();
0

According to the information from Slack, it was called on the code like:

public class User {

    // user name
    private String name;
}

And the comment is not a JavaDoc, and that's why getDocComment() returned null.

0

Please sign in to leave a comment.