{"record":{"id":"1a0cf8c1c20656d4","repo":"JetBrains/intellij-community","slug":"unable-to-extract-method-from-annotation-value","errorCode":null,"errorMessage":"Unable to extract method from annotation value","messagePattern":"Unable to extract method from annotation value","errorType":"exception","errorClass":"PrepareFailedException","httpStatus":null,"severity":"error","filePath":"java/java-impl-refactorings/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java","lineNumber":374,"sourceCode":"      }\n      else {\n        myExpression = expression;\n      }\n    }\n\n    final PsiElement codeFragment = ControlFlowUtil.findCodeFragment(myElements[0]);\n    myCodeFragmentMember = codeFragment.getUserData(ElementToWorkOn.PARENT);\n    if (myCodeFragmentMember == null) {\n      myCodeFragmentMember = codeFragment.getParent();\n    }\n    if (myCodeFragmentMember == null) {\n      PsiElement context = codeFragment.getContext();\n      LOG.assertTrue(context != null, \"code fragment context is null: \" + codeFragment.getClass());\n      myCodeFragmentMember = ControlFlowUtil.findCodeFragment(context).getParent();\n    }\n\n    if (PsiTreeUtil.getParentOfType(myElements[0].isPhysical() ? myElements[0] : myCodeFragmentMember, PsiAnnotation.class) != null) {\n      throw new PrepareFailedException(JavaRefactoringBundle.message(\"extract.method.error.annotation.value\"), myElements[0]);\n    }\n\n    myControlFlowWrapper = new ControlFlowWrapper(codeFragment, myElements);\n\n    try {\n      myExitStatements = myControlFlowWrapper.prepareAndCheckExitStatements(myElements, codeFragment);\n      if (myControlFlowWrapper.isGenerateConditionalExit()) {\n        myGenerateConditionalExit = true;\n      } else {\n        myHasReturnStatement = myExpression == null && myControlFlowWrapper.isReturnPresentBetween();\n      }\n      myFirstExitStatementCopy = myControlFlowWrapper.getFirstExitStatementCopy();\n    }\n    catch (ControlFlowWrapper.ExitStatementsNotSameException e) {\n      myExitStatements = myControlFlowWrapper.getExitStatements();\n      myNotNullConditionalCheck = areAllExitPointsNotNull(getExpectedReturnType());\n      if (!myNotNullConditionalCheck) {\n        showMultipleExitPointsMessage();","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-impl-refactorings/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java#L356-L392","documentation":"ExtractMethodProcessor passes over the selected elements and, if any lies inside a PsiAnnotation (PsiTreeUtil.getParentOfType finds an annotation ancestor, following the code-fragment member for non-physical elements), throws PrepareFailedException with 'extract.method.error.annotation.value'. Java annotation member values are highly constrained (constant expressions only), so extracting them into a method is not representable and the refactoring refuses to run.","triggerScenarios":"Selecting an expression inside an annotation value — e.g. part of the 'value = ...' argument of @SuppressWarnings(\"...\") or an array initializer inside @MyAnnotation({...}) — and invoking Extract Method.","commonSituations":"Users trying to deduplicate constant expressions used in annotations; selecting inside generated or injected code hosted in annotation values; refactorings invoked on fragments whose code-fragment member resolves into an annotation (including non-physical copies in editor previews).","solutions":["Cancel the refactoring — annotation values cannot be replaced by method calls by language rules.","Extract the shared constant into a static final constant instead, then reference that constant from the annotation (constants are allowed in annotation values).","If the annotation is your own, consider a repeatable/alias annotation or a constant defined next to it.","Make sure the selection is really the code you think: caret inside a normal method body, not an annotation argument."],"exampleFix":"// before:\n@SuppressWarnings({\"unused\", \"rawtypes\"})\nclass A { }\n// selecting \"unused\" inside the annotation and pressing Extract Method fails\n\n// after:\nclass A {\n  static final String[] SUPPRESS = {\"unused\", \"rawtypes\"};\n  @SuppressWarnings(SUPPRESS)\n  static class B { }\n}","handlingStrategy":"validation","validationCode":"PsiElement anchor = elements[0].isPhysical() ? elements[0] : codeFragmentMember;\nif (PsiTreeUtil.getParentOfType(anchor, PsiAnnotation.class) != null) {\n  // selection lives inside an annotation value: block Extract Method with an explanation\n}","typeGuard":null,"tryCatchPattern":"try { processor.prepare(); } catch (PrepareFailedException e) { /* show message and element; extraction inside annotation values is impossible */ }","preventionTips":["Never attempt to extract code inside annotation arguments; hoist the value into a constant instead.","Check the selection is in a method body (not an annotation) before offering the action."],"tags":["java","intellij","refactoring","extract-method","annotation","language-limit"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}