{"record":{"id":"4d8a567197c985c4","repo":"mybatis/mybatis-3","slug":"unable-to-find-a-usable-constructor-for","errorCode":null,"errorMessage":"Unable to find a usable constructor for {}","messagePattern":"Unable to find a usable constructor for (.+?)","errorType":"exception","errorClass":"TypeException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java","lineNumber":531,"sourceCode":"      if (javaType != null) {\n        try {\n          c = handlerClass.getConstructor(Type.class);\n          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    }","sourceCodeStart":513,"sourceCodeEnd":549,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java#L513-L549","documentation":"TypeHandlerRegistry.getInstance looks for a public constructor taking java.lang.reflect.Type (or Class) and, failing that, a public no-arg constructor. If the handler class has neither, the NoSuchMethodException is wrapped in this TypeException: the handler cannot be instantiated for a mapped type.","triggerScenarios":"A custom handler whose only constructors take other arguments (e.g. MyHandler(String)) or are private; a handler with only a (Class) constructor that is not public; abstract handlers accidentally referenced from a mapper.","commonSituations":"DI-oriented handler classes designed for container injection (no usable plain constructor); inner non-static handler classes; mapper XML referencing an abstract base handler instead of the concrete subclass.","solutions":["Add a public no-arg constructor to the handler class","Or add a public constructor accepting a single Class/Type argument","Reference the concrete (non-abstract, static) handler class from the mapper/configuration"],"exampleFix":"// before\npublic class MyHandler extends BaseTypeHandler<Json> {\n  private MyHandler(String cfg) { ... }\n}\n\n// after\npublic class MyHandler extends BaseTypeHandler<Json> {\n  public MyHandler() { this(\"default\"); }\n  private MyHandler(String cfg) { ... }\n}","handlingStrategy":"validation","validationCode":"boolean hasUsableCtor = Arrays.stream(handlerClass.getConstructors()).anyMatch(\n  c -> c.getParameterCount() == 0\n   || (c.getParameterCount() == 1 && (Type.class.equals(c.getParameterTypes()[0]) || Class.class.equals(c.getParameterTypes()[0]))));\nif (!hasUsableCtor) throw new IllegalStateException(handlerClass + \" needs a public () or (Class) constructor\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Standardize custom handlers on: public no-arg ctor plus optional public (Class) ctor","Avoid constructor injection in handlers; inject configuration lazily"],"tags":["mybatis","type-handler","constructor","reflection"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}