Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Class To Record

Đang mở
#391 2 bình luận 3 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
java
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu với Java 17 Records specification được liên kết trong issue và kiểm tra Lombok value-to-record implementation được tham chiếu qua issue #141. Xác định các điều kiện tiên quyết của class được hỗ trợ và các phép biến đổi bị ảnh hưởng trước khi triển khai chúng; phần hoàn thành phải bao gồm việc tái cấu trúc các class đủ điều kiện, cập nhật các lời gọi getter và loại bỏ các phương thức canonical khi áp dụng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

recipe

What problem are you trying to solve?

It would be neat to have a capability to convert a class to a record

What precondition(s) should be checked before applying this recipe?

  • Java 17+
  • Should probably have an arg to specify the type to transform (versus rampantly changing every eligible class to a record)
  • Check for things that would disqualify the class from being a record, such as:
    • type must not participate in inheritance
    • type must be immutable
      • type must not have setters (or maybe make an extra-fancy version of this recipe which can replaces usages of setters with a constructor call) (and/or maybe include a force-override option for this no-setters rule)
      • non-static fields must be final or effectively final
    • any custom constructor(s) must delegate to another constructor via this(...) in the first line

Describe the situation before applying the recipe

See Java's specification for Records for context:
https://docs.oracle.com/en/java/javase/17/language/records.html#GUID-6699E26F-4A9B-4393-A08B-1E47D4B2D263

Describe the situation after applying the recipe

  • The targeted class should be restructured as a record
  • Method invocations of the type's getters should be replaced with record property accessors (eg getColor() to color())
  • (bonus points) "canonical" equals/hashCode/toString implementations are removed in favor of the default

Have you considered any alternatives or workarounds?

Any additional context

The implementation for Lombok value to record likely has some useful code to reference/reuse.

This feels like a problem with many weird edge cases (even beyond what's written above), but even a naive implementation which only handles some of those scenarios could probably still be valuable, and the recipe could grow iteratively from there.

Are you interested in contributing this recipe to OpenRewrite?

Maybe; priority TBD

Ngôn ngữ chính
Java
Star
156
Fork
129
Merge trung bình
7 giờ 21 phút
Pull request đã merge (30 ngày)
42

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của openrewrite/rewrite-migrate-java

Tất cả issue của openrewrite/rewrite-migrate-java

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.