-
Notifications
You must be signed in to change notification settings - Fork 465
Description
From [email protected] on August 16, 2011 07:23:53
Version of google-http-java-client (e.g. 1.5.0-beta)? 1.5.0-beta Java environment (e.g. Java 6, Android 2.3, App Engine 1.4.3)? All Describe the problem. This class is synchronizing on the monitor of a ConcurrentHashMap instance. Concurrent objects have their own concurrency controls that are distinct form and incompatible with the keyword synchronized. The following function will not prevent other threads from making changes to NULL_CHANGE because it uses the NULL_CHANGE monitor, while other threads may be using the built-in concurrency provided by the collection:
public static T nullOf(Class objClass) {
Object result = NULL_CACHE.get(objClass);
if (result == null) {
synchronized (NULL_CACHE) {
result = NULL_CACHE.get(objClass);
if (result == null) {
if (objClass.isArray()) {
// arrays are special because we need to compute both the dimension and component type
int dims = 0;
Class componentType = objClass;
do {
componentType = componentType.getComponentType();
dims++;
} while (componentType.isArray());
result = Array.newInstance(componentType, new int[dims]);
} else if (objClass.isEnum()) {
// enum requires look for constant with @NullValue
FieldInfo fieldInfo = ClassInfo.of(objClass).getFieldInfo(null);
Preconditions.checkNotNull(
fieldInfo, "enum missing constant with @NullValue annotation: %s", objClass);
@SuppressWarnings({"unchecked", "rawtypes"})
Enum e = fieldInfo.enumValue();
result = e;
} else {
// other classes are simpler
result = Types.newInstance(objClass);
}
NULL_CACHE.put(objClass, result);
}
}
}
@SuppressWarnings("unchecked")
T tResult = (T) result;
return tResult;
}
FindBugs Output:
Synchronization performed on util.concurrent instance
This method performs synchronization an object that is an instance of a class from the java.util.concurrent package (or its subclasses). Instances of these classes have there own concurrency control mechanisms that are distinct from and incompatible with the use of the keyword synchronized. How would you expect it to be fixed?
Original issue: http://code.google.com/p/google-http-java-client/issues/detail?id=24