Skip to content

Commit

Permalink
7902930: JOL: Bring back GraphWalker visitors
Browse files Browse the repository at this point in the history
Reviewed-by: shade
  • Loading branch information
s1ck authored and shipilev committed May 21, 2021
1 parent eb9083e commit 517f7e1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class ArrayGraphPathRecord extends GraphPathRecord {
}

@Override
final String path() {
public final String path() {
if (parent != null) {
return parent.path() + "[" + idx + "]";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class FieldGraphPathRecord extends GraphPathRecord {
}

@Override
final String path() {
public final String path() {
if (parent != null) {
return parent.path() + "." + name;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
* @author Aleksey Shipilev
*/
abstract class GraphPathRecord {
public abstract class GraphPathRecord {
protected final GraphPathRecord parent;
private final int depth;
private final Object obj;
Expand All @@ -47,13 +47,13 @@ final Object obj() {
return obj;
}

abstract String path();
public abstract String path();

final Class<?> klass() {
public final Class<?> klass() {
return obj.getClass();
}

final long size() {
public final long size() {
if (size == 0) {
// Object size would not change, fine to compute lazily.
size = VM.current().sizeOf(obj);
Expand Down
15 changes: 11 additions & 4 deletions jol-core/src/main/java/org/openjdk/jol/info/GraphWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
*/
package org.openjdk.jol.info;

import org.openjdk.jol.util.SimpleIdentityHashSet;
import org.openjdk.jol.util.ObjectUtils;
import org.openjdk.jol.util.SimpleIdentityHashSet;
import org.openjdk.jol.util.SimpleStack;
import org.openjdk.jol.vm.VM;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
* Concrete class to walk object graphs.
Expand All @@ -40,9 +39,11 @@
*/
public class GraphWalker extends AbstractGraphWalker {

private final Map<Class<?>, Long> sizeCache;
private final GraphVisitor[] visitors;
private final HashMap<Class<?>, Long> sizeCache;

public GraphWalker() {
public GraphWalker(GraphVisitor... visitor) {
this.visitors = visitor;
sizeCache = new HashMap<>();
}

Expand Down Expand Up @@ -84,6 +85,9 @@ public GraphLayout walk(Object... roots) {
if (e != null && visited.add(e)) {
GraphPathRecord gpr = new ArrayGraphPathRecord(cGpr, i, cGpr.depth() + 1, e);
data.addRecord(gpr);
for (GraphVisitor v : visitors) {
v.visit(gpr);
}
s.push(gpr);
}
}
Expand All @@ -100,6 +104,9 @@ public GraphLayout walk(Object... roots) {
if (e != null && visited.add(e)) {
GraphPathRecord gpr = new FieldGraphPathRecord(cGpr, f.getName(), cGpr.depth() + 1, e);
data.addRecord(gpr);
for (GraphVisitor v : visitors) {
v.visit(gpr);
}
s.push(gpr);
}
}
Expand Down

0 comments on commit 517f7e1

Please sign in to comment.