{"record":{"id":"34d72e33a5d43872","repo":"MyCATApache/Mycat-Server","slug":"rule-function-must-implements-abstractpartitiona","errorCode":null,"errorMessage":"rule function must implements ${AbstractPartitionAlgorithm.class.getName()}, name=${name}","messagePattern":"rule function must implements (.+?), name=(.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/config/loader/xml/XMLRuleLoader.java","lineNumber":222,"sourceCode":"\t\t\t\t//根据class利用反射新建分片算法\r\n\t\t\t\tAbstractPartitionAlgorithm function = createFunction(name, clazz);\r\n\t\t\t\t//根据读取参数配置分片算法\r\n\t\t\t\tParameterMapping.mapping(function, ConfigUtil.loadElements(e));\r\n\t\t\t\t//每个AbstractPartitionAlgorithm可能会实现init来初始化\r\n\t\t\t\tfunction.init();\r\n\t\t\t\t//放入functions map\r\n\t\t\t\tfunctions.put(name, function);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tprivate AbstractPartitionAlgorithm createFunction(String name, String clazz)\r\n\t\t\tthrows ClassNotFoundException, InstantiationException,\r\n\t\t\tIllegalAccessException, InvocationTargetException {\r\n\t\tClass<?> clz = Class.forName(clazz);\r\n\t\t//判断是否继承AbstractPartitionAlgorithm\r\n\t\tif (!AbstractPartitionAlgorithm.class.isAssignableFrom(clz)) {\r\n\t\t\tthrow new IllegalArgumentException(\"rule function must implements \"\r\n\t\t\t\t\t+ AbstractPartitionAlgorithm.class.getName() + \", name=\" + name);\r\n\t\t}\r\n\t\treturn (AbstractPartitionAlgorithm) clz.newInstance();\r\n\t}\r\n\r\n}","sourceCodeStart":204,"sourceCodeEnd":228,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/config/loader/xml/XMLRuleLoader.java#L204-L228","documentation":"createFunction loads the class named by a <function> element's class attribute via reflection and verifies it extends AbstractPartitionAlgorithm. If the class exists but does not inherit from AbstractPartitionAlgorithm, this IllegalArgumentException is thrown, aborting config load. MyCat requires all sharding functions to implement its algorithm contract.","triggerScenarios":"A rule.xml <function class=\"...\"> pointing at a class that implements a different interface (e.g. an older rule-function interface or a plain class); load() -> loadFunctions() -> createFunction() fails the isAssignableFrom check.","commonSituations":"Migrating configs across MyCat versions where the function API changed (old io.mycat.route.function classes vs custom code written against a different base); using a third-party algorithm jar built for another version; typo'd class name resolving to an unrelated class.","solutions":["Change the class attribute to a class that extends AbstractPartitionAlgorithm shipped with your MyCat version.","If it is custom code, make the class extend AbstractPartitionAlgorithm and implement calculate(String)/init() appropriately for your version.","Verify the algorithm jar version matches the MyCat server version; recompile against the correct API."],"exampleFix":"// before\npublic class MyModHash implements SomeOtherFunction { ... }\n// after\nimport io.mycat.route.function.AbstractPartitionAlgorithm;\npublic class MyModHash extends AbstractPartitionAlgorithm {\n  @Override public void init() { ... }\n  @Override public Integer calculate(String columnValue) { ... }\n}","handlingStrategy":"validation","validationCode":"// Pre-check every function class before load\nfor (String clazz : functionClasses) {\n    Class<?> c = Class.forName(clazz);\n    if (!AbstractPartitionAlgorithm.class.isAssignableFrom(c))\n        throw new IllegalStateException(clazz + \" does not extend AbstractPartitionAlgorithm\");\n}","typeGuard":"static boolean isValidShardingFunction(String clazz) {\n    try {\n        return AbstractPartitionAlgorithm.class.isAssignableFrom(Class.forName(clazz));\n    } catch (ClassNotFoundException e) {\n        return false;\n    }\n}","tryCatchPattern":"try {\n    ruleLoader.load();\n} catch (ConfigException | IllegalArgumentException e) {\n    if (String.valueOf(e.getMessage()).contains(\"must implements\")) {\n        LOG.error(\"Sharding function class incompatible: {}\", e.getMessage());\n    }\n    throw e;\n}","preventionTips":["Only reference AbstractPartitionAlgorithm subclasses shipped with your exact MyCat version.","Compile custom algorithms against the server's own dependency jar.","Unit-test custom functions by asserting they instantiate and calculate() correctly before deployment."],"tags":["reflection","class-not-found","sharding","incompatible-type"],"backgroundTag":"type-mismatch","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}