容器访问类型不匹配¶
ID: java/type-mismatch-access
Kind: problem
Security severity:
Severity: error
Precision: very-high
Tags:
- reliability
- correctness
- logic
Query suites:
- java-security-and-quality.qls
Collection
接口的 contains
方法的参数类型为 Object
。因此,您可以尝试检查任何类型的对象是否为集合的成员,而不管集合的元素类型如何。但是,虽然您可以使用与集合类型不同的参数调用 contains
,但集合实际上不太可能包含这种类型的对象。
类似的考虑适用于其他容器访问方法,例如 Map.get
,其中参数也可以具有类型 Object
。
建议¶
确保在调用 contains
时使用正确的参数。
示例¶
在以下示例中,虽然 contains
的参数是整数,但代码不会导致类型错误,因为参数不必与 list
元素的类型匹配。但是,该参数不太可能被找到(因此 if
语句的主体不会执行),因此它可能是一个印刷错误:该参数应该用引号括起来。
void m(List<String> list) {
if (list.contains(123)) { // Call 'contains' with non-string argument (without quotation marks)
// ...
}
}
请注意,在处理包含装箱类型集合时,必须格外小心,如下面的示例所示。第一次调用contains
返回false
,因为无法比较两个不同类型的装箱数值基本类型,在本例中为Short(1)
(在set
中)和Integer(1)
(参数)。第二次调用contains
返回true
,因为可以比较Short(1)
和Short(1)
。
HashSet<Short> set = new HashSet<Short>();
short s = 1;
set.add(s);
// Following statement prints 'false', because the argument is a literal int, which is auto-boxed
// to an Integer
System.out.println(set.contains(1));
// Following statement prints 'true', because the argument is a literal int that is cast to a short,
// which is auto-boxed to a Short
System.out.println(set.contains((short)1));
参考文献¶
Java API 规范:Collection.contains.