mirror of
https://github.com/VibedByKaKi/alexandroid.git
synced 2026-11-15 02:17:18 +01:00
175 lines
7 KiB
Java
175 lines
7 KiB
Java
package com.google.common.base;
|
|
|
|
import com.google.common.annotations.GwtIncompatible;
|
|
import com.google.common.annotations.J2ktIncompatible;
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
import com.google.common.base.internal.Finalizer;
|
|
import java.io.Closeable;
|
|
import java.io.IOException;
|
|
import java.lang.ref.PhantomReference;
|
|
import java.lang.ref.Reference;
|
|
import java.lang.ref.ReferenceQueue;
|
|
import java.lang.reflect.Method;
|
|
import java.net.URL;
|
|
import java.net.URLClassLoader;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
import okio.Buffer$$ExternalSyntheticBUOutline0;
|
|
import okio.Path$$ExternalSyntheticBUOutline0;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
@J2ktIncompatible
|
|
@GwtIncompatible
|
|
/* loaded from: classes8.dex */
|
|
public class FinalizableReferenceQueue implements Closeable {
|
|
private static final String FINALIZER_CLASS_NAME = "com.google.common.base.internal.Finalizer";
|
|
private static final Logger logger = Logger.getLogger(FinalizableReferenceQueue.class.getName());
|
|
private static final Method startFinalizer = getStartFinalizer(loadFinalizer(new SystemLoader(), new DecoupledLoader(), new DirectLoader()));
|
|
final PhantomReference<Object> frqRef;
|
|
final ReferenceQueue<Object> queue;
|
|
final boolean threadStarted;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
public static class DecoupledLoader implements FinalizerLoader {
|
|
private static final String LOADING_ERROR = "Could not load Finalizer in its own class loader. Loading Finalizer in the current class loader instead. As a result, you will not be able to garbage collect this class loader. To support reclaiming this class loader, either resolve the underlying issue, or move Guava to your system class path.";
|
|
|
|
public URL getBaseUrl() throws IOException {
|
|
String str = FinalizableReferenceQueue.FINALIZER_CLASS_NAME.replace('.', '/') + ".class";
|
|
URL resource = getClass().getClassLoader().getResource(str);
|
|
if (resource == null) {
|
|
Path$$ExternalSyntheticBUOutline0.m(str);
|
|
return null;
|
|
}
|
|
String url = resource.toString();
|
|
if (url.endsWith(str)) {
|
|
return new URL(resource, url.substring(0, url.length() - str.length()));
|
|
}
|
|
Buffer$$ExternalSyntheticBUOutline0.m$2("Unsupported path style: ".concat(url));
|
|
return null;
|
|
}
|
|
|
|
@Override // com.google.common.base.FinalizableReferenceQueue.FinalizerLoader
|
|
public Class<?> loadFinalizer() {
|
|
try {
|
|
return newLoader(getBaseUrl()).loadClass(FinalizableReferenceQueue.FINALIZER_CLASS_NAME);
|
|
} catch (Exception e) {
|
|
FinalizableReferenceQueue.logger.log(Level.WARNING, LOADING_ERROR, (Throwable) e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public URLClassLoader newLoader(URL url) {
|
|
return new URLClassLoader(new URL[]{url}, null);
|
|
}
|
|
}
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
public static class DirectLoader implements FinalizerLoader {
|
|
@Override // com.google.common.base.FinalizableReferenceQueue.FinalizerLoader
|
|
public Class<?> loadFinalizer() {
|
|
try {
|
|
int i = Finalizer.$r8$clinit;
|
|
return Finalizer.class;
|
|
} catch (ClassNotFoundException e) {
|
|
Buffer$$ExternalSyntheticBUOutline0.m((Object) e);
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
public interface FinalizerLoader {
|
|
Class<?> loadFinalizer();
|
|
}
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
public static class SystemLoader implements FinalizerLoader {
|
|
|
|
@VisibleForTesting
|
|
static boolean disabled;
|
|
|
|
@Override // com.google.common.base.FinalizableReferenceQueue.FinalizerLoader
|
|
public Class<?> loadFinalizer() {
|
|
if (disabled) {
|
|
return null;
|
|
}
|
|
try {
|
|
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
|
|
if (systemClassLoader != null) {
|
|
try {
|
|
return systemClassLoader.loadClass(FinalizableReferenceQueue.FINALIZER_CLASS_NAME);
|
|
} catch (ClassNotFoundException unused) {
|
|
}
|
|
}
|
|
return null;
|
|
} catch (SecurityException unused2) {
|
|
FinalizableReferenceQueue.logger.info("Not allowed to access system class loader.");
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public FinalizableReferenceQueue() {
|
|
boolean z;
|
|
ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
|
|
this.queue = referenceQueue;
|
|
PhantomReference<Object> phantomReference = new PhantomReference<>(this, referenceQueue);
|
|
this.frqRef = phantomReference;
|
|
try {
|
|
startFinalizer.invoke(null, FinalizableReference.class, referenceQueue, phantomReference);
|
|
z = true;
|
|
} catch (IllegalAccessException e) {
|
|
Buffer$$ExternalSyntheticBUOutline0.m((Object) e);
|
|
throw null;
|
|
} catch (Throwable th) {
|
|
logger.log(Level.INFO, "Failed to start reference finalizer thread. Reference cleanup will only occur when new references are created.", th);
|
|
z = false;
|
|
}
|
|
this.threadStarted = z;
|
|
}
|
|
|
|
public static Method getStartFinalizer(Class<?> cls) {
|
|
try {
|
|
return cls.getMethod("startFinalizer", Class.class, ReferenceQueue.class, PhantomReference.class);
|
|
} catch (NoSuchMethodException e) {
|
|
Buffer$$ExternalSyntheticBUOutline0.m((Object) e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static Class<?> loadFinalizer(FinalizerLoader... finalizerLoaderArr) {
|
|
for (FinalizerLoader finalizerLoader : finalizerLoaderArr) {
|
|
Class<?> loadFinalizer = finalizerLoader.loadFinalizer();
|
|
if (loadFinalizer != null) {
|
|
return loadFinalizer;
|
|
}
|
|
}
|
|
Buffer$$ExternalSyntheticBUOutline0.m$2();
|
|
return null;
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public void cleanUp() {
|
|
if (this.threadStarted) {
|
|
return;
|
|
}
|
|
while (true) {
|
|
Reference<? extends Object> poll = this.queue.poll();
|
|
if (poll == 0) {
|
|
return;
|
|
}
|
|
poll.clear();
|
|
try {
|
|
((FinalizableReference) poll).finalizeReferent();
|
|
} catch (Throwable th) {
|
|
logger.log(Level.SEVERE, "Error cleaning up after reference.", th);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // java.io.Closeable, java.lang.AutoCloseable
|
|
public void close() {
|
|
this.frqRef.enqueue();
|
|
cleanUp();
|
|
}
|
|
}
|