boolean (@Nullable PsiElement psiElement, @NotNull MyPsiPossibleTypes possiblePsiTypes) { if (!isInstanceOf(psiElement, possiblePsiTypes.forAnnotations)) { return false; } final var modifierListOwner = PsiTreeUtil.findFirstParent( psiElement, it -> isInstanceOf(it, possiblePsiTypes.forAnnotationOwners) && !isInstanceOf(it, possiblePsiTypes.forAnnotations)); return modifierListOwner == null // just added annotation || isInstanceOf(modifierListOwner, possiblePsiTypes.forClasses) || (isInstanceOf(modifierListOwner, possiblePsiTypes.forMethods) && !isInstanceOf(modifierListOwner, possiblePsiTypes.forVariables)); }