{"record":{"id":"1fb1d39dc7010add","repo":"junit-team/junit5","slug":"unsupported-annotatedelement-type-for-tempdir-s","errorCode":null,"errorMessage":"Unsupported AnnotatedElement type for @TempDir: %s","messagePattern":"Unsupported AnnotatedElement type for @TempDir: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-api/src/main/java/org/junit/jupiter/api/io/TempDirDeletionStrategy.java","lineNumber":139,"sourceCode":"\n\t\t\treturn DeletionResult.builder(tempDir).build();\n\t\t}\n\n\t\tprivate void logWarning(AnnotatedElementContext elementContext, DeletionException exception) {\n\t\t\tlogger.warn(exception, () -> \"Failed to delete all temporary files for %s\".formatted(\n\t\t\t\tdescriptionFor(elementContext.getAnnotatedElement())));\n\t\t}\n\n\t\t@API(status = INTERNAL, since = \"6.1\")\n\t\tpublic static String descriptionFor(AnnotatedElement annotatedElement) {\n\t\t\tif (annotatedElement instanceof Field field) {\n\t\t\t\treturn \"field \" + field.getDeclaringClass().getSimpleName() + \".\" + field.getName();\n\t\t\t}\n\t\t\tif (annotatedElement instanceof Parameter parameter) {\n\t\t\t\tExecutable executable = parameter.getDeclaringExecutable();\n\t\t\t\treturn \"parameter '\" + parameter.getName() + \"' in \" + descriptionFor(executable);\n\t\t\t}\n\t\t\tthrow new IllegalStateException(\"Unsupported AnnotatedElement type for @TempDir: \" + annotatedElement);\n\t\t}\n\n\t\tprivate static String descriptionFor(Executable executable) {\n\t\t\tboolean isConstructor = executable instanceof Constructor<?>;\n\t\t\tString type = isConstructor ? \"constructor\" : \"method\";\n\t\t\tString name = isConstructor ? executable.getDeclaringClass().getSimpleName() : executable.getName();\n\t\t\treturn \"%s %s(%s)\".formatted(type, name,\n\t\t\t\tClassUtils.nullSafeToString(Class::getSimpleName, executable.getParameterTypes()));\n\t\t}\n\t}\n\n\t/**\n\t * Standard {@link TempDirDeletionStrategy} implementation that recursively\n\t * deletes all files and directories within the temporary directory.\n\t *\n\t * <p>Symbolic and other types of links, such as junctions on Windows, are\n\t * not followed. A warning is logged when deleting a link that targets a\n\t * location outside the temporary directory.","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/junit-team/junit5/blob/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/io/TempDirDeletionStrategy.java#L121-L157","documentation":"IllegalStateException from the internal TempDirDeletionStrategy.IgnoreFailures.descriptionFor(AnnotatedElement) helper, which only knows how to describe Fields and Parameters. If the AnnotatedElement passed in is neither (e.g. a Class, Method, Constructor, or Module), the method cannot produce a human-readable description and throws. It is an internal (@API INTERNAL) safeguard indicating an unsupported element context reached the @TempDir cleanup path.","triggerScenarios":"An extension or custom TempDirDeletionStrategy calls descriptionFor(...) with an AnnotatedElement that is not a java.lang.reflect.Field or java.lang.reflect.Parameter. Normal @TempDir usage (field or parameter injection) never hits this; it requires an unusual element-context implementation.","commonSituations":"Writing a custom TempDirDeletionStrategy / AnnotatedElementContext that returns a Method or Class as its annotated element; integration code that reuses the internal descriptionFor helper; an edge case where a constructor-level element is misclassified.","solutions":["If you wrote a custom AnnotatedElementContext, ensure getAnnotatedElement() returns the actual Field or Parameter annotated with @TempDir.","Do not reuse the internal IgnoreFailures.descriptionFor helper for arbitrary element types; build your own description string.","File a JUnit issue if this occurs with stock @TempDir field/parameter injection — it indicates a framework bug."],"exampleFix":"// before: custom context returns a Method\npublic AnnotatedElement getAnnotatedElement() { return method; }\n// after\npublic AnnotatedElement getAnnotatedElement() { return parameter; }","handlingStrategy":"type-guard","validationCode":"AnnotatedElement ae = ctx.getAnnotatedElement();\nif (!(ae instanceof Field || ae instanceof Parameter)) {\n    throw new IllegalArgumentException(\"Unsupported element: \" + ae);\n}","typeGuard":"static boolean isSupportedTempDirElement(AnnotatedElement ae) {\n    return ae instanceof Field || ae instanceof Parameter;\n}","tryCatchPattern":null,"preventionTips":["Custom AnnotatedElementContext must return the actual @TempDir Field or Parameter.","Don't reuse the internal descriptionFor helper for arbitrary element types.","Report framework bugs if stock injection triggers this."],"tags":["tempdir","illegal-state","internal-api","reflection","extension"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}