Lombok-generated consecutive locals merged into one declaration (sameLvc)

Đang mở Phù hợp với người mới
#234 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

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

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
76/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
java
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu tại ScanCompilationUnit.visitVariable và kiểm tra sameLvc, hiện đang so sánh vị trí bắt đầu và dòng trong mã nguồn. Kiểm tra cách LocalVariableCreation cung cấp kiểu đã khai báo và các modifier, rồi so sánh chúng với đề xuất trong issue. Hoàn thành khi các biến cục bộ do Lombok tạo ra có kiểu hoặc modifier khác nhau được tách riêng, đồng thời vẫn giữ các câu lệnh nhiều khai báo hợp lệ. Payload không nêu tệp kiểm thử, vì vậy hãy tìm các kiểm thử parsing hoặc printing phù hợp.

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

Mô tả

With Lombok on, consecutive local variables in a Lombok-generated method get merged into one multi-declarator statement that takes the first variable's type and modifiers. The printed source no longer compiles.

import lombok.Data;
import java.math.BigDecimal;

@Data
public class LoanFeeData {
    private final BigDecimal feeChargesCharged;
    private final BigDecimal feeAdjustments;
}

Lombok generates

public int hashCode() {
    final int PRIME = 59;
    int result = 1;
    final Object $feeChargesCharged = this.getFeeChargesCharged();
    result = result * PRIME + ($feeChargesCharged == null ? 43 : $feeChargesCharged.hashCode());
    ...

and printing the parsed type gives

public int hashCode() {
    final int PRIME = 59, result = 1, $feeChargesCharged = getFeeChargesCharged();
    result = result * PRIME + ($feeChargesCharged == null ? 43 : $feeChargesCharged.hashCode());
    ...

$feeChargesCharged is now an int, so re-parsing the printed source fails with Unresolved method call 'hashCode'. result is also final now. In Apache Fineract's portfolio.loanaccount packages, this affects every @Data / @EqualsAndHashCode class whose hashCode covers a field (27 files).

Cause. In ScanCompilationUnit.visitVariable, a local variable is merged into the previous LocalVariableCreation when sameLvc holds, and sameLvc compares only the begin position and begin line:

private boolean sameLvc(LocalVariableCreation lvc1, LocalVariableCreation lvc2) {
    Source s1 = lvc1.source(); Source s2 = lvc2.source();
    return s1.beginPos() == s2.beginPos() && s1.beginLine() == s2.beginLine();
}

That's right for int a = 1, b = 2;. But every statement Lombok generates carries its annotation's position, so all consecutive generated locals look like one declaration.

Proposed fix. Also require the same declared type and the same modifiers. javac gives a real multi-declarator's variables the same type and modifiers, so nothing changes for hand-written code. Two generated locals with the same type and modifiers may still be merged, but the result is valid Java.

Ngôn ngữ chính
Java
Star
1
Fork
1
Merge trung bình
48 phút
Pull request đã merge (30 ngày)
2

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 CodeLaser/maddi

Tất cả issue của CodeLaser/maddi

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.