42 lines
880 B
Plaintext
42 lines
880 B
Plaintext
![]() |
include::../description.adoc[]
|
||
|
|
||
|
== Noncompliant Code Example
|
||
|
|
||
|
----
|
||
|
public class S1944 {
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
List<String> list = (List<String>) getAttributes(); // Noncompliant; List<Integer> return by getAttributes() is not be casted to List<String>
|
||
|
String s = list.get(0); // java.lang.ClassCastException will be raised here
|
||
|
}
|
||
|
|
||
|
private static List<?> getAttributes() {
|
||
|
List<Integer> result = new ArrayList<>();
|
||
|
result.add(0);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
----
|
||
|
|
||
|
== Compliant Solution
|
||
|
|
||
|
----
|
||
|
public class S1944 {
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
List<Integer> list = (List<Integer>) getAttributes(); // Compliant
|
||
|
String s = String.valueOf(list.get(0));
|
||
|
}
|
||
|
|
||
|
private static List<?> getAttributes() {
|
||
|
List<Integer> result = new ArrayList<>();
|
||
|
result.add(0);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
----
|
||
|
|
||
|
include::../see.adoc[]
|