Skip to content

Commit e320013

Browse files
SONARJAVA-4952 Update Rules Metadata (#4778)
1 parent 50333fc commit e320013

5 files changed

Lines changed: 73 additions & 23 deletions

File tree

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1452.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ <h3>Articles &amp; blog posts</h3>
4141
<ul>
4242
<li> <a href="https://medium.com/javarevisited/variance-in-java-and-scala-63af925d21dc">Sinisa Louc - A Complete Guide to Variance in Java and
4343
Scala</a> </li>
44-
<li> <a href="https://kotlinexpertise.com/kotlin-generics-and-variance-vs-java">Kotlin Expertise Blog - Kotlin Generics and Variance (Compared to
45-
Java)</a> </li>
44+
<li> <a href="https://web.archive.org/web/20240206045705/https://kotlinexpertise.com/kotlin-generics-and-variance-vs-java/">Kotlin Expertise Blog -
45+
Kotlin Generics and Variance (Compared to Java)</a> </li>
4646
<li> <a href="https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)">Wikipedia - Covariance and contravariance (computer
4747
science)</a> </li>
4848
<li> <a href="https://schneide.blog/2015/05/11/declaration-site-and-use-site-variance-explained/">Schneide Blog - Declaration-site and use-site

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S3740.html

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,90 @@
1-
<p>Generic types should not be used raw (without type parameters). To fix this issue, add the type parameters.</p>
1+
<p>Generic types should not be used raw (without type arguments). To fix this issue, add the type parameters.</p>
22
<h2>Why is this an issue?</h2>
33
<p>A generic type is a generic class or interface that is parameterized over types. For example, <code>java.util.List</code> has one type parameter:
44
the type of its elements.</p>
5-
<p>When generic types are used raw (without type parameters), the compiler is not able to do generic type checking. For this reason, it is sometimes
6-
necessary to cast objects and defer type-checking to runtime.</p>
5+
<p>Using generic types raw (without binding arguments to the type parameters) prevents compile-time type checking for expressions that use these type
6+
parameters. Explicit type casts are necessary for them, which do perform a runtime type check that may fail with a
7+
<code>ClassCastException</code>.</p>
78
<h3>What is the potential impact?</h3>
8-
<p>When a cast fails, a <code>ClassCastException</code> is thrown and the program most likely crashes. Therefore, this issue might impact the
9-
availability and reliability of your application.</p>
9+
<p>The compiler cannot assert that the program is inherently type safe. When a cast fails, a <code>ClassCastException</code> is thrown during runtime
10+
and the program most likely crashes. Therefore, this issue might impact the availability and reliability of your application.</p>
11+
<h3>Exceptions</h3>
12+
<p>The rule does not raise an issue for the simple <code>instanceof</code> operator, which checks against runtime types where type parameter
13+
information has been erased. Since it does not return a rawly typed instance but a boolean value, it does not prevent compile-time type checking.</p>
14+
<p>This, however, is not the case for the <code>cast</code> operator as well as the extended <code>instanceof</code> operator which are both not an
15+
exception from this rule. Since they operate on the erased runtime type as well, they must use wildcard type arguments when checked against a
16+
parameterized type (see the examples).</p>
1017
<h2>How to fix it</h2>
11-
<p>You should add type parameters. In the case of collections, the type parameter(s) should correspond to the type of elements that the list is
12-
intended to store.</p>
18+
<p>For any usage of parameterized types, bind the type parameters with type arguments. For example, when a function returns a list of strings, the
19+
return type is <code>List&lt;String&gt;</code>, where the type parameter <code>E</code> in interface <code>List&lt;E&gt;</code> is bound with the
20+
argument <code>String</code>.</p>
21+
<p>If the concrete binding is unknown, you still should not use the type raw. Use a wildcard type argument instead, with optional lower or upper
22+
bound, such as in <code>List&lt;?&gt;</code> for a list whose element type is unknown, or <code>List&lt;? extends Number&gt;</code> for a list whose
23+
element type is <code>Number</code> or a subtype of it.</p>
1324
<h3>Code examples</h3>
1425
<h4>Noncompliant code example</h4>
1526
<pre data-diff-id="1" data-diff-type="noncompliant">
27+
// List is supposed to store integers only
1628
List integers = new ArrayList&lt;&gt;();
1729

18-
// It is possible to add a string to a list that is supposed to be integers only
30+
// Yet, we can add strings, because we did not give
31+
// this information to the compiler
1932
integers.add("Hello World!");
2033

21-
Integer a = (Integer) integers.get(0); // ClassCastException!
34+
// Type is checked during runtime and will throw a ClassCastException
35+
Integer a = (Integer) integers.get(0);
2236
</pre>
2337
<h4>Compliant solution</h4>
2438
<pre data-diff-id="1" data-diff-type="compliant">
39+
// List is supposed to store integers, and we let the compiler know
2540
List&lt;Integer&gt; integers = new ArrayList&lt;&gt;();
2641

27-
// The program does not compile anymore with this mistake:
28-
// integers.add("Hello World!");
42+
// Now we can add only integers.
43+
// Adding a string results in a compile time error.
2944
integers.add(42);
3045

31-
Integer a = integers.get(0); // No need to cast anymore.
46+
// No cast required anymore, and no possible ClassCastException
47+
Integer a = integers.get(0);
48+
</pre>
49+
<h4>Noncompliant code example</h4>
50+
<pre data-diff-id="2" data-diff-type="noncompliant">
51+
String getStringFromForcedList(Object object) {
52+
// Cast expression and instanceof can check runtime type only.
53+
// The solution is _not_ to skip the type argument in that case.
54+
return object instanceof List stringList ? (String) stringList.getFirst(): "";
55+
}
56+
</pre>
57+
<h4>Compliant solution</h4>
58+
<pre data-diff-id="2" data-diff-type="compliant">
59+
String getStringFromForcedList(Object object) {
60+
// The solution is to use a wildcard type argument in that case.
61+
return object instanceof List&lt;?&gt; stringList ? (String) stringList.getFirst(): "";
62+
}
63+
</pre>
64+
<h4>Noncompliant code example</h4>
65+
<pre data-diff-id="3" data-diff-type="noncompliant">
66+
String getStringFromForcedList(Object object) {
67+
return object instanceof List stringList ? (String) stringList.getFirst(): "";
68+
}
69+
70+
String returnString() {
71+
Object object = List.of("Hello");
72+
return getStringFromForcedList(object);
73+
}
74+
</pre>
75+
<h4>Compliant solution</h4>
76+
<pre data-diff-id="3" data-diff-type="compliant">
77+
Object getObjectFromForcedList(Object object) {
78+
// You may also choose not to make assumptions about type arguments you cannot infer.
79+
return object instanceof List&lt;?&gt; list ? list.getFirst(): "";
80+
}
81+
82+
String returnString(Object object) {
83+
// Instead, delegate the decision to use-site, which may have more information.
84+
Object object = List.of("Hello");
85+
return (String) getObjectFromForcedList(object);
86+
}
3287
</pre>
33-
<h3>How does this work?</h3>
34-
<p>In the noncompliant example, <code>List</code> is used as a raw type. Even though the list stores integers, the compiler will type its elements as
35-
<code>Object</code>, To use an element of the list as an integer, it needs to be cast first. But elements are not garanteed to be integers. In this
36-
case, a <code>String</code> is erroneously appended to the list, causing the cast to <code>Integer</code> to fail.</p>
37-
<p>When the type parameter is specified, this bug is detected by the compiler during type-checking. The cast is also unncessary in this case.</p>
3888
<h2>Resources</h2>
3989
<h3>Documentation</h3>
4090
<ul>

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S6437.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ <h4>Compliant solution</h4>
5252
String inputString = System.getenv("SECRET");
5353
byte[] key = inputString.getBytes();
5454

55-
SHA256.getHMAC(key, message); // Noncompliant
55+
SHA256.getHMAC(key, message);
5656
</pre>
5757
<h3>How does this work?</h3>
5858
<p>While the noncompliant code example contains a hard-coded password, the compliant solution retrieves the secret’s value from its environment. This

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S6830.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ <h2>Why is this an issue?</h2>
22
<p>Consistent naming of beans is important for the readability and maintainability of the code. More precisely, according to the Spring
33
documentation:</p>
44
<pre>
5-
Naming beans consistently makes your configuration easier to read and understand, and if you are using Spring AOP it helps a lot when applying advice to a set of beans related by name.
5+
Naming beans consistently makes your configuration easier to read and understand. Also, if you use Spring AOP, it helps a lot when applying advice to a set of beans related by name.
66
</pre>
77
<p>Not following accepted conventions can introduce inconsistent naming, especially when multiple developers work on the same project, leading to
88
technical debt.</p>
@@ -48,7 +48,7 @@ <h4>Compliant solution</h4>
4848
<h2>Resources</h2>
4949
<h3>Documentation</h3>
5050
<ul>
51-
<li> Spring Framework Documentation - <a href="https://docs.spring.io/spring-framework/docs/3.0.0.M4/reference/html/ch03s03.html">3.3 Bean
51+
<li> Spring Framework Documentation - <a href="https://docs.spring.io/spring-framework/reference/core/beans/definition.html#beans-beanname">3.3 Bean
5252
overview</a> </li>
5353
</ul>
5454
<h3>Articles &amp; blog posts</h3>

sonarpedia.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"languages": [
44
"JAVA"
55
],
6-
"latest-update": "2024-04-04T13:21:36.775963517Z",
6+
"latest-update": "2024-04-24T09:55:28.527679Z",
77
"options": {
88
"no-language-in-filenames": true,
99
"preserve-filenames": false

0 commit comments

Comments
 (0)