为什么我无法从 Kotlin 获取 PsiClass 但在 Java 中成功

Why cannot I get PsiClass from Kotlin but successful in Java

我尝试从 Kotlin 文件中获取 PsiClass(PsiFile 实例是 KtFile),但始终获取 null,在 Java 情况下是成功的。

    PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
    Editor editor = e.getData(CommonDataKeys.EDITOR);

    // null in kotlin
    PsiClass psiClass = getPsiClass(editor, psiFile);

    .....    

    private PsiClass getPsiClass(Editor editor, PsiFile file) {

        int offset = editor.getCaretModel().getOffset();
        PsiElement element = file.findElementAt(offset);

        if (element == null)
            element = file.findElementAt(offset - 1);
        if (element == null) {
            return null;
        } else {
            PsiClass target = (PsiClass) PsiTreeUtil.getParentOfType(element, PsiClass.class);
            if (target == null) {
                element = file.findElementAt(offset - 1);
                if (element == null)
                    return null;
                target = (PsiClass) PsiTreeUtil.getParentOfType(element, PsiClass.class);
            }
            return target instanceof SyntheticElement ? null : target;
        }
    }

PsiClass 仅用于 java classes. For Kotlin you should use KtClass