rspec/rules/S2293/java/rule.adoc

49 lines
1.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Java 7 introduced the diamond operator (``++<>++``) to reduce the verbosity of generics code. For instance, instead of having to declare a ``++List++``'s type in both its declaration and its constructor, you can now simplify the constructor declaration with ``++<>++``, and the compiler will infer the type.
*Note* that this rule is automatically disabled when the project's ``++sonar.java.source++`` is lower than ``++7++``.
=== Noncompliant code example
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
----
List<String> strings = new ArrayList<String>(); // Noncompliant
Map<String,List<Integer>> map = new HashMap<String,List<Integer>>(); // Noncompliant
----
=== Compliant solution
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
----
List<String> strings = new ArrayList<>();
Map<String,List<Integer>> map = new HashMap<>();
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace the type specification in this constructor call with the diamond operator ("<>"). [(sonar.java.source not set. Assuming 7 or greater.)]
'''
== Comments And Links
(visible only on this page)
=== on 17 Dec 2014, 13:55:04 Nicolas Peru wrote:
Fine by me. This rule should probably be implemented only when we have figured a way to activate rules depending on version of java used.
=== on 17 Dec 2014, 14:46:31 Ann Campbell wrote:
\[~nicolas.peru] I've set it to off by default & tagged it java7 just like some of the other Java 7/8-specific rules. I think this could be implemented now & we trust the user to turn it on only when indicated.
endif::env-github,rspecator-view[]