apple/pkl · error · JavaCodeGeneratorException

Pkl function types are not supported by the Java code…

Error message

Pkl function types are not supported by the Java code generator.

What it means

JavaCodeGenerator.toJavaPoetName throws JavaCodeGeneratorException when the type being mapped resolves to a Pkl function type: javaPoet has no representation for function types in generated classes, so codegen refuses the input schema rather than emitting invalid Java.

Solutions

  1. Replace the function type in the Pkl schema with a concrete class or type alias that maps to a Java type.
  2. Exclude the offending property/type from code generation and hand-write the mapping.

Example fix

// before (Pkl)
transform: (String) -> String
// after
class Transform { mode: String }
Defensive patterns

Strategy: validation

Validate before calling

fun hasFunctionType(module: Module) = module.declarations.any { it.type is PType.Function }

Type guard

fun PType.isFunctionType() = this is PType.Function

Try / catch

try { generate() } catch (e: JavaCodeGeneratorException) { if ("function types" in e.message!!) stripFunctionalMembers() else throw e }

Prevention

When it happens

Trigger: A Pkl module declares a property, parameter, or type alias whose type is a function type, and that module is fed to the Java code generator.

Common situations: Modules exposing callback-style properties or functional parameters, or typealiases like `type Handler = (String) -> Boolean` appearing in a schema meant for codegen.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at pkl-codegen-java/src/main/kotlin/org/pkl/codegen/java/JavaCodeGenerator.kt:887

          else -> {
            if (CodeGeneratorUtils.isRepresentableAsEnum(aliasedType, null)) {
              if (typeAlias.isStandardLibraryMember) {
                throw JavaCodeGeneratorException(
                  "Standard library typealias `${typeAlias.qualifiedName}` is not supported by Java code generator. " +
                    "If you think this is an omission, please let us know."
                )
              } else {
                // reference generated enum class
                typeAlias.toJavaPoetName().nullableIf(nullable)
              }
            } else {
              // inline type alias
              aliasedType.toJavaPoetName(nullable)
            }
          }
        }
      is PType.Function ->
        throw JavaCodeGeneratorException(
          "Pkl function types are not supported by the Java code generator."
        )
      is PType.Union ->
        if (CodeGeneratorUtils.isRepresentableAsString(this)) STRING.nullableIf(nullable)
        else
          throw JavaCodeGeneratorException(
            "Pkl union types are not supported by the Java code generator."
          )
      else ->
        // should never encounter PType.TypeVariableNode because it can only occur in stdlib classes
        throw AssertionError("Encountered unexpected PType subclass: $this")
    }

  private fun TypeName.nullableIf(isNullable: Boolean): TypeName =
    if (isPrimitive && isNullable) box()
    else if (isPrimitive || isNullable) this else annotated(nonNullAnnotation)

  private fun TypeName.boxIf(shouldBox: Boolean): TypeName = if (shouldBox) box() else this

View on GitHub (pinned to f3efcbfc9b)