{"record":{"id":"d78a2106b741965f","repo":"apache/iceberg","slug":"cannot-bind-constructors","errorCode":null,"errorMessage":"Cannot bind constructors","messagePattern":"Cannot bind constructors","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"common/src/main/java/org/apache/iceberg/common/DynConstructors.java","lineNumber":104,"sourceCode":"    @Override\n    @SuppressWarnings(\"unchecked\")\n    public <R> R invoke(Object target, Object... args) {\n      Preconditions.checkArgument(\n          target == null, \"Invalid call to constructor: target must be null\");\n      return (R) newInstance(args);\n    }\n\n    @Override\n    @SuppressWarnings(\"unchecked\")\n    <R> R invokeChecked(Object target, Object... args) throws Exception {\n      Preconditions.checkArgument(\n          target == null, \"Invalid call to constructor: target must be null\");\n      return (R) newInstanceChecked(args);\n    }\n\n    @Override\n    public DynMethods.BoundMethod bind(Object receiver) {\n      throw new IllegalStateException(\"Cannot bind constructors\");\n    }\n\n    @Override\n    public boolean isStatic() {\n      return true;\n    }\n\n    @Override\n    public String toString() {\n      return getClass().getSimpleName() + \"(constructor=\" + ctor + \", class=\" + constructed + \")\";\n    }\n  }\n\n  /**\n   * Constructs a new builder for finding a constructor dynamically.\n   *\n   * @return a Builder for finding a constructor\n   */","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/common/src/main/java/org/apache/iceberg/common/DynConstructors.java#L86-L122","documentation":"A constructor handle produced by DynConstructors cannot be bound to a receiver instance — constructors are invoked with their arguments directly and are conceptually static. Calling bind(receiver) on such a handle throws this IllegalStateException by design.","triggerScenarios":"Treating a DynConstructors handle like a DynMethods handle: holding a CtorHandle (as a DynMethods.UnboundMethod) and calling bind(obj) to attach a receiver instead of calling newInstance(args).","commonSituations":"Generic reflective-dispatch code written against DynMethods that was handed a constructor handle; mixing constructor and method lookups in one code path.","solutions":["Don't call bind() on constructor handles; invoke with newInstance(...) or newInstanceChecked(...) passing constructor arguments.","Separate constructor lookups from method lookups so bind() is only called on genuine method handles.","Check isStatic() before binding — constructor handles report isStatic() == true and cannot bind a receiver.","If a receiver-ful call is intended, look up an instance method via DynMethods instead of DynConstructors."],"exampleFix":"// before\nDynMethods.UnboundMethod ctor = DynConstructors.builder().impl(Foo.class).build();\nFoo f = ctor.bind(receiver).invoke();\n// after\nFoo f = DynConstructors.builder().impl(Foo.class).build().newInstance();","handlingStrategy":"type-guard","validationCode":"DynMethods.UnboundMethod handle = /* looked up */;\nif (handle.isStatic()) {\n  // constructor or static handle: never call bind(receiver)\n}","typeGuard":"boolean canBindReceiver(DynMethods.UnboundMethod m) { return !m.isStatic(); }","tryCatchPattern":"try {\n  bound = handle.bind(receiver);\n} catch (IllegalStateException e) {\n  if (e.getMessage().equals(\"Cannot bind constructors\")) {\n    throw new IllegalStateException(\"Use newInstance(args) for constructor handles, not bind()\", e);\n  }\n  throw e;\n}","preventionTips":["Never call bind() on handles obtained from DynConstructors — use newInstance/newInstanceChecked","Keep constructor and method reflection in separate code paths","Gate bind() calls behind isStatic() checks","Write a small unit test exercising the reflective invocation path before shipping"],"tags":["reflection","constructor","api-misuse"],"backgroundTag":"unsupported-operation","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}