boolean (@NotNull List<? extends PsiElement> elements) { if (elements.isEmpty()) { return false; } final PsiElement firstElement = elements.get(0); if (firstElement instanceof PsiDeclarationStatement && firstElement.getFirstChild() instanceof PsiClass) { return true; } if (elements.size() < 2) { return false; } final PsiElement secondElement = elements.get(1); final PsiElement lastElement = elements.get(elements.size() - 1); if (firstElement instanceof PsiDocComment) { // might be method with javadoc return true; } else if (firstElement instanceof PsiDeclarationStatement && PsiTreeUtil.lastChild(firstElement) instanceof PsiErrorElement) { // might be method or static initializer return true; } else if (firstElement instanceof PsiErrorElement && secondElement instanceof PsiExpressionStatement && PsiTreeUtil.lastChild(secondElement) instanceof PsiErrorElement) { // might be generic method return true; } else if (firstElement instanceof PsiSwitchLabelStatement && ((PsiSwitchLabelStatement)firstElement).isDefaultCase() && PsiTreeUtil.lastChild(firstElement) instanceof PsiErrorElement && secondElement instanceof PsiDeclarationStatement) { // possible default method return true; } else if (firstElement instanceof PsiExpressionStatement && firstElement.getFirstChild() instanceof PsiMethodCallExpression && firstElement.getLastChild() instanceof PsiErrorElement && lastElement instanceof PsiBlockStatement) { // might be constructor return true; } return false; }