apple/pkl · error · ConversionException

Error invoking constructor of class `%s`.

Error message

Error invoking constructor of class `%s`.

What it means

Thrown by PCollectionToCollection.createInstantiator when a Pkl collection (List/Set/Map) is mapped to a Java Collection type. The mapper reflectively invokes the target collection class's two-arg constructor `(int initialCapacity, float loadFactor)` (typical for HashMap/HashSet); if the reflective `ctor2.invoke(...)` call throws any Throwable (instantiation failure, IllegalArgumentException from bad capacity, constructor throwing internally), it is wrapped in this ConversionException.

Source

Thrown at pkl-config-java/src/main/java/org/pkl/config/java/mapper/PCollectionToCollection.java:61

    return createInstantiator(targetClass)
        .map(instantiator -> new ConverterImpl<>(instantiator, elementType));
  }

  private <T> Optional<Function<Integer, Collection<T>>> createInstantiator(Class<T> clazz) {
    try {
      try {
        // constructor with capacity and load factor parameters, e.g. HashSet
        var ctor2 =
            lookup.findConstructor(
                clazz, MethodType.methodType(void.class, int.class, float.class));
        return Optional.of(
            length -> {
              try {
                //noinspection unchecked
                return (Collection<T>) ctor2.invoke((int) (length / .75f) + 1, .75f);
              } catch (Throwable t) {
                throw new ConversionException(
                    String.format("Error invoking constructor of class `%s`.", clazz), t);
              }
            });
      } catch (NoSuchMethodException e2) {
        try {
          // constructor with size parameter, e.g. ArrayList
          var ctor1 = lookup.findConstructor(clazz, MethodType.methodType(void.class, int.class));
          return Optional.of(
              length -> {
                try {
                  //noinspection unchecked
                  return (Collection<T>) ctor1.invoke(length);
                } catch (Throwable t) {
                  throw new ConversionException(
                      String.format("Error invoking constructor of class `%s`.", clazz), t);
                }
              });
        } catch (NoSuchMethodException e1) {

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Inspect the wrapped cause (`getCause()`) — the real failure is inside the target class's (int, float) constructor; fix that constructor or its preconditions.
  2. Map to a standard collection type (java.util.ArrayList/HashMap/LinkedHashSet) instead of the custom class.
  3. If a custom class is required, provide an accessible no-arg or `(int)` constructor that does not throw for the computed initial capacity.
  4. Register a custom Converter for that target type instead of relying on reflective instantiation.

Example fix

// before
public MySet(int cap, float lf) { if (cap < 10) throw new IllegalArgumentException("cap too small"); ... }
// after
public MySet(int cap, float lf) { super(Math.max(16, cap), lf); }
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure the target class has a working (int, float) constructor:
Class<?> c = targetClass; c.getConstructor(int.class, float.class).newInstance(16, 0.75f);

Try / catch

try { MyConfig cfg = mapper.map(module, MyConfig.class); } catch (ConversionException e) { Throwable real = e.getCause(); log.error("Constructor of {} failed: {}", e.getMessage(), real, real); }

Prevention

When it happens

Trigger: Mapping a Pkl collection to a custom Collection/Map class whose `(int, float)` constructor throws — e.g. a constructor that validates capacity and rejects the computed value `(int)(length / .75f) + 1`, or an abstract/uninstantiable class reached via a custom mapping target.

Common situations: Custom collection implementations with guarded constructors (negative/zero-capacity checks failing for empty Pkl collections); classes whose constructors have side effects (required config, DB connections) that fail; third-party collection libs with strict invariants.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/aa4d218ff1b59431. Report an issue: GitHub.