github / github/codeql

Java: Record components (and their annotations) are not extracted

Đang mở
#7,727 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Java
Ngôn ngữ chính
CodeQL
Star
10.1k
Fork
2.1k
Merge trung bình
2 ngày 15 giờ
Pull request đã merge (30 ngày)
141

Mô tả

The components of a Record class are currently not modeled by CodeQL, and their annotations are not extracted. Therefore it is not possible to test for annotations which only use `ElementType.RECORD_COMPONENT`, or for annotations with `ElementType.METHOD` which only exist in source but not in the class file because the corresponding accessor method is overridden.

The backing private field as well as the implicit accessor method are extracted.

Example:
```java
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

record RecordTest(
@RecordComponentAnnotation
int i
) {
@Retention(RUNTIME)
@Target(ElementType.RECORD_COMPONENT)
@interface RecordComponentAnnotation { }
}
```

The following query does not find any usage of `@RecordComponentAnnotation`, and does not have any element modeling the Record component as result:
```ql
import java

from Top t
where t.getLocation().getFile().getBaseName().matches("RecordTest%")
select t, t.getLocation().getStartLine(), t.getPrimaryQlClasses()
```

Interestingly CodeQL reports a `FieldDeclaration` at the location of the component.
This issue might be solvable by modeling the component using the internal field (pretending the field is also the component), but this can cause inaccuracies for annotations, e.g.:
- In the byte code (and accessible through reflection) an annotation with `ElementType.RECORD_COMPONENT` is only present on the component, and `ElementType.FIELD` is only present on the internal field. With this approach both would be present on the field.
- It would not be possible to model an annotation with `ElementType.METHOD` on the component, or it would at least be weird that a `Field` has a method annotation

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

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.