{"record":{"id":"262550d396a332b5","repo":"alibaba/ARouter","slug":"the-inject-fields-can-not-be-private-please-c","errorCode":null,"errorMessage":"The inject fields CAN NOT BE 'private'!!! please check field [<field>] in class [<class>]","messagePattern":"The inject fields CAN NOT BE 'private'!!! please check field \\[<field>\\] in class \\[<class>\\]","errorType":"validation","errorClass":"IllegalAccessException","httpStatus":null,"severity":"error","filePath":"arouter-compiler/src/main/java/com/alibaba/android/arouter/compiler/processor/AutowiredProcessor.java","lineNumber":267,"sourceCode":"            case DOUBLE:\n                return \"getDouble\";\n            default:\n                throw new IllegalArgumentException(\"Unsupported primitive type: \" + TypeKind.values()[type]);\n        }\n    }\n\n    /**\n     * Categories field, find his papa.\n     *\n     * @param elements Field need autowired\n     */\n    private void categories(Set<? extends Element> elements) throws IllegalAccessException {\n        if (CollectionUtils.isNotEmpty(elements)) {\n            for (Element element : elements) {\n                TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();\n\n                if (element.getModifiers().contains(Modifier.PRIVATE)) {\n                    throw new IllegalAccessException(\"The inject fields CAN NOT BE 'private'!!! please check field [\"\n                            + element.getSimpleName() + \"] in class [\" + enclosingElement.getQualifiedName() + \"]\");\n                }\n\n                if (parentAndChild.containsKey(enclosingElement)) { // Has categries\n                    parentAndChild.get(enclosingElement).add(element);\n                } else {\n                    List<Element> childs = new ArrayList<>();\n                    childs.add(element);\n                    parentAndChild.put(enclosingElement, childs);\n                }\n            }\n\n            logger.info(\"categories finished.\");\n        }\n    }\n}\n","sourceCodeStart":249,"sourceCodeEnd":284,"githubUrl":"https://github.com/alibaba/ARouter/blob/84f451d244e3fb1d0e37801de1b9ee2af2f81d68/arouter-compiler/src/main/java/com/alibaba/android/arouter/compiler/processor/AutowiredProcessor.java#L249-L284","documentation":"Thrown by AutowiredProcessor.categories when an annotated @Autowired field is declared private. ARouter generates code in a separate helper class that writes injected values into the field reflectively/generated bytecode, so private fields cannot be assigned. The compiler aborts processing with the field and enclosing class named in the message.","triggerScenarios":"Declaring a field with @Autowired (or @Autowired in a class processed by ARouter) with the `private` modifier, detected during annotation processing via element.getModifiers().contains(Modifier.PRIVATE).","commonSituations":"Kotlin `private var` fields annotated with @Autowired; Java fields made private for encapsulation; refactoring previously package-private fields to private without removing the annotation.","solutions":["Remove the `private` modifier from the annotated field (use public or package-private).","Use @Autowired(name = \"key\") on a non-private field and keep the private one as a wrapper if encapsulation is needed.","In Kotlin, avoid `private` on @Autowired fields; declare as `var`/`lateinit var` without private."],"exampleFix":"// before\n@Autowired\nprivate String name;\n\n// after\n@Autowired\nString name; // non-private so ARouter can inject","handlingStrategy":"validation","validationCode":"for (Field f : activity.getDeclaredFields()) {\n    if (f.isAnnotationPresent(Autowired.class) && Modifier.isPrivate(f.getModifiers())) {\n        throw new IllegalStateException(\"@Autowired field must not be private: \" + f.getName());\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never combine `private` with @Autowired.","In Kotlin, declare @Autowired fields as public `var` or `lateinit var`.","Add a lint/checkstyle rule scanning for private @Autowired fields in CI."],"tags":["annotation-processor","arouter","field-visibility"],"backgroundTag":"insufficient-permissions","analyzedSha":"84f451d244e3fb1d0e37801de1b9ee2af2f81d68","analyzedAt":"2026-09-06T13:30:41.084Z","contentChangedAt":"2026-09-06T13:30:41.084Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}