{"record":{"id":"45902acdb593757e","repo":"apple/pkl","slug":"error-accessing-constructor-of-class-s","errorCode":null,"errorMessage":"Error accessing constructor of class `%s`.","messagePattern":"Error accessing constructor of class `(.+?)`\\.","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"pkl-config-java/src/main/java/org/pkl/config/java/mapper/PCollectionToCollection.java","lineNumber":99,"sourceCode":"            // default constructor\n            var ctor0 = lookup.findConstructor(clazz, MethodType.methodType(void.class));\n            return Optional.of(\n                length -> {\n                  try {\n                    //noinspection unchecked\n                    return (Collection<T>) ctor0.invoke();\n                  } catch (Throwable t) {\n                    throw new ConversionException(\n                        String.format(\"Error invoking constructor of class `%s`.\", clazz), t);\n                  }\n                });\n          } catch (NoSuchMethodException e0) {\n            return Optional.empty();\n          }\n        }\n      }\n    } catch (IllegalAccessException e) {\n      throw new ConversionException(\n          String.format(\"Error accessing constructor of class `%s`.\", clazz), e);\n    }\n  }\n\n  private static class ConverterImpl<T> implements Converter<Collection<Object>, Collection<T>> {\n    private final Function<Integer, Collection<T>> targetInstantiator;\n    private final Type targetElementType;\n\n    private PClassInfo<Object> cachedElementType = PClassInfo.Unavailable;\n    private @Nullable Converter<Object, T> cachedConverter;\n\n    private ConverterImpl(\n        Function<Integer, Collection<T>> targetInstantiator, Type targetElementType) {\n      this.targetInstantiator = targetInstantiator;\n      this.targetElementType = targetElementType;\n    }\n\n    @Override","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-config-java/src/main/java/org/pkl/config/java/mapper/PCollectionToCollection.java#L81-L117","documentation":"Thrown by PCollectionToCollection.createInstantiator when the reflective lookup machinery itself fails: `clazz.getConstructors()` (or equivalent) threw IllegalAccessException, meaning the class or its constructors are not accessible to the caller (non-public class, restrictive module/JPMS access, or a security manager). Wrapped in a ConversionException with the target class name.","triggerScenarios":"Mapping a Pkl collection to a package-private or non-public collection class, or to a class in a closed Java module that does not export/opens its package to the pkl-config-java module, so `getConstructors()` cannot expose them.","commonSituations":"Custom collection classes declared package-private; applications using JPMS (module-info.java) without `opens`/`exports` for the package containing the target class; security-manager-restricted environments.","solutions":["Make the target collection class and its constructors public.","With JPMS, add `opens <package>` (or `exports <package>`) for the target class's package in module-info.java, or run with `--add-opens <module>/<package>=ALL-UNNAMED`.","Map to a standard public collection type (ArrayList, HashSet, HashMap) instead of the restricted class.","Register a custom Converter for the type to bypass reflective constructor access entirely."],"exampleFix":"// before (module-info.java)\nmodule app { }\n// after (module-info.java)\nmodule app { opens com.example.collections; }","handlingStrategy":"try-catch","validationCode":"try { targetClass.getConstructors(); } catch (SecurityException | java.lang.reflect.InaccessibleObjectException e) { throw new IllegalStateException(\"Target collection class is not accessible to pkl-config-java: \" + targetClass); }","typeGuard":"static boolean isReflectivelyAccessible(Class<?> c) { return java.lang.reflect.Modifier.isPublic(c.getModifiers()); }","tryCatchPattern":"try { MyConfig cfg = mapper.map(module, MyConfig.class); } catch (ConversionException e) { throw new IllegalStateException(\"Make \" + e.getMessage() + \" public and JPMS-accessible (add 'opens' in module-info.java)\", e.getCause()); }","preventionTips":["Declare mapping-target collection classes public","Add opens/exports directives for target packages when using JPMS","Avoid mapping to package-private or module-encapsulated classes","Fall back to standard public JDK collections when access cannot be granted"],"tags":["pkl","java","reflection","access","jpms"],"backgroundTag":"reflective-constructor-access-failed","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}