[PR #710] [MERGED] codegen-java: Change condition for generating equals/hashCode/toString/with/Serializable #687

Closed
opened 2025-12-30 01:26:10 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/apple/pkl/pull/710
Author: @odenix
Created: 10/19/2024
Status: Merged
Merged: 10/24/2024
Merged by: @bioball

Base: mainHead: codegen-java


📝 Commits (1)

  • f970f66 codegen-java: Change condition for generating equals/hashCode/toString/with/Serializable

📊 Changes

3 files changed (+958 additions, -814 deletions)

View changed files

📝 pkl-codegen-java/src/main/kotlin/org/pkl/codegen/java/JavaCodeGenerator.kt (+10 -8)
📝 pkl-codegen-java/src/test/kotlin/org/pkl/codegen/java/JavaCodeGeneratorTest.kt (+936 -794)
📝 pkl-codegen-java/src/test/resources/org/pkl/codegen/java/Inheritance.jva (+12 -12)

📄 Description

Motivation:

  • Currently, the condition for generating equals/hashCode/toString methods is "generated class declares properties". As a result, classes that don't declare properties inherit these methods from their generated superclasses. However, generated equals and toString methods aren't designed to be inherited and will either fail or produce wrong results when called for a subclass.
  • Currently, the condition for generating with methods is "class is not abstract". However, it isn't useful to generate with methods for non-instantiable non-abstract classes.
  • Currently, the condition for making classes serializable is "class is not a module class". However, it isn't useful to make non-instantiable non-module classes serializable, and it is useful to make instantiable module classes serializable.

Changes:
Change the condition for generating equals/hashCode/toString/with/Serializable to "class is instantiable". This is a breaking change.
(A generated class is instantiable, i.e., declares a public constructor, if it is neither abstract nor stateless. This behavior remains unchanged.)

Result:

  • Fixes all motivating issues.
  • Fixes #706.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/apple/pkl/pull/710 **Author:** [@odenix](https://github.com/odenix) **Created:** 10/19/2024 **Status:** ✅ Merged **Merged:** 10/24/2024 **Merged by:** [@bioball](https://github.com/bioball) **Base:** `main` ← **Head:** `codegen-java` --- ### 📝 Commits (1) - [`f970f66`](https://github.com/apple/pkl/commit/f970f669b763f779bd7eef46095c376e1b042a27) codegen-java: Change condition for generating equals/hashCode/toString/with/Serializable ### 📊 Changes **3 files changed** (+958 additions, -814 deletions) <details> <summary>View changed files</summary> 📝 `pkl-codegen-java/src/main/kotlin/org/pkl/codegen/java/JavaCodeGenerator.kt` (+10 -8) 📝 `pkl-codegen-java/src/test/kotlin/org/pkl/codegen/java/JavaCodeGeneratorTest.kt` (+936 -794) 📝 `pkl-codegen-java/src/test/resources/org/pkl/codegen/java/Inheritance.jva` (+12 -12) </details> ### 📄 Description Motivation: * Currently, the condition for generating `equals/hashCode/toString` methods is "generated class declares properties". As a result, classes that don't declare properties inherit these methods from their generated superclasses. However, generated `equals` and `toString` methods aren't designed to be inherited and will either fail or produce wrong results when called for a subclass. * Currently, the condition for generating `with` methods is "class is not abstract". However, it isn't useful to generate `with` methods for non-instantiable non-abstract classes. * Currently, the condition for making classes serializable is "class is not a module class". However, it isn't useful to make non-instantiable non-module classes serializable, and it is useful to make instantiable module classes serializable. Changes: Change the condition for generating `equals/hashCode/toString/with/Serializable` to "class is instantiable". This is a breaking change. (A generated class is instantiable, i.e., declares a public constructor, if it is neither abstract nor stateless. This behavior remains unchanged.) Result: * Fixes all motivating issues. * Fixes #706. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-30 01:26:10 +01:00
adam closed this issue 2025-12-30 01:26:10 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#687