{"record":{"id":"94417ed55ab8195c","repo":"quarkusio/quarkus","slug":"annotationinstance-instance-is-an-invalid-targe","errorCode":null,"errorMessage":"AnnotationInstance ${instance} is an invalid target. Only Class targets are supported","messagePattern":"AnnotationInstance (.+?) is an invalid target\\. Only Class targets are supported","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"extensions/spring-di/deployment/src/main/java/io/quarkus/spring/di/deployment/SpringDIProcessor.java","lineNumber":502,"sourceCode":"        // in Spring List<SomeBean> means that all instances of SomeBean should be injected\n        List<Type> parameters = methodInfo.parameterTypes();\n        for (int i = 0; i < parameters.size(); i++) {\n            Type parameter = parameters.get(i);\n            if (parameter.name().equals(DotNames.LIST)) {\n                annotationsToAdd.add(create(\n                        QUARKUS_ALL_ANNOTATION,\n                        MethodParameterInfo.create(methodInfo, (short) i),\n                        Collections.emptyList()));\n            }\n        }\n    }\n\n    /**\n     * Meant to be called with instances of @Component, @Service, @Repository\n     */\n    private String getBeanNameFromStereotypeInstance(AnnotationInstance annotationInstance) {\n        if (annotationInstance.target().kind() != AnnotationTarget.Kind.CLASS) {\n            throw new IllegalStateException(\n                    \"AnnotationInstance \" + annotationInstance + \" is an invalid target. Only Class targets are supported\");\n        }\n        final AnnotationValue value = annotationInstance.value();\n        if ((value == null) || value.asString().isEmpty()) {\n            return getDefaultBeanNameFromClass(annotationInstance.target().asClass().name().toString());\n        } else {\n            return value.asString();\n        }\n    }\n\n    /**\n     * Meant to be called with instances of @Bean\n     */\n    private String getBeanNameFromBeanInstance(AnnotationInstance annotationInstance) {\n        if (annotationInstance.target().kind() != AnnotationTarget.Kind.METHOD) {\n            throw new IllegalStateException(\n                    \"AnnotationInstance \" + annotationInstance + \" is an invalid target. Only Method targets are supported\");\n        }","sourceCodeStart":484,"sourceCodeEnd":520,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/spring-di/deployment/src/main/java/io/quarkus/spring/di/deployment/SpringDIProcessor.java#L484-L520","documentation":"The Spring DI extension derives bean names from stereotype annotations (@Component, @Service, @Repository). This method expects the annotation instance to target a class; when the target is anything else (method, field, parameter), the build fails with this IllegalStateException. Only class-level stereotype placement is supported for bean-name derivation.","triggerScenarios":"@Component/@Service/@Repository applied to a non-class target — e.g., a field or method parameter — via a meta-annotation or custom composed annotation that the index resolves with a non-CLASS target.","commonSituations":"Copy-pasting @Component onto a field; custom meta-annotations inadvertently exposing stereotype annotations to non-class elements; Spring's non-component usages (e.g., @Repository on exceptions for exception translation) being picked up by the DI processor.","solutions":["Move @Component/@Service/@Repository to class level only; use @Bean methods in a @Configuration class for method-level registration","If @Repository is used on an exception class for Spring exception translation, drop it or guard it — Quarkus doesn't need it","Refactor custom composed annotations so stereotype annotations are only @Target(TYPE)","Check index results for stray stereotype annotations on non-class members and remove them"],"exampleFix":"// before\npublic class Config {\n    @Component\n    private MyService service; // invalid target\n}\n\n// after\n@Component\npublic class MyService { }","handlingStrategy":"validation","validationCode":"// Guard custom annotations to class targets only:\n@Target(ElementType.TYPE)\n@Retention(RetentionPolicy.RUNTIME)\n@Component\npublic @interface MyService {}\n// and never place @Component/@Service/@Repository on fields or parameters","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Restrict stereotype annotations to ElementType.TYPE in composed annotations","Use @Bean methods for programmatic bean registration","Grep codebase for stereotype annotations on non-class declarations","Remove @Repository from exception classes in Quarkus apps"],"tags":["quarkus","spring-di","annotations","build-time","cdi"],"backgroundTag":"invalid-annotation-target","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}