{"record":{"id":"965713bf95f07152","repo":"mybatis/mybatis-3","slug":"failed-to-invoke-constructor-for-handler","errorCode":null,"errorMessage":"Failed to invoke constructor for handler {}","messagePattern":"Failed to invoke constructor for handler (.+?)","errorType":"exception","errorClass":"TypeException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java","lineNumber":534,"sourceCode":"          return (TypeHandler<T>) c.newInstance(javaType);\n        } catch (NoSuchMethodException ignored) {\n        }\n        if (javaType instanceof Class) {\n          try {\n            c = handlerClass.getConstructor(Class.class);\n            return (TypeHandler<T>) c.newInstance(javaType);\n          } catch (NoSuchMethodException ignored) {\n          }\n        }\n      }\n      try {\n        c = handlerClass.getConstructor();\n        return (TypeHandler<T>) c.newInstance();\n      } catch (NoSuchMethodException e) {\n        throw new TypeException(\"Unable to find a usable constructor for \" + handlerClass, e);\n      }\n    } catch (ReflectiveOperationException e) {\n      throw new TypeException(\"Failed to invoke constructor for handler \" + handlerClass, e);\n    }\n  }\n\n  // scan\n\n  public void register(String packageName) {\n    ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();\n    resolverUtil.find(new ResolverUtil.IsA(TypeHandler.class), packageName);\n    Set<Class<? extends Class<?>>> handlerSet = resolverUtil.getClasses();\n    for (Class<?> type : handlerSet) {\n      // Ignore inner classes and interfaces (including package-info.java) and abstract classes\n      if (!type.isAnonymousClass() && !type.isInterface() && !Modifier.isAbstract(type.getModifiers())) {\n        register(type);\n      }\n    }\n  }\n\n  // get information","sourceCodeStart":516,"sourceCodeEnd":552,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java#L516-L552","documentation":"The companion of error 257: a usable constructor was FOUND but invoking it failed with a ReflectiveOperationException other than NoSuchMethodException — typically an exception thrown inside the no-arg constructor, or instantiation of an abstract/classloader-hidden class. The handler class name is included in the message.","triggerScenarios":"A no-arg constructor that performs initialization (loading config, opening resources) and throws; handler class loaded by a different classloader triggering IllegalAccess; accidentally referencing an abstract handler class.","commonSituations":"Handlers that read properties/registries in their constructor; fat-jar or container classloader setups; constructor logic that depends on environment (files, env vars) absent in the current deployment.","solutions":["Check the Cause — if it is your own exception, move that initialization out of the constructor or make it safe","Ensure the handler class is public, concrete, and visible to MyBatis' classloader","Initialize expensive state lazily (on first set/get) instead of in the constructor"],"exampleFix":"// before\npublic MyHandler() { this.cfg = Files.readString(Path.of(\"/etc/app.cfg\")); } // throws in prod\n\n// after\npublic MyHandler() { }\nprivate Config cfg() { return ConfigHolder.INSTANCE; } // lazy","handlingStrategy":"try-catch","validationCode":"try {\n  Object h = handlerClass.getDeclaredConstructor().newInstance(); // dry-run at registration time\n} catch (ReflectiveOperationException e) {\n  throw new IllegalStateException(\"Handler constructor failed: \" + handlerClass, e);\n}","typeGuard":null,"tryCatchPattern":"try { registry.getInstance(type, handlerClass); } catch (TypeException e) { Throwable c = e.getCause(); // constructor body failed: inspect c and fix initialization } ","preventionTips":["Keep constructors trivial; defer resource loading to first use","Fail fast at boot by eagerly instantiating all registered handlers"],"tags":["mybatis","type-handler","constructor","initialization"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}