private static Field getCachedTypemapFieldFor()

in google-http-client/src/main/java/com/google/api/client/json/JsonParser.java [896:938]


  private static Field getCachedTypemapFieldFor(Class<?> key) {
    if (key == null) {
      return null;
    }
    lock.lock();
    try {
      // Must use containsKey because we do store null values for when the class has no
      // JsonPolymorphicTypeMap field.
      if (cachedTypemapFields.containsKey(key)) {
        return cachedTypemapFields.get(key);
      }
      // Find the field that determines the type and cache it.
      Field value = null;
      Collection<FieldInfo> fieldInfos = ClassInfo.of(key).getFieldInfos();
      for (FieldInfo fieldInfo : fieldInfos) {
        Field field = fieldInfo.getField();
        JsonPolymorphicTypeMap typemapAnnotation =
            field.getAnnotation(JsonPolymorphicTypeMap.class);
        if (typemapAnnotation != null) {
          Preconditions.checkArgument(value == null,
              "Class contains more than one field with @JsonPolymorphicTypeMap annotation: %s",
              key);
          Preconditions.checkArgument(Data.isPrimitive(field.getType()),
              "Field which has the @JsonPolymorphicTypeMap, %s, is not a supported type: %s", key,
              field.getType());
          value = field;
          // Check for duplicate typeDef keys
          TypeDef[] typeDefs = typemapAnnotation.typeDefinitions();
          HashSet<String> typeDefKeys = Sets.newHashSet();
          Preconditions.checkArgument(
              typeDefs.length > 0, "@JsonPolymorphicTypeMap must have at least one @TypeDef");
          for (TypeDef typeDef : typeDefs) {
            Preconditions.checkArgument(typeDefKeys.add(typeDef.key()),
                "Class contains two @TypeDef annotations with identical key: %s", typeDef.key());
          }
        }
      }
      cachedTypemapFields.put(key, value);
      return value;
    } finally {
      lock.unlock();
    }
  }