{"record":{"id":"72d6df04331daa90","repo":"apache/beam","slug":"failed-to-construct-instance-from-factory-method-s-s-s-due","errorCode":null,"errorMessage":"Failed to construct instance from factory method %s#%s(%s) due to access restriction","messagePattern":"Failed to construct instance from factory method (.+?)#(.+?)\\((.+?)\\) due to access restriction","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/util/InstanceBuilder.java","lineNumber":232,"sourceCode":"      return type.cast(method.invoke(null, args));\n\n    } catch (NoSuchMethodException e) {\n      throw new RuntimeException(\n          String.format(\n              \"Unable to find factory method %s#%s(%s)\",\n              factoryClass.getSimpleName(), methodName, Joiner.on(\", \").join(types)));\n    } catch (InvocationTargetException e) {\n      if (e.getTargetException() instanceof RuntimeException) {\n        // If underlying exception is unchecked re-raise it as-is\n        throw (RuntimeException) e.getTargetException();\n      }\n      throw new RuntimeException(\n          String.format(\n              \"Encountered checked exception when constructing an instance from factory method %s#%s(%s)\",\n              factoryClass.getSimpleName(), methodName, Joiner.on(\", \").join(types)),\n          e.getTargetException());\n    } catch (IllegalAccessException e) {\n      throw new RuntimeException(\n          String.format(\n              \"Failed to construct instance from factory method %s#%s(%s) due to access restriction\",\n              factoryClass.getSimpleName(), methodName, Joiner.on(\", \").join(types)),\n          e);\n    }\n  }\n\n  private T buildFromConstructor(Class<?>[] types) {\n    checkState(factoryClass != null);\n\n    try {\n      Constructor<?> constructor = factoryClass.getDeclaredConstructor(types);\n\n      checkState(\n          type.isAssignableFrom(factoryClass),\n          \"Instance type %s must be assignable to %s\",\n          factoryClass.getName(),\n          type.getSimpleName());","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/util/InstanceBuilder.java#L214-L250","documentation":"InstanceBuilder.buildFromMethod() catches IllegalAccessException and throws RuntimeException 'Failed to construct instance ... due to access restriction'. This occurs when Java access control prevents invoking the otherwise-matching static factory method.","triggerScenarios":"The factory method or its class is package-private/private or defined in a non-exported module/package, while the caller has insufficient access (e.g. JPMS strong encapsulation, reflection without setAccessible).","commonSituations":"Using --add-opens-less environments under Java 9+ module system; shading moved classes into packages where the method is no longer accessible; passing an inner class's private factory method.","solutions":["Make the factory method and its class public","Under JPMS, open/export the containing package or add --add-opens/--add-exports JVM flags","Avoid shaded/relocated classes in the builder configuration","Check the module-info/package-private modifiers of the factory class"],"exampleFix":"// before\nclass MyFactory { static MyThing of(String s) {...} }\n// after\npublic class MyFactory { public static MyThing of(String s) {...} }","handlingStrategy":"validation","validationCode":"int mods = factoryClass.getModifiers() | factoryMethod.getModifiers();\nif (!java.lang.reflect.Modifier.isPublic(mods)) { throw new IllegalStateException(\"factory method/class must be public\"); }","typeGuard":null,"tryCatchPattern":"try { instance = builder.build(); } catch (RuntimeException e) { if (e.getMessage().contains(\"access restriction\")) { /* fix visibility or JPMS exports */ } throw e; }","preventionTips":["Make factory classes and methods public","Add --add-exports/--add-opens when using strongly encapsulated modules","Avoid shading if reflective construction is required"],"tags":["java","reflection","access-control","jpms"],"backgroundTag":"permission-denied","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}