-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathImpossibleCast.qhelp
More file actions
42 lines (28 loc) · 1.2 KB
/
ImpossibleCast.qhelp
File metadata and controls
42 lines (28 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Some downcasts on arrays will fail at runtime.
An object <code>a</code> with dynamic type <code>A[]</code> cannot be cast to <code>B[]</code>,
where <code>B</code> is a subtype of <code>A</code>,
even if all the elements of <code>a</code> can be cast to <code>B</code>.
</p>
</overview>
<recommendation>
<p>Ensure that the array creation expression constructs an array object of the right type.</p>
</recommendation>
<example>
<p>The following example shows an assignment that throws a <code>ClassCastException</code> at runtime.</p>
<sample language="java">String[] strs = (String[])new Object[]{ "hello", "world" };</sample>
<p>To avoid the exception, a <code>String</code> array should be created instead.</p>
<sample language="java">String[] strs = new String[]{ "hello", "world" };</sample>
</example>
<references>
<li>
Java Language Specification:
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-5.html#jls-5.1.6">Narrowing Reference Conversion</a>,
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.10.3">Subtyping among Array Types</a>.
</li>
</references>
</qhelp>