{"record":{"id":"35e67ba4b3646981","repo":"aeron-io/aeron","slug":"name-code-must-equal-ordinal-value-code-code","errorCode":null,"errorMessage":"<name()> - code must equal ordinal value: code=<code>","messagePattern":"<name\\(\\)> - code must equal ordinal value: code=<code>","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"aeron-cluster/src/main/java/io/aeron/cluster/service/Cluster.java","lineNumber":76,"sourceCode":"        /**\n         * The cluster node is a candidate to become a leader in an election.\n         */\n        CANDIDATE(1),\n\n        /**\n         * The cluster node is the leader for the current leadership term.\n         */\n        LEADER(2);\n\n        static final Role[] ROLES = values();\n\n        private final int code;\n\n        Role(final int code)\n        {\n            if (code != ordinal())\n            {\n                throw new IllegalArgumentException(name() + \" - code must equal ordinal value: code=\" + code);\n            }\n\n            this.code = code;\n        }\n\n        /**\n         * The code which matches the role in the cluster.\n         *\n         * @return the code which matches the role in the cluster.\n         */\n        public final int code()\n        {\n            return code;\n        }\n\n        /**\n         * Get the role from a code read from a counter.\n         *","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/aeron-io/aeron/blob/6d60124e15e35c11b49ba2e3c2c2858a09a18803/aeron-cluster/src/main/java/io/aeron/cluster/service/Cluster.java#L58-L94","documentation":"Cluster.Role's constructor enforces that each role's numeric counter code equals its enum ordinal; a mismatch breaks the contract that the cluster role counter value maps directly to the enum. IllegalArgumentException is thrown at class initialization, an internal invariant of the enum definition.","triggerScenarios":"Adding or reordering Cluster.Role constants while passing explicit codes that no longer match ordinal positions (e.g. inserting a new role in the middle).","commonSituations":"Contributors modifying the Role enum in the Aeron source and changing order or codes inconsistently; merge conflicts that reorder constants.","solutions":["Set each Role constant's code to match its ordinal position","Do not insert role constants in the middle of the enum; append new ones","Add a test touching all Role values so the constructor check fails fast in CI"],"exampleFix":"// before\nFOLLOWER(0), MEMBER(2), LEADER(1); // codes don't match ordinals\n// after\nFOLLOWER(0), LEADER(1), MEMBER(2);","handlingStrategy":"validation","validationCode":"// For contributors: verify code==ordinal before editing Role\nCluster.Role[] roles = Cluster.Role.values();\nfor (int i = 0; i < roles.length; i++) {\n    if (roles[i].name() == null) { /* unreachable; init throws otherwise */ }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only append new Role constants; never reorder","Keep explicit codes aligned with positions","Add an enum round-trip test in CI"],"tags":["aeron","enum","invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"6d60124e15e35c11b49ba2e3c2c2858a09a18803","analyzedAt":"2026-09-12T11:17:07.683Z","contentChangedAt":"2026-09-12T11:17:07.683Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}