{"record":{"id":"ca2e42d81fe56b67","repo":"spring-projects/spring-security","slug":"unsupported-element-of-type-element-getclass-ca2e42","errorCode":null,"errorMessage":"Unsupported element of type {element.getClass()}","messagePattern":"Unsupported element of type (.+?)","errorType":"exception","errorClass":"AnnotationConfigurationException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/springframework/security/core/annotation/UniqueSecurityAnnotationScanner.java","lineNumber":127,"sourceCode":"\t\tAssert.notNull(types, \"types cannot be null\");\n\t\tthis.types = types;\n\t}\n\n\t@Override\n\tMergedAnnotation<A> merge(AnnotatedElement element, @Nullable Class<?> targetClass) {\n\t\tif (element instanceof Parameter parameter) {\n\t\t\treturn this.uniqueParameterAnnotationCache.computeIfAbsent(parameter, (p) -> {\n\t\t\t\tList<MergedAnnotation<A>> annotations = findParameterAnnotations(p);\n\t\t\t\treturn requireUnique(p, annotations);\n\t\t\t});\n\t\t}\n\t\tif (element instanceof Method method) {\n\t\t\treturn this.uniqueMethodAnnotationCache.computeIfAbsent(new MethodClassKey(method, targetClass), (k) -> {\n\t\t\t\tList<MergedAnnotation<A>> annotations = findMethodAnnotations(method, targetClass);\n\t\t\t\treturn requireUnique(method, annotations);\n\t\t\t});\n\t\t}\n\t\tthrow new AnnotationConfigurationException(\"Unsupported element of type \" + element.getClass());\n\t}\n\n\tprivate @Nullable MergedAnnotation<A> requireUnique(AnnotatedElement element,\n\t\t\tList<MergedAnnotation<A>> annotations) {\n\t\treturn switch (annotations.size()) {\n\t\t\tcase 0 -> null;\n\t\t\tcase 1 -> annotations.get(0);\n\t\t\tdefault -> {\n\t\t\t\tList<Annotation> synthesized = new ArrayList<>();\n\t\t\t\tfor (MergedAnnotation<A> annotation : annotations) {\n\t\t\t\t\tsynthesized.add(annotation.synthesize());\n\t\t\t\t}\n\t\t\t\tthrow new AnnotationConfigurationException(\"\"\"\n\t\t\t\t\t\tPlease ensure there is one unique annotation of type %s attributed to %s. \\\n\t\t\t\t\t\tFound %d competing annotations: %s\"\"\".formatted(this.types, element, annotations.size(),\n\t\t\t\t\t\tsynthesized));\n\t\t\t}\n\t\t};","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/core/src/main/java/org/springframework/security/core/annotation/UniqueSecurityAnnotationScanner.java#L109-L145","documentation":"UniqueSecurityAnnotationScanner.merge supports only Class and Method elements; for any other AnnotatedElement type it throws AnnotationConfigurationException, since a classpath/annotation-configuration problem is implied. It enforces that exactly one matching security annotation exists on the element.","triggerScenarios":"Passing a Field/Constructor/Parameter (anything not Class or Method) to the scanner's merge, e.g. from a custom metadata source scanning other reflective element kinds.","commonSituations":"Custom security annotation scanning setups; framework hooks that iterate over all AnnotatedElements of a type instead of just classes and methods.","solutions":["Only call merge with Class or Method elements","Filter the element type before invoking the scanner","Provide a custom scanner implementation if other element types must be supported"],"exampleFix":"// before\nreturn scanner.merge(element, targetClass); // element may be a Field\n// after\nif (element instanceof Class || element instanceof Method) {\n    return scanner.merge(element, targetClass);\n}\nreturn null;","handlingStrategy":"type-guard","validationCode":"if (!(element instanceof Class) && !(element instanceof Method)) { return null; }","typeGuard":"boolean isSupportedElement(AnnotatedElement e) { return e instanceof Class || e instanceof Method; }","tryCatchPattern":null,"preventionTips":["Only pass Class or Method to the unique scanner","Filter unsupported AnnotatedElement types upstream","Keep custom metadata sources restricted to methods/classes"],"tags":["annotation-scanning","unsupported-type","authorization"],"backgroundTag":"unsupported-operation","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}