• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ben-manes / caffeine / #5707

02 Aug 2026 12:45AM UTC coverage: 0.106% (-99.9%) from 100.0%
#5707

push

github

ben-manes
fix wikibench trace reader after overly strict audit fixes

0 of 4235 branches covered (0.0%)

9 of 8528 relevant lines covered (0.11%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/guava/src/main/java/com/github/benmanes/caffeine/guava/CaffeinatedGuavaLoadingCache.java
1
/*
2
 * Copyright 2015 Ben Manes. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.github.benmanes.caffeine.guava;
17

18
import static java.util.Objects.requireNonNull;
19

20
import java.io.Serializable;
21
import java.util.HashMap;
22
import java.util.Map;
23
import java.util.Set;
24
import java.util.concurrent.CompletableFuture;
25
import java.util.concurrent.ExecutionException;
26
import java.util.concurrent.Executor;
27

28
import org.jspecify.annotations.Nullable;
29

30
import com.github.benmanes.caffeine.cache.CacheLoader;
31
import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
32
import com.google.common.cache.CacheLoader.UnsupportedLoadingOperationException;
33
import com.google.common.cache.LoadingCache;
34
import com.google.common.collect.ImmutableList;
35
import com.google.common.collect.ImmutableMap;
36
import com.google.common.util.concurrent.ExecutionError;
37
import com.google.common.util.concurrent.FutureCallback;
38
import com.google.common.util.concurrent.Futures;
39
import com.google.common.util.concurrent.ListenableFuture;
40
import com.google.common.util.concurrent.UncheckedExecutionException;
41
import com.google.errorprone.annotations.CanIgnoreReturnValue;
42

43
/**
44
 * A Caffeine-backed loading cache through a Guava facade.
45
 *
46
 * @author ben.manes@gmail.com (Ben Manes)
47
 */
48
@SuppressWarnings("serial")
49
final class CaffeinatedGuavaLoadingCache<K, V>
50
    extends CaffeinatedGuavaCache<K, V> implements LoadingCache<K, V> {
51
  private static final ThreadLocal<Boolean> nullBulkLoad = new ThreadLocal<>();
×
52
  private static final long serialVersionUID = 1L;
53

54
  private final com.github.benmanes.caffeine.cache.LoadingCache<K, V> cache;
55

56
  CaffeinatedGuavaLoadingCache(com.github.benmanes.caffeine.cache.LoadingCache<K, V> cache) {
57
    super(cache);
×
58
    this.cache = cache;
×
59
  }
×
60

61
  @Override
62
  @SuppressWarnings("PMD.PreserveStackTrace")
63
  public V get(K key) throws ExecutionException {
64
    requireNonNull(key);
×
65
    try {
66
      return requireLoaded(cache.get(key));
×
67
    } catch (InvalidCacheLoadException e) {
×
68
      throw e;
×
69
    } catch (CacheLoaderException e) {
×
70
      throw new ExecutionException(e.getCause());
×
71
    } catch (RuntimeException e) {
×
72
      throw new UncheckedExecutionException(e);
×
73
    } catch (Error e) {
×
74
      throw new ExecutionError(e);
×
75
    }
76
  }
77

78
  @Override
79
  @SuppressWarnings({"CatchingUnchecked", "PMD.PreserveStackTrace"})
80
  public V getUnchecked(K key) {
81
    requireNonNull(key);
×
82
    try {
83
      return requireLoaded(cache.get(key));
×
84
    } catch (InvalidCacheLoadException e) {
×
85
      throw e;
×
86
    } catch (CacheLoaderException e) {
×
87
      throw new UncheckedExecutionException(e.getCause());
×
88
    } catch (Exception e) {
×
89
      throw new UncheckedExecutionException(e);
×
90
    } catch (Error e) {
×
91
      throw new ExecutionError(e);
×
92
    }
93
  }
94

95
  @Override
96
  @SuppressWarnings({"CatchingUnchecked", "PMD.PreserveStackTrace"})
97
  public ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException {
98
    requireNonNull(keys);
×
99
    var keysToLoad = ImmutableList.copyOf(keys);
×
100
    try {
101
      Map<K, V> result = cache.getAll(keysToLoad);
×
102
      if (nullBulkLoad.get() != null) {
×
103
        throw new InvalidCacheLoadException("null key or value");
×
104
      }
105
      for (K key : keysToLoad) {
×
106
        if (!result.containsKey(key)) {
×
107
          throw new InvalidCacheLoadException("loadAll failed to return a value for " + key);
×
108
        }
109
      }
×
110
      return ImmutableMap.copyOf(result);
×
111
    } catch (InvalidCacheLoadException e) {
×
112
      throw e;
×
113
    } catch (CacheLoaderException e) {
×
114
      throw new ExecutionException(e.getCause());
×
115
    } catch (Exception e) {
×
116
      throw new UncheckedExecutionException(e);
×
117
    } catch (Error e) {
×
118
      throw new ExecutionError(e);
×
119
    } finally {
120
      nullBulkLoad.remove();
×
121
    }
122
  }
123

124
  @Override
125
  @SuppressWarnings("deprecation")
126
  public V apply(K key) {
127
    return getUnchecked(key);
×
128
  }
129

130
  @Override
131
  @SuppressWarnings("FutureReturnValueIgnored")
132
  public void refresh(K key) {
133
    cache.refresh(key);
×
134
  }
×
135

136
  /** Returns the loaded value, or throws if absent. */
137
  @CanIgnoreReturnValue
138
  private static <V> V requireLoaded(@Nullable V value) {
139
    if (value == null) {
×
140
      throw new InvalidCacheLoadException("null value");
×
141
    }
142
    return value;
×
143
  }
144

145
  abstract static class CaffeinatedLoader<K, V> implements CacheLoader<K, V>, Serializable {
146
    private static final long serialVersionUID = 1L;
147

148
    final com.google.common.cache.CacheLoader<K, V> cacheLoader;
149

150
    CaffeinatedLoader(com.google.common.cache.CacheLoader<K, V> cacheLoader) {
×
151
      this.cacheLoader = requireNonNull(cacheLoader);
×
152
    }
×
153
    @SuppressWarnings("ConstantValue")
154
    @Override public CompletableFuture<V> asyncReload(K key, V oldValue, Executor executor) {
155
      var future = new CompletableFuture<V>();
×
156
      try {
157
        ListenableFuture<V> reloader = cacheLoader.reload(key, oldValue);
×
158
        if (reloader == null) {
×
159
          future.completeExceptionally(new InvalidCacheLoadException("null future"));
×
160
        } else {
161
          Futures.addCallback(reloader, new FutureCompleter<>(future), Runnable::run);
×
162
        }
163
      } catch (InterruptedException e) {
×
164
        Thread.currentThread().interrupt();
×
165
        future.completeExceptionally(e);
×
166
      } catch (Throwable t) {
×
167
        future.completeExceptionally(t);
×
168
      }
×
169
      return future;
×
170
    }
171
    /** Sequentially loads each missing entry. */
172
    @SuppressWarnings("PMD.SignatureDeclareThrowsException")
173
    Map<K, V> loadSequentially(Set<? extends K> keys) throws Exception {
174
      var result = new HashMap<K, V>(keys.size(), /* loadFactor= */ 1.0f);
×
175
      for (K key : keys) {
×
176
        result.put(key, load(key));
×
177
      }
×
178
      return result;
×
179
    }
180
  }
181

182
  static class InternalSingleLoader<K, V> extends CaffeinatedLoader<K, V> {
183
    private static final long serialVersionUID = 1L;
184

185
    InternalSingleLoader(com.google.common.cache.CacheLoader<K, V> cacheLoader) {
186
      super(cacheLoader);
×
187
    }
×
188
    @SuppressWarnings("ConstantValue")
189
    @Override public V load(K key) {
190
      try {
191
        V value = cacheLoader.load(key);
×
192
        if (value == null) {
×
193
          throw new InvalidCacheLoadException("null value");
×
194
        }
195
        return value;
×
196
      } catch (RuntimeException | Error e) {
×
197
        throw e;
×
198
      } catch (InterruptedException e) {
×
199
        Thread.currentThread().interrupt();
×
200
        throw new CacheLoaderException(e);
×
201
      } catch (Exception e) {
×
202
        throw new CacheLoaderException(e);
×
203
      }
204
    }
205
  }
206

207
  static final class InternalBulkLoader<K, V> extends InternalSingleLoader<K, V> {
208
    private static final long serialVersionUID = 1L;
209

210
    InternalBulkLoader(com.google.common.cache.CacheLoader<K, V> cacheLoader) {
211
      super(cacheLoader);
×
212
    }
×
213
    @SuppressWarnings("ConstantValue")
214
    @Override public Map<K, V> loadAll(Set<? extends K> keys) {
215
      try {
216
        Map<K, V> loaded;
217
        try {
218
          loaded = cacheLoader.loadAll(keys);
×
219
        } catch (UnsupportedLoadingOperationException e) {
×
220
          return loadSequentially(keys);
×
221
        }
×
222
        if (loaded == null) {
×
223
          throw new InvalidCacheLoadException("null map");
×
224
        }
225
        var result = new HashMap<K, V>(loaded.size(), /* loadFactor= */ 1.0f);
×
226
        loaded.forEach((key, value) -> {
×
227
          if ((key == null) || (value == null)) {
×
228
            nullBulkLoad.set(true);
×
229
          } else {
230
            result.put(key, value);
×
231
          }
232
        });
×
233
        return result;
×
234
      } catch (RuntimeException | Error e) {
×
235
        throw e;
×
236
      } catch (InterruptedException e) {
×
237
        Thread.currentThread().interrupt();
×
238
        throw new CacheLoaderException(e);
×
239
      } catch (Exception e) {
×
240
        throw new CacheLoaderException(e);
×
241
      }
242
    }
243
  }
244

245
  static class ExternalSingleLoader<K, V> extends CaffeinatedLoader<K, V> {
246
    private static final long serialVersionUID = 1L;
247

248
    ExternalSingleLoader(com.google.common.cache.CacheLoader<K, V> cacheLoader) {
249
      super(cacheLoader);
×
250
    }
×
251
    @SuppressWarnings("ConstantValue")
252
    @Override public V load(K key) throws Exception {
253
      V value = cacheLoader.load(key);
×
254
      if (value == null) {
×
255
        throw new InvalidCacheLoadException("null value");
×
256
      }
257
      return value;
×
258
    }
259
  }
260

261
  static final class ExternalBulkLoader<K, V> extends ExternalSingleLoader<K, V> {
262
    private static final long serialVersionUID = 1L;
263

264
    ExternalBulkLoader(com.google.common.cache.CacheLoader<K, V> cacheLoader) {
265
      super(cacheLoader);
×
266
    }
×
267
    @SuppressWarnings("ConstantValue")
268
    @Override public Map<K, V> loadAll(Set<? extends K> keys) throws Exception {
269
      Map<K, V> loaded;
270
      try {
271
        loaded = cacheLoader.loadAll(keys);
×
272
      } catch (UnsupportedLoadingOperationException e) {
×
273
        return loadSequentially(keys);
×
274
      }
×
275
      if (loaded == null) {
×
276
        throw new InvalidCacheLoadException("null map");
×
277
      }
278
      for (var entry : loaded.entrySet()) {
×
279
        if ((entry.getKey() == null) || (entry.getValue() == null)) {
×
280
          throw new InvalidCacheLoadException("null key or value");
×
281
        }
282
      }
×
283
      return loaded;
×
284
    }
285
  }
286

287
  static final class FutureCompleter<V> implements FutureCallback<V> {
288
    final CompletableFuture<V> future;
289

290
    FutureCompleter(CompletableFuture<V> future) {
×
291
      this.future = future;
×
292
    }
×
293
    @Override public void onSuccess(@Nullable V value) {
294
      if (value == null) {
×
295
        future.completeExceptionally(new InvalidCacheLoadException("null value"));
×
296
      } else {
297
        future.complete(value);
×
298
      }
299
    }
×
300
    @Override public void onFailure(Throwable t) {
301
      future.completeExceptionally(t);
×
302
    }
×
303
  }
304
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc