{"record":{"id":"76333b8d15df77be","repo":"apple/pkl","slug":"pkl-this-types-are-not-supported-by-the-java","errorCode":null,"errorMessage":"Pkl `${this}` types are not supported by the Java code generator.","messagePattern":"Pkl `(.+?)` types are not supported by the Java code generator\\.","errorType":"exception","errorClass":"JavaCodeGeneratorException","httpStatus":null,"severity":"error","filePath":"pkl-codegen-java/src/main/kotlin/org/pkl/codegen/java/JavaCodeGenerator.kt","lineNumber":757,"sourceCode":"    val baseName = toJavaPoetName(boxed = true)\n    return if (this is PType.Class && (pClass.isAbstract || pClass.isOpen)) {\n      WildcardTypeName.subtypeOf(baseName)\n    } else {\n      baseName\n    }\n  }\n\n  private val PType.isBytesClass: Boolean\n    get() = this is PType.Class && this.pClass.info == PClassInfo.Bytes\n\n  private fun PType.toJavaPoetName(nullable: Boolean = false, boxed: Boolean = false): TypeName =\n    when (this) {\n      PType.UNKNOWN -> OBJECT.nullableIf(nullable)\n      PType.NOTHING -> TypeName.VOID\n      PType.MODULE,\n      PType.THIS ->\n        // TODO: support self types: `class Foo<T extends Foo<T>>`\n        throw JavaCodeGeneratorException(\n          \"Pkl `${this}` types are not supported by the Java code generator.\"\n        )\n      is PType.StringLiteral -> STRING.nullableIf(nullable)\n      is PType.Class -> {\n        // if in doubt, spell it out\n        when (val classInfo = pClass.info) {\n          PClassInfo.Any -> OBJECT\n          PClassInfo.Typed,\n          PClassInfo.Dynamic -> OBJECT.nullableIf(nullable)\n          PClassInfo.Boolean -> TypeName.BOOLEAN.boxIf(boxed).nullableIf(nullable)\n          PClassInfo.String -> STRING.nullableIf(nullable)\n          // seems more useful to generate `double` than `java.lang.Number`\n          PClassInfo.Number -> TypeName.DOUBLE.boxIf(boxed).nullableIf(nullable)\n          PClassInfo.Int -> TypeName.LONG.boxIf(boxed).nullableIf(nullable)\n          PClassInfo.Float -> TypeName.DOUBLE.boxIf(boxed).nullableIf(nullable)\n          PClassInfo.Duration -> DURATION.nullableIf(nullable)\n          PClassInfo.DataSize -> DATA_SIZE.nullableIf(nullable)\n          PClassInfo.Bytes -> ArrayTypeName.of(TypeName.BYTE)","sourceCodeStart":739,"sourceCodeEnd":775,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-codegen-java/src/main/kotlin/org/pkl/codegen/java/JavaCodeGenerator.kt#L739-L775","documentation":"toJavaPoetName maps Pkl types to JavaPoet TypeNames; PType.MODULE and PType.THIS (self types) have no Java representation, so generation throws. Supporting self types would require recursive generics like `class Foo<T extends Foo<T>>`, which is not implemented (see TODO).","triggerScenarios":"Generating Java for a schema whose property/declared type is the module type or `this` — e.g. a property typed as the enclosing module name or literal `this` in a Pkl module.","commonSituations":"Modules with self-referential builder/fluent APIs, properties typed as the module itself, or `this`-typed function returns in Pkl definitions.","solutions":["Refactor the Pkl module to avoid MODULE/THIS-typed properties; use a concrete class type instead.","Replace `this`-typed declarations with the explicit class name.","Split self-referential parts into a separate non-generated API surface.","Request/await upstream support for self types (there is an explicit TODO)."],"exampleFix":"// before (Pkl)\nprop: this\n// after\nprop: ModuleClass","handlingStrategy":"validation","validationCode":"fun usesSelfType(module: Module): Boolean = module.properties.any { it.type in setOf(PType.MODULE, PType.THIS) }","typeGuard":"fun PType.isSelfType() = this == PType.MODULE || this == PType.THIS","tryCatchPattern":"try { generate() } catch (e: JavaCodeGeneratorException) { if (\"types are not supported\" in e.message!!) refactorSelfTypes() else throw e }","preventionTips":["Avoid `this` and module-typed properties in generated modules","Use concrete class types for self-referential members","Track upstream support for self types before adopting the pattern"],"tags":["codegen","types","unsupported"],"backgroundTag":"unsupported-operation","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}