2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2023-05-31 16:04:32 +02:00
|
|
|
Programming languages evolve over time, and new versions of Java introduce additional keywords.
|
|
|
|
If future keywords are used in the current code, it can create compatibility issues when transitioning to newer versions of Java.
|
|
|
|
The code may fail to compile or behave unexpectedly due to conflicts with newly introduced keywords.
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-05-31 16:04:32 +02:00
|
|
|
The following keywords are marked as invalid identifiers:
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2021-06-03 16:43:28 +02:00
|
|
|
[frame=all]
|
|
|
|
[cols="^1,^1"]
|
|
|
|
|===
|
2023-05-31 16:04:32 +02:00
|
|
|
|Keyword|Added in version
|
2021-06-03 16:43:28 +02:00
|
|
|
|
2023-05-31 16:04:32 +02:00
|
|
|
|`_`|9
|
|
|
|
|`enum`|5.0
|
2021-06-03 16:43:28 +02:00
|
|
|
|===
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-05-31 16:04:32 +02:00
|
|
|
`assert` and `strictfp` are another example of valid identifiers which became keywords in later versions, but are not supported by this rule.
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-05-31 16:04:32 +02:00
|
|
|
== How to fix it
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-05-31 16:04:32 +02:00
|
|
|
Rename the identifiers that use Java keywords.
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-31 16:04:32 +02:00
|
|
|
=== Code examples
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-05-31 16:04:32 +02:00
|
|
|
==== Noncompliant code example
|
|
|
|
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
2023-05-31 16:04:32 +02:00
|
|
|
public class MyClass {
|
|
|
|
int enum = 42; // Noncompliant
|
|
|
|
String _ = ""; // Noncompliant
|
2021-04-28 16:49:39 +02:00
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-31 16:04:32 +02:00
|
|
|
==== Compliant solution
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-05-31 16:04:32 +02:00
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
2023-05-31 16:04:32 +02:00
|
|
|
public class MyClass {
|
|
|
|
int magic = 42; // Noncompliant
|
|
|
|
String s = ""; // Noncompliant
|
2021-04-28 16:49:39 +02:00
|
|
|
}
|
|
|
|
----
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-31 16:04:32 +02:00
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html[Oracle - Java Language Keywords]
|
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 variable to something other than "XXX", which is a Java keyword.
|
|
|
|
|
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)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== is duplicated by: S3859
|
|
|
|
|
|
|
|
=== relates to: S1669
|
|
|
|
|
|
|
|
=== relates to: S2306
|
|
|
|
|
|
|
|
=== is related to: S1527
|
|
|
|
|
|
|
|
=== is related to: S1189
|
|
|
|
|
|
|
|
=== on 8 Aug 2013, 16:32:59 Freddy Mallet wrote:
|
|
|
|
Is implemented by \http://jira.codehaus.org/browse/SONARJAVA-280
|
|
|
|
|
|
|
|
=== on 13 Aug 2019, 10:18:47 Michael Gumowski wrote:
|
|
|
|
Removing reference to JIRA ticket in rule description. See SONARJAVA-285 and SONARJAVA-3179 for rule limitation.
|
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|