achrefB3 / google-api-java-client

Automatically exported from code.google.com/p/google-api-java-client
0 stars 0 forks source link

FindBugs found: Synchronization issue in api.client.util.Data #253

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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> 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.<Enum>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.

Original issue reported on code.google.com by avall...@google.com on 28 Jul 2011 at 2:55

GoogleCodeExporter commented 9 years ago
I think the best way to fix this would be to use a non-concurrent collection 
(it's not really helping here), and explicitly synchronize all uses with it's 
monitor.

Original comment by avall...@google.com on 28 Jul 2011 at 3:01

GoogleCodeExporter commented 9 years ago
http://code.google.com/p/google-http-java-client/issues/detail?id=24

Original comment by yan...@google.com on 16 Aug 2011 at 2:24