ThreeState (String text, PsiElement context) { if (text.isEmpty()) { return ThreeState.NO; } final Project project = context.getProject(); final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(project); final PsiElement fragment; PsiElement parent = context.getParent(); if (parent instanceof PsiMethod method) { if (!MethodUtils.isInsideMethodBody(context, method)) { parent = method.getParent(); } } else if (parent instanceof PsiField) { parent = parent.getParent(); } else if (parent instanceof PsiClass aClass) { if (!ClassUtils.isInsideClassBody(context, aClass)) { parent = aClass.getParent(); } } if (parent instanceof PsiJavaFile) { fragment = PsiFileFactory.getInstance(project).createFileFromText("__dummy.java", JavaFileType.INSTANCE, text); } else if (parent instanceof PsiClass) { fragment = factory.createMemberCodeFragment(text, context, false); } else { fragment = factory.createCodeBlockCodeFragment(text, context, false); } final boolean allowDanglingElse = isIfStatementWithoutElse(PsiTreeUtil.getPrevSiblingOfType(context, PsiStatement.class)); if (!isInvalidCode(fragment, allowDanglingElse)) { return ThreeState.YES; } else if (PsiTreeUtil.getDeepestLast(fragment) instanceof PsiErrorElement) { return ThreeState.NO; } return ThreeState.UNSURE; }