You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
2.8 KiB
114 lines
2.8 KiB
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
package codetoanalyze.java.infer;
|
|
|
|
import java.util.HashMap;
|
|
|
|
public class HashMapExample {
|
|
|
|
public static void putIntegerTwiceThenGetTwiceOk(HashMap<Integer, Integer> hashMap) {
|
|
Integer i32 = new Integer(32);
|
|
Integer i52 = new Integer(52);
|
|
|
|
hashMap.put(i32, i32);
|
|
hashMap.put(i52, i52);
|
|
|
|
Integer a = hashMap.get(i32);
|
|
Integer b = hashMap.get(i52);
|
|
|
|
a.intValue();
|
|
b.intValue();
|
|
}
|
|
|
|
public static void containsIntegerTwiceThenGetTwiceOk(HashMap<Integer, Integer> hashMap) {
|
|
Integer i32 = new Integer(32);
|
|
Integer i52 = new Integer(52);
|
|
|
|
if (hashMap.containsKey(i32) && hashMap.containsKey(i52)) {
|
|
Integer a = hashMap.get(i32);
|
|
Integer b = hashMap.get(i52);
|
|
a.intValue();
|
|
b.intValue();
|
|
}
|
|
}
|
|
|
|
public static int getOneIntegerWithoutCheckBad() {
|
|
HashMap<Integer, Integer> hashMap = new HashMap<>();
|
|
Integer i32 = new Integer(32);
|
|
|
|
Integer a = hashMap.get(i32);
|
|
|
|
return a.intValue();
|
|
}
|
|
|
|
public static void getTwoIntegersWithOneCheckOk(Integer i, Integer j) {
|
|
HashMap<Integer, Integer> hashMap = new HashMap<>();
|
|
|
|
if (hashMap.containsKey(i) && !i.equals(j)) {
|
|
Integer a = hashMap.get(i);
|
|
Integer b = hashMap.get(j);
|
|
|
|
a.intValue();
|
|
b.intValue();
|
|
}
|
|
}
|
|
|
|
public static Integer getOrCreateInteger(final HashMap<Integer, Integer> map, final int id) {
|
|
Integer x = null;
|
|
if (map.containsKey(id)) {
|
|
x = map.get(id);
|
|
} else {
|
|
x = new Integer(0);
|
|
map.put(id, x);
|
|
}
|
|
return x;
|
|
}
|
|
|
|
public static void getOrCreateIntegerThenDerefOk(final HashMap<Integer, Integer> map) {
|
|
Integer x = getOrCreateInteger(map, 42);
|
|
// dereference x
|
|
x.toString();
|
|
}
|
|
|
|
void getAfterRemovingTheKeyBad() {
|
|
HashMap<Integer, Object> map = new HashMap();
|
|
Integer key = 42;
|
|
map.put(key, new Object());
|
|
map.remove(key);
|
|
map.get(key).toString(); // NPE here
|
|
}
|
|
|
|
void getAfterRemovingAnotherKeyOk() {
|
|
HashMap<Integer, Object> map = new HashMap();
|
|
Integer key = 42;
|
|
map.put(key, new Object());
|
|
map.remove(0);
|
|
map.get(key).toString();
|
|
}
|
|
|
|
void getAfterClearBad() {
|
|
HashMap<Integer, Object> map = new HashMap();
|
|
Integer key = 42;
|
|
map.put(key, new Object());
|
|
map.clear();
|
|
map.get(key).toString(); // NPE here
|
|
}
|
|
|
|
// A null pointer dereference is currently reported as latent (during the access of
|
|
// s.equals("foo")).
|
|
// Once keySet() is modelled, this null deference should not be reported anymore
|
|
void FP_latent_getFromKeySetOk(HashMap<String, String> map) {
|
|
for (String key : map.keySet()) {
|
|
String s = map.get(key);
|
|
if (s.equals("foo")) {
|
|
System.out.println("true");
|
|
}
|
|
}
|
|
}
|
|
}
|