4521
4522 /* --------------- Async Loading Cache --------------- */
4523
4524 static final class BoundedLocalAsyncLoadingCache<K, V>4525 extends LocalAsyncLoadingCache<K, V> implements Serializable {4526 private static final long serialVersionUID = 1;45274528 final BoundedLocalCache<K, CompletableFuture<V>> cache;4529 final boolean isWeighted;45304531 @Nullable ConcurrentMap<K, CompletableFuture<V>> mapView;4532 @Nullable Policy<K, V> policy;45334534 @SuppressWarnings("unchecked")4535 BoundedLocalAsyncLoadingCache(Caffeine<K, V> builder, AsyncCacheLoader<? super K, V> loader) {4536 super(loader);4537 isWeighted = builder.isWeighted();4538 cache = (BoundedLocalCache<K, CompletableFuture<V>>) LocalCacheFactory4539 .newBoundedLocalCache(builder, loader, /* async */ true);4540 }45414542 @Override4543 public BoundedLocalCache<K, CompletableFuture<V>> cache() {4544 return cache;4545 }45464547 @Override4548 public ConcurrentMap<K, CompletableFuture<V>> asMap() {4549 return (mapView == null) ? (mapView = new AsyncAsMapView<>(this)) : mapView;4550 }45514552 @Override4553 public Policy<K, V> policy() {4554 if (policy == null) {4555 @SuppressWarnings("unchecked")4556 BoundedLocalCache<K, V> castCache = (BoundedLocalCache<K, V>) cache;4557 Function<CompletableFuture<V>, V> transformer = Async::getIfReady;4558 @SuppressWarnings("unchecked")4559 Function<V, V> castTransformer = (Function<V, V>) transformer;4560 policy = new BoundedPolicy<>(castCache, castTransformer, isWeighted);4561 }4562 return policy;4563 }45644565 @SuppressWarnings("UnusedVariable")4566 private void readObject(ObjectInputStream stream) throws InvalidObjectException {4567 throw new InvalidObjectException("Proxy required");4568 }45694570 private Object writeReplace() {4571 return makeSerializationProxy(cache);4572 }4573 }4574}
4575
4576/** The namespace for field padding through inheritance. */
This serializable class has a non-serializable superclass that does not declare a default constructor. Deserializing such a class will fail with an InvalidClassException
because Java will not be able to instantiate it.
Java's Serializable
interface enforces specific requirements on serializable classes that extend a non-serializable class:
To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class
Serializable
if this is not the case. The error will be detected at runtime.
Put simply, given the following conditions:
Serializable
.Java will throw an InvalidClassException
when attempting to deserialize an instance of the class.
class SuperClass {
int x;
public SuperClass(int a) {
x = a;
}
}
// Java will fail to deserialize this class.
class SubClass extends SuperClass implements Serializable {
// ...
}
class SuperClass {
int x;
public SuperClass(int a) {
x = a;
}
public SuperClass() {
x = 0;
}
}
class SubClass extends SuperClass implements Serializable {
// ...
}