rspec/rules/S6211/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

86 lines
2.5 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

== Why is this an issue?
Before records appeared in Java 16, there was a common way to represent getters for private fields of a class:  a method named "get" with a capitalized field name. For example, for a ``++String++`` field named "myField" the signature of the getter method will be: ``++public String getMyField()++``
In records, getters are named differently. Getters created by default do not contain the "get" prefix. So for a record's ``++String++`` field "myField" the getter method will be: ``++public String myField()++``
This means that if you want to override the default getter behavior it is better to use the method provided by records instead of creating a new one. Otherwise, this will bring confusion to the users of the record as two getters will be available and even leads to bugs if the behavior is different from the default one.
This rule raises an issue when a record contains a getter named "get" with a capitalized field name that is not behaving the same as the default one.
=== Noncompliant code example
[source,java]
----
record Person(String name, int age) {
public String getName() { // Noncompliant
return name.toUpperCase(Locale.ROOT);
}
}
----
=== Compliant solution
[source,java]
----
record Person(String name, int age) {
@Override
public String name() { // Compliant
return name.toUpperCase(Locale.ROOT);
}
}
record Person(String name, int age) {
public String getNameUpperCase() { // Compliant
return name.toUpperCase(Locale.ROOT);
}
}
record Person(String name, int age) {
public String getName() { // Compliant, is equivalent to 'name()'
return name;
}
}
record Person(String name, int age) {
@Override
public String name() { // Compliant
return name.toUpperCase(Locale.ROOT);
}
public String getName() { // Compliant, equal to 'name()'
return name.toUpperCase(Locale.ROOT);
}
}
----
=== Exceptions
If the implementations of ``++getMyField()++`` and ``++myField()++`` methods are equivalent, the issue should not be raised as this was probably done to support compatibility with the previous convention.
== Resources
* https://docs.oracle.com/javase/specs/jls/se16/html/jls-8.html#jls-8.10[Records specification]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
remove this getter 'getXxx()' from record and override an existing one 'xxx()'
=== Highlighting
method declaration
endif::env-github,rspecator-view[]