Remove SoftClassReference

They were `get` immediately, and it's unclear
what happens if a reference is GCed and reparsed
later (it may set fields of existing enclosing
classes).
This commit is contained in:
Clément Fournier committed 2022-03-06 14:01:20 +01:00
1 parent 1f008958e8
commit e97e2cde27
2 files changed
+12 -58

No files matched your search

@@ -31,19 +31,19 @@ public class AsmSymbolResolver implements SymbolResolver {
private final Classpath classLoader;
private final SignatureParser typeLoader;
private final ConcurrentMap<String, SoftClassReference> knownStubs = new ConcurrentHashMap<>();
private final ConcurrentMap<String, ClassStub> knownStubs = new ConcurrentHashMap<>();
/**
* Sentinel for when we fail finding a URL. This allows using a single map,
* instead of caching failure cases separately.
*/
private final SoftClassReference failed;
private final ClassStub failed;
public AsmSymbolResolver(TypeSystem ts, Classpath classLoader) {
this.ts = ts;
this.classLoader = classLoader;
this.typeLoader = new SignatureParser(this);
this.failed = new SoftClassReference(this, "/*failed-lookup*/", FailedLoader.INSTANCE, 0);
this.failed = new ClassStub(this, "/*failed-lookup*/", FailedLoader.INSTANCE, 0);
}
@Override
@@ -52,25 +52,23 @@ public class AsmSymbolResolver implements SymbolResolver {
String internalName = getInternalName(binaryName);
SoftClassReference found = knownStubs.computeIfAbsent(internalName, iname -> {
ClassStub found = knownStubs.computeIfAbsent(internalName, iname -> {
@Nullable URL url = getUrlOfInternalName(iname);
if (url == null) {
return failed;
}
return new SoftClassReference(this, iname, new UrlLoader(url), ClassStub.UNKNOWN_ARITY);
return new ClassStub(this, iname, new UrlLoader(url), ClassStub.UNKNOWN_ARITY);
});
if (found == failed) { // NOPMD CompareObjectsWithEquals
return null;
}
ClassStub stub = found.get();
if (!stub.hasCanonicalName()) {
if (!found.hasCanonicalName()) {
// note: this check needs to be done outside of computeIfAbsent
// to prevent recursive updates of the knownStubs map.
knownStubs.put(internalName, failed);
return null;
found = failed;
}
return stub;
return found == failed ? null : found; // NOPMD CompareObjectsWithEquals
}
SignatureParser getSigParser() {
@@ -131,7 +129,7 @@ public class AsmSymbolResolver implements SymbolResolver {
}
@Nullable URL url = getUrlOfInternalName(iname);
Loader loader = url == null ? FailedLoader.INSTANCE : new UrlLoader(url);
return new SoftClassReference(this, iname, loader, observedArity);
}).get();
return new ClassStub(this, iname, loader, observedArity);
});
}
}
@@ -1,44 +0,0 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.symbols.internal.asm;
import java.lang.ref.SoftReference;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* A soft reference over a (possibly loaded class stub).
*/
final class SoftClassReference {
private final Loader loader;
private final String internalName;
private final int observedArity;
private final AsmSymbolResolver resolver;
private SoftReference<ClassStub> ref;
SoftClassReference(AsmSymbolResolver resolver, String internalName, Loader loader, int observedArity) {
this.resolver = resolver;
this.loader = loader;
this.internalName = internalName;
this.observedArity = observedArity;
}
@SuppressWarnings("PMD.AssignmentInOperand")
@NonNull ClassStub get() {
ClassStub c;
if (ref == null || (c = ref.get()) == null) { // SUPPRESS CHECKSTYLE NOW
c = new ClassStub(resolver, internalName, loader, observedArity);
ref = new SoftReference<>(c);
}
return c;
}
@Override
public String toString() {
return internalName + " " + loader;
}
}