Skip to content

Commit fa816d2

Browse files
committed
HeapViewer API improvements
1 parent f122218 commit fa816d2

File tree

6 files changed

+305
-0
lines changed

6 files changed

+305
-0
lines changed

visualvm/heapviewer/nbproject/project.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
<package>org.graalvm.visualvm.heapviewer.model</package>
153153
<package>org.graalvm.visualvm.heapviewer.ui</package>
154154
<package>org.graalvm.visualvm.heapviewer.utils</package>
155+
<package>org.graalvm.visualvm.heapviewer.utils.counters</package>
155156
</friend-packages>
156157
</data>
157158
</configuration>

visualvm/heapviewer/src/org/graalvm/visualvm/heapviewer/java/InstanceNode.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package org.graalvm.visualvm.heapviewer.java;
2727

28+
import java.util.Objects;
2829
import org.graalvm.visualvm.lib.jfluid.heap.GCRoot;
2930
import org.graalvm.visualvm.lib.jfluid.heap.Heap;
3031
import org.graalvm.visualvm.lib.jfluid.heap.Instance;
@@ -176,4 +177,53 @@ protected void setupCopy(InstanceNode copy) {
176177
copy.logicalValue = logicalValue;
177178
}
178179

180+
181+
public static class IncludingNull extends InstanceNode {
182+
183+
public IncludingNull(Instance instance) {
184+
super(instance);
185+
}
186+
187+
public JavaClass getJavaClass() {
188+
if (getInstance() == null) return null;
189+
else return super.getJavaClass();
190+
}
191+
192+
public String getName(Heap heap) {
193+
if (getInstance() == null) return "null"; // NOI18N
194+
else return super.getName(heap);
195+
}
196+
197+
public String getLogicalValue(Heap heap) {
198+
if (getInstance() == null) return DataType.LOGICAL_VALUE.getNoValue();
199+
else return super.getLogicalValue(heap);
200+
}
201+
202+
public long getOwnSize() {
203+
if (getInstance() == null) return DataType.OWN_SIZE.getNoValue();
204+
else return super.getOwnSize();
205+
}
206+
207+
public long getRetainedSize(Heap heap) {
208+
if (getInstance() == null) return DataType.RETAINED_SIZE.valuesAvailable(heap) ?
209+
DataType.RETAINED_SIZE.getNoValue() : DataType.RETAINED_SIZE.getNotAvailableValue();
210+
else return super.getRetainedSize(heap);
211+
}
212+
213+
public boolean equals(Object o) {
214+
if (o == this) return true;
215+
if (!(o instanceof InstanceNode)) return false;
216+
return Objects.equals(getInstance(), ((InstanceNode)o).getInstance());
217+
}
218+
219+
public int hashCode() {
220+
return getInstance() == null ? 37 : super.hashCode();
221+
}
222+
223+
public boolean isLeaf() {
224+
return getInstance() == null ? true : super.isLeaf();
225+
}
226+
227+
}
228+
179229
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package org.graalvm.visualvm.heapviewer.model;
26+
27+
import java.util.Objects;
28+
import org.graalvm.visualvm.lib.jfluid.heap.Heap;
29+
30+
/**
31+
*
32+
* @author Jiri Sedlacek
33+
*/
34+
public class HeapViewerNodeWrapper extends HeapViewerNode {
35+
36+
private final HeapViewerNode node;
37+
38+
39+
public HeapViewerNodeWrapper(HeapViewerNode node) {
40+
this.node = node;
41+
}
42+
43+
44+
public final HeapViewerNode getNode() {
45+
return node;
46+
}
47+
48+
49+
@Override
50+
public boolean equals(Object o) {
51+
if (o == this) return true;
52+
if (!(o instanceof HeapViewerNodeWrapper)) return false;
53+
return Objects.equals(node, ((HeapViewerNodeWrapper)o).node);
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
return node.hashCode();
59+
}
60+
61+
62+
@Override
63+
public String toString() {
64+
return node.toString();
65+
}
66+
67+
68+
@Override
69+
protected <T> T getValue(DataType<T> type, Heap heap) {
70+
return super.getValue(type, heap);
71+
}
72+
73+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package org.graalvm.visualvm.heapviewer.ui;
26+
27+
import javax.accessibility.AccessibleContext;
28+
import javax.swing.JComponent;
29+
import org.graalvm.visualvm.heapviewer.model.HeapViewerNode;
30+
31+
/**
32+
*
33+
* @author Jiri Sedlacek
34+
*/
35+
public abstract class HeapViewerRendererWrapper implements HeapViewerRenderer {
36+
37+
private HeapViewerRenderer renderer;
38+
39+
@Override
40+
public void setValue(Object value, int row) {
41+
HeapViewerNode node = getNode(value);
42+
renderer = getRenderer(node);
43+
renderer.setValue(node, row);
44+
}
45+
46+
@Override
47+
public int getHorizontalAlignment() {
48+
return renderer.getHorizontalAlignment();
49+
}
50+
51+
@Override
52+
public JComponent getComponent() {
53+
return renderer.getComponent();
54+
}
55+
56+
@Override
57+
public void move(int x, int y) {
58+
renderer.move(x, y);
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return renderer.toString();
64+
}
65+
66+
@Override
67+
public String getShortName() {
68+
return renderer.getShortName();
69+
}
70+
71+
@Override
72+
public AccessibleContext getAccessibleContext() {
73+
return renderer.getAccessibleContext();
74+
}
75+
76+
protected abstract HeapViewerNode getNode(Object value);
77+
78+
protected abstract HeapViewerRenderer getRenderer(HeapViewerNode node);
79+
80+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package org.graalvm.visualvm.heapviewer.utils;
26+
27+
import java.util.Iterator;
28+
29+
/**
30+
*
31+
* @author Jiri Sedlacek
32+
*/
33+
public abstract class ExcludingIterator<T> implements Iterator<T> {
34+
35+
private final Iterator<T> iterator;
36+
private T next;
37+
38+
39+
protected ExcludingIterator(Iterator<T> iterator) {
40+
this.iterator = iterator;
41+
computeNext();
42+
}
43+
44+
45+
protected abstract boolean exclude(T item);
46+
47+
48+
@Override
49+
public boolean hasNext() {
50+
return next != null;
51+
}
52+
53+
@Override
54+
public T next() {
55+
T ret = next;
56+
computeNext();
57+
return ret;
58+
}
59+
60+
private void computeNext() {
61+
while (iterator.hasNext()) {
62+
next = iterator.next();
63+
if (!exclude(next)) return;
64+
}
65+
next = null;
66+
}
67+
68+
}

visualvm/heapviewer/src/org/graalvm/visualvm/heapviewer/utils/HeapUtils.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
package org.graalvm.visualvm.heapviewer.utils;
2727

2828
import java.util.Collection;
29+
import java.util.Collections;
2930
import java.util.HashMap;
3031
import java.util.HashSet;
32+
import java.util.Iterator;
3133
import java.util.List;
3234
import java.util.Map;
3335
import org.graalvm.visualvm.lib.jfluid.heap.FieldValue;
@@ -65,6 +67,10 @@ public static Collection<JavaClass> getSubclasses(Heap heap, String baseClass) {
6567
return subclasses;
6668
}
6769

70+
public static Iterator<Instance> instancesIterator(Collection<JavaClass> classes) {
71+
return new InstancesIterator(classes);
72+
}
73+
6874
// public static Map<String, Object> getValuesOfFields(Instance instance, String... fields) {
6975
// Map<String, Object> values = new HashMap();
7076
// for (String field : fields) values.put(field, null);
@@ -186,4 +192,31 @@ public static String htmlize(String text) {
186192

187193
private HeapUtils() {}
188194

195+
196+
private static class InstancesIterator implements Iterator<Instance> {
197+
198+
private final Iterator<JavaClass> classIt;
199+
private Iterator<Instance> instanceIt;
200+
201+
public InstancesIterator(Collection<JavaClass> cls) {
202+
classIt = cls.iterator();
203+
instanceIt = Collections.EMPTY_LIST.iterator();
204+
}
205+
206+
@Override
207+
public boolean hasNext() {
208+
if (instanceIt.hasNext()) return true;
209+
if (!classIt.hasNext()) return false;
210+
211+
instanceIt = classIt.next().getInstancesIterator();
212+
return hasNext();
213+
}
214+
215+
@Override
216+
public Instance next() {
217+
return instanceIt.next();
218+
}
219+
220+
}
221+
189222
}

0 commit comments

Comments
 (0)