Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.function.BiConsumer;

/**
* Lazily associate a computed value with (potentially) every {@link ClassLoader}. The computed
Expand Down Expand Up @@ -108,9 +109,37 @@ public final void remove(ClassLoader cl) {
}
}

/** Removes all values computed by this {@code ClassLoaderValue}. */
public final void clear() {
bootValue = null;
systemValue = null;
otherValues.clear();
}

/**
* Visits the values computed by this {@code ClassLoaderValue}, for diagnostic purposes.
*
* @param visitor the visitor of class-loader values
*/
public final void visit(BiConsumer<ClassLoader, V> visitor) {
if (bootValue != null) {
visitor.accept(BOOT_CLASS_LOADER, bootValue);
}
if (systemValue != null) {
visitor.accept(SYSTEM_CLASS_LOADER, systemValue);
}
for (Map.Entry<Object, V> entry : otherValues.entrySet()) {
// skip stale values by checking the associated class-loader
ClassLoader cl = ((ClassLoaderKey) entry.getKey()).get();
if (cl != null) {
visitor.accept(cl, entry.getValue());
}
}
}

/** For testing purposes. */
int size() {
return (null != bootValue ? 1 : 0) + (null != systemValue ? 1 : 0) + otherValues.size();
final int size() {
return (bootValue != null ? 1 : 0) + (systemValue != null ? 1 : 0) + otherValues.size();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
Expand Down Expand Up @@ -35,18 +37,18 @@ void basicOperation() {

assertThat(classLoaderValue.get(cl0)).isEqualTo("0=null");
assertThat(classLoaderValue.get(cl1)).matches("1=.*App.*");
assertThat(classLoaderValue.get(cl2)).matches("2=CL2");
assertThat(classLoaderValue.get(cl3)).matches("3=CL3");
assertThat(classLoaderValue.get(cl4)).matches("4=CL4");
assertThat(classLoaderValue.get(cl5)).matches("5=CL5");
assertThat(classLoaderValue.get(cl2)).isEqualTo("2=CL2");
assertThat(classLoaderValue.get(cl3)).isEqualTo("3=CL3");
assertThat(classLoaderValue.get(cl4)).isEqualTo("4=CL4");
assertThat(classLoaderValue.get(cl5)).isEqualTo("5=CL5");

assertEquals(6, classLoaderValue.size());

// repeated requests should return the same values
assertThat(classLoaderValue.get(cl5)).matches("5=CL5");
assertThat(classLoaderValue.get(cl4)).matches("4=CL4");
assertThat(classLoaderValue.get(cl3)).matches("3=CL3");
assertThat(classLoaderValue.get(cl2)).matches("2=CL2");
assertThat(classLoaderValue.get(cl5)).isEqualTo("5=CL5");
assertThat(classLoaderValue.get(cl4)).isEqualTo("4=CL4");
assertThat(classLoaderValue.get(cl3)).isEqualTo("3=CL3");
assertThat(classLoaderValue.get(cl2)).isEqualTo("2=CL2");
assertThat(classLoaderValue.get(cl1)).matches("1=.*App.*");
assertThat(classLoaderValue.get(cl0)).isEqualTo("0=null");

Expand All @@ -59,10 +61,10 @@ void basicOperation() {
// only the removed value should be replaced
assertThat(classLoaderValue.get(cl0)).isEqualTo("6=null");
assertThat(classLoaderValue.get(cl1)).matches("1=.*App.*");
assertThat(classLoaderValue.get(cl2)).matches("2=CL2");
assertThat(classLoaderValue.get(cl3)).matches("3=CL3");
assertThat(classLoaderValue.get(cl4)).matches("4=CL4");
assertThat(classLoaderValue.get(cl5)).matches("5=CL5");
assertThat(classLoaderValue.get(cl2)).isEqualTo("2=CL2");
assertThat(classLoaderValue.get(cl3)).isEqualTo("3=CL3");
assertThat(classLoaderValue.get(cl4)).isEqualTo("4=CL4");
assertThat(classLoaderValue.get(cl5)).isEqualTo("5=CL5");

assertEquals(6, classLoaderValue.size());

Expand All @@ -73,10 +75,10 @@ void basicOperation() {
// only the removed value should be replaced
assertThat(classLoaderValue.get(cl0)).isEqualTo("6=null");
assertThat(classLoaderValue.get(cl1)).matches("7=.*App.*");
assertThat(classLoaderValue.get(cl2)).matches("2=CL2");
assertThat(classLoaderValue.get(cl3)).matches("3=CL3");
assertThat(classLoaderValue.get(cl4)).matches("4=CL4");
assertThat(classLoaderValue.get(cl5)).matches("5=CL5");
assertThat(classLoaderValue.get(cl2)).isEqualTo("2=CL2");
assertThat(classLoaderValue.get(cl3)).isEqualTo("3=CL3");
assertThat(classLoaderValue.get(cl4)).isEqualTo("4=CL4");
assertThat(classLoaderValue.get(cl5)).isEqualTo("5=CL5");

assertEquals(6, classLoaderValue.size());

Expand All @@ -87,17 +89,23 @@ void basicOperation() {
// only the removed value should be replaced
assertThat(classLoaderValue.get(cl0)).isEqualTo("6=null");
assertThat(classLoaderValue.get(cl1)).matches("7=.*App.*");
assertThat(classLoaderValue.get(cl2)).matches("2=CL2");
assertThat(classLoaderValue.get(cl3)).matches("8=CL3");
assertThat(classLoaderValue.get(cl4)).matches("4=CL4");
assertThat(classLoaderValue.get(cl5)).matches("5=CL5");
assertThat(classLoaderValue.get(cl2)).isEqualTo("2=CL2");
assertThat(classLoaderValue.get(cl3)).isEqualTo("8=CL3");
assertThat(classLoaderValue.get(cl4)).isEqualTo("4=CL4");
assertThat(classLoaderValue.get(cl5)).isEqualTo("5=CL5");

assertEquals(6, classLoaderValue.size());

classLoaderValue.clear();

assertEquals(0, classLoaderValue.size());
}

@Test
@Timeout(30)
void removeStaleEntries() {
Map<ClassLoader, String> visited = new HashMap<>();

ClassLoader cl0 = null;
ClassLoader cl1 = ClassLoader.getSystemClassLoader();
ClassLoader cl2 = newCL("CL2");
Expand All @@ -107,15 +115,30 @@ void removeStaleEntries() {

assertEquals(0, classLoaderValue.size());

classLoaderValue.visit(visited::put);
assertEquals(0, visited.size());
visited.clear();

assertThat(classLoaderValue.get(cl0)).isEqualTo("0=null");
assertThat(classLoaderValue.get(cl1)).matches("1=.*App.*");
assertThat(classLoaderValue.get(cl2)).matches("2=CL2");
assertThat(classLoaderValue.get(cl3)).matches("3=CL3");
assertThat(classLoaderValue.get(cl4)).matches("4=CL4");
assertThat(classLoaderValue.get(cl5)).matches("5=CL5");
assertThat(classLoaderValue.get(cl2)).isEqualTo("2=CL2");
assertThat(classLoaderValue.get(cl3)).isEqualTo("3=CL3");
assertThat(classLoaderValue.get(cl4)).isEqualTo("4=CL4");
assertThat(classLoaderValue.get(cl5)).isEqualTo("5=CL5");

assertEquals(6, classLoaderValue.size());

classLoaderValue.visit(visited::put);
assertThat(visited.values())
.containsExactlyInAnyOrder(
classLoaderValue.get(cl0),
classLoaderValue.get(cl1),
classLoaderValue.get(cl2),
classLoaderValue.get(cl3),
classLoaderValue.get(cl4),
classLoaderValue.get(cl5));
visited.clear();

cl2 = null;
cl4 = null;

Expand All @@ -124,6 +147,15 @@ void removeStaleEntries() {
System.runFinalization();
ClassLoaderValue.removeStaleEntries();
}

classLoaderValue.visit(visited::put);
assertThat(visited.values())
.containsExactlyInAnyOrder(
classLoaderValue.get(cl0),
classLoaderValue.get(cl1),
classLoaderValue.get(cl3),
classLoaderValue.get(cl5));
visited.clear();
}

private static ClassLoader newCL(String name) {
Expand Down