24 lines
880 B
Plaintext
24 lines
880 B
Plaintext
In general, altering or bypassing the accessibility of classes, methods, or fields violates the encapsulation principle and could lead to run-time errors. For records the case is even trickier: you cannot change the visibility of records's fields and trying to update the existing value will lead to ``++IllegalAccessException++`` in runtime.
|
|
|
|
|
|
This rule raises an issue when reflection is used to change the visibility of a record's field, and when it is used to directly update a record's field value.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
record Person(String name, int age) {}
|
|
|
|
Person person = new Person("A", 26);
|
|
Field field = Person.class.getDeclaredField("name");
|
|
field.setAccessible(true); // secondary
|
|
field.set(person, "B"); // Noncompliant
|
|
----
|
|
|
|
|
|
== See
|
|
|
|
* https://docs.oracle.com/javase/specs/jls/se15/preview/specs/records-jls.html#jls-8.10[Records specification]
|
|
|
|
|