boolean (PsiExpression expression) { PsiElement parent = expression.getParent(); if (parent instanceof PsiCallExpression) { return true; } if (expression instanceof PsiReferenceExpression && parent instanceof PsiReferenceExpression) { PsiElement target = ((PsiReferenceExpression)expression).resolve(); if (target instanceof PsiPackage || target instanceof PsiClass) { return true; } if (target == null) { // unresolved qualifier: if it's just reference chain like a.b.c it could be inaccessible package, so let's avoid parenthesizing it PsiExpression qualifier = expression; while (qualifier instanceof PsiReferenceExpression) { qualifier = ((PsiReferenceExpression)qualifier).getQualifierExpression(); } return qualifier == null; } } if (expression instanceof PsiArrayInitializerExpression && parent instanceof PsiArrayInitializerExpression) { return true; } if (expression instanceof PsiSuperExpression) { return true; } return false; }