2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2023-07-14 14:17:45 +02:00
|
|
|
Two classes can have the same simple name if they are in two different packages.
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-07-14 14:17:45 +02:00
|
|
|
[source,java]
|
|
|
|
----
|
|
|
|
package org.foo.domain;
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-07-14 14:17:45 +02:00
|
|
|
public class User {
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
----
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
2023-07-14 14:17:45 +02:00
|
|
|
package org.foo.presentation;
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-07-14 14:17:45 +02:00
|
|
|
public class User {
|
|
|
|
// ..
|
|
|
|
}
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
|
2023-07-14 14:17:45 +02:00
|
|
|
However, this becomes an issue when a class has the same name as the superclass it extends or the interfaces it
|
|
|
|
implements, also known as class name shadowing. It is problematic because it can be unclear which class is being
|
|
|
|
referred to in the code, leading to ambiguity and potential bugs.
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-07-14 14:17:45 +02:00
|
|
|
Therefore, it is recommended to use unique and descriptive class names that do not conflict with the names of the
|
|
|
|
superclass or interfaces.
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-07-14 14:17:45 +02:00
|
|
|
This rule raises an issue when a class name shadows its superclass or interface names.
|
|
|
|
|
|
|
|
== How to fix it
|
|
|
|
|
|
|
|
Rename the class using a more descriptive name.
|
|
|
|
|
|
|
|
=== Code examples
|
|
|
|
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
2023-07-14 14:17:45 +02:00
|
|
|
package org.foo.presentation;
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-07-14 14:17:45 +02:00
|
|
|
public class User implements org.foo.domain.User { // Noncompliant
|
|
|
|
// ...
|
|
|
|
}
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-07-14 14:17:45 +02:00
|
|
|
==== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
|
|
----
|
|
|
|
package org.foo.presentation;
|
|
|
|
|
|
|
|
import org.foo.domain.User;
|
|
|
|
|
|
|
|
public class UserView implements User { // Compliant
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
== Resources
|
|
|
|
|
|
|
|
=== Documentation
|
|
|
|
|
|
|
|
* https://docs.oracle.com/javase/specs/jls/se17/html/jls-6.html#jls-6.4.1[Java SE Specifications > Java Language Specification > 6.4.1. Shadowing]
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Rename this class.
|
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
Modify rules S1612,S1640,S1643,S1656,S1710,S1751,S1849,S1858,S1862,S1871,S1872,S1940,S1994,S2093,S2130,S2133,S2140,S2147,S2153,S2154,S2168,S2178,S2183,S2185: SONARJAVA-5186 Improve Test Code Support Part 3 (#4525)
2024-11-22 09:51:54 +01:00
|
|
|
=== on 21 Nov 2024, 16:48:00 Alban Auzeill wrote:
|
|
|
|
[test-code-support-investigation-for-java] Decision for scope: Keep 'Main'. Not sure about the value/impact in test.
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== on 21 Oct 2014, 14:08:46 Nicolas Peru wrote:
|
|
|
|
LGTM
|
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|