-
Notifications
You must be signed in to change notification settings - Fork 3
/
MultiplePath2GCRootsQuery.java
898 lines (779 loc) · 30.6 KB
/
MultiplePath2GCRootsQuery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
/*******************************************************************************
* Copyright (c) 2008, 2023 SAP AG, IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SAP AG - initial API and implementation
* Andrew Johnson (IBM Corporation) - selective expand by class
*******************************************************************************/
package org.eclipse.mat.internal.snapshot.inspections;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.collect.HashMapIntObject;
import org.eclipse.mat.collect.SetInt;
import org.eclipse.mat.internal.Messages;
import org.eclipse.mat.query.Bytes;
import org.eclipse.mat.query.Column;
import org.eclipse.mat.query.ContextProvider;
import org.eclipse.mat.query.IContextObject;
import org.eclipse.mat.query.IContextObjectSet;
import org.eclipse.mat.query.IDecorator;
import org.eclipse.mat.query.IIconProvider;
import org.eclipse.mat.query.IQuery;
import org.eclipse.mat.query.IResult;
import org.eclipse.mat.query.IResultTree;
import org.eclipse.mat.query.ISelectionProvider;
import org.eclipse.mat.query.ResultMetaData;
import org.eclipse.mat.query.annotations.Argument;
import org.eclipse.mat.query.annotations.CommandName;
import org.eclipse.mat.query.annotations.HelpUrl;
import org.eclipse.mat.query.annotations.Icon;
import org.eclipse.mat.query.annotations.Menu;
import org.eclipse.mat.query.annotations.Menu.Entry;
import org.eclipse.mat.snapshot.IMultiplePathsFromGCRootsComputer;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.model.GCRootInfo;
import org.eclipse.mat.snapshot.model.IClass;
import org.eclipse.mat.snapshot.model.IObject;
import org.eclipse.mat.snapshot.model.IObjectArray;
import org.eclipse.mat.snapshot.model.NamedReference;
import org.eclipse.mat.snapshot.query.IHeapObjectArgument;
import org.eclipse.mat.snapshot.query.Icons;
import org.eclipse.mat.util.IProgressListener;
import org.eclipse.mat.util.MessageUtil;
import org.eclipse.mat.util.SimpleMonitor;
import org.eclipse.mat.util.VoidProgressListener;
@CommandName("merge_shortest_paths")
@Icon("/META-INF/icons/mpaths_from_gc.gif")
@Menu( { @Entry(options = "-excludes \"\";"), //
@Entry(options = "-excludes java.lang.ref.WeakReference:referent java.lang.ref.Finalizer:referent,unfinalized java.lang.Runtime:<Unfinalized>;"), //
@Entry(options = "-excludes java.lang.ref.SoftReference:referent;"), //
@Entry(options = "-excludes java.lang.ref.PhantomReference:referent;"), //
@Entry(options = "-excludes java.lang.ref.WeakReference:referent java.lang.ref.Finalizer:referent,unfinalized java.lang.Runtime:<Unfinalized> java.lang.ref.SoftReference:referent;"), //
@Entry(options = "-excludes java.lang.ref.PhantomReference:referent java.lang.ref.SoftReference:referent;"), //
@Entry(options = "-excludes java.lang.ref.PhantomReference:referent java.lang.ref.WeakReference:referent java.lang.ref.Finalizer:referent,unfinalized java.lang.Runtime:<Unfinalized>;"), //
@Entry(options = "-excludes java.lang.ref.Reference:referent java.lang.ref.Finalizer:unfinalized java.lang.Runtime:<Unfinalized>;") //
})
@HelpUrl("/org.eclipse.mat.ui.help/reference/inspections/merge_shortest_paths.html")
public class MultiplePath2GCRootsQuery implements IQuery
{
public enum Grouping
{
FROM_GC_ROOTS(Messages.MultiplePath2GCRootsQuery_Group_FromGCRoots, //
Icons.getURL("mpaths_from_gc.gif")), //$NON-NLS-1$
FROM_GC_ROOTS_BY_CLASS(Messages.MultiplePath2GCRootsQuery_Group_FromGCRootsOnClass, Icons
.getURL("mpaths_from_gc_by_class.gif")), //$NON-NLS-1$
FROM_OBJECTS_BY_CLASS(Messages.MultiplePath2GCRootsQuery_Group_ToGCRoots, Icons
.getURL("mpaths_to_gc_by_class.gif")); //$NON-NLS-1$
String label;
URL icon;
private Grouping(String label, URL icon)
{
this.label = label;
this.icon = icon;
}
public URL getIcon()
{
return icon;
}
public String toString()
{
return label;
}
}
@Argument
public ISnapshot snapshot;
@Argument(flag = Argument.UNFLAGGED)
public IHeapObjectArgument objects;
@Argument(isMandatory = false)
public List<String> excludes = Arrays.asList( //
new String[] { "java.lang.ref.WeakReference:referent", "java.lang.ref.SoftReference:referent" }); //$NON-NLS-1$ //$NON-NLS-2$
@Argument(isMandatory = false)
public Grouping groupBy = Grouping.FROM_GC_ROOTS;
public IResult execute(IProgressListener listener) throws Exception
{
SimpleMonitor monitor = new SimpleMonitor(
MessageUtil.format(Messages.MultiplePath2GCRootsQuery_ProgressName, objects.getLabel()), listener,
new int[] { 20, 100 });
// convert excludes into the required format
Map<IClass, Set<String>> excludeMap = Path2GCRootsQuery.convert(snapshot, excludes);
// calculate the shortest path for each object
IMultiplePathsFromGCRootsComputer computer = snapshot.getMultiplePathsFromGCRoots(objects.getIds(monitor.nextMonitor()),
excludeMap);
Object[] paths = computer.getAllPaths(monitor.nextMonitor());
List<int[]> result = new ArrayList<int[]>(paths.length);
for (int ii = 0; ii < paths.length; ii++)
result.add((int[]) paths[ii]);
if (groupBy == null)
groupBy = Grouping.FROM_GC_ROOTS;
listener.done();
return create(groupBy, snapshot, result);
}
public static Tree create(ISnapshot snapshot, IMultiplePathsFromGCRootsComputer computer, int[] selection)
throws SnapshotException
{
return create(snapshot, computer, selection, new VoidProgressListener());
}
public static Tree create(ISnapshot snapshot, IMultiplePathsFromGCRootsComputer computer, int[] selection, IProgressListener listener)
throws SnapshotException
{
Object[] paths = computer.getAllPaths(listener);
List<int[]> result = new ArrayList<int[]>(paths.length);
for (int ii = 0; ii < paths.length; ii++)
result.add((int[]) paths[ii]);
return selection != null ? new TreeByObjectSelected(snapshot, result, selection) : new TreeByObject(snapshot,
result);
}
/**
* Creates a tree by class.
* @param snapshot
* @param computer
* @param selection list of classes, or null, which are the path to be expanded.
* @param mergeFromRoots
* @param listener
* @return the tree
* @throws SnapshotException
*/
public static Tree create(ISnapshot snapshot, IMultiplePathsFromGCRootsComputer computer, int[] selection, boolean mergeFromRoots, IProgressListener listener)
throws SnapshotException
{
Object[] paths = computer.getAllPaths(listener);
List<int[]> result = new ArrayList<int[]>(paths.length);
for (int ii = 0; ii < paths.length; ii++)
result.add((int[]) paths[ii]);
return selection != null ? new TreeByClassSelected(snapshot, result, selection, mergeFromRoots) : new TreeByClass(snapshot,
result, mergeFromRoots);
}
private static Tree create(Grouping groupBy, ISnapshot snapshot, List<int[]> paths)
{
switch (groupBy)
{
case FROM_GC_ROOTS:
return new TreeByObject(snapshot, paths);
case FROM_GC_ROOTS_BY_CLASS:
return new TreeByClass(snapshot, paths, true);
case FROM_OBJECTS_BY_CLASS:
return new TreeByClass(snapshot, paths, false);
}
return null;
}
public static abstract class Tree implements IResultTree
{
protected ISnapshot snapshot;
protected List<int[]> paths;
protected Tree(ISnapshot snapshot, List<int[]> paths)
{
this.snapshot = snapshot;
this.paths = paths;
}
public ResultMetaData getResultMetaData()
{
return null;
}
public List<?> getElements()
{
return prepare(0, paths);
}
protected abstract List<Node> prepare(int level, List<int[]> paths);
public boolean hasChildren(Object element)
{
// too expensive to calculate
return true;
}
public List<?> getChildren(Object parent)
{
Node node = (Node) parent;
return prepare(node.level + 1, node.paths);
}
public abstract Grouping getGroupedBy();
public Tree groupBy(Grouping groupBy)
{
if (groupBy == getGroupedBy())
return this;
return create(groupBy, snapshot, paths);
}
}
private static class Node
{
int objectId;
int level;
List<int[]> paths = new ArrayList<int[]>();
String attribute;
String label;
String gcRoots;
Bytes shallowHeap;
Bytes refShallowHeap;
Bytes retainedHeap;
private static final Bytes UNSET = new Bytes(-1);
protected Node(int objectId, int level)
{
this.objectId = objectId;
this.level = level;
this.shallowHeap = UNSET;
this.refShallowHeap = UNSET;
this.retainedHeap = UNSET;
}
int[] getReferencedObjects()
{
int[] result = new int[paths.size()];
int ii = 0;
for (int[] path : paths)
result[ii++] = path[0];
return result;
}
/**
* Needed as getElements returns new Nodes each time.
*/
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + level;
result = prime * result + objectId;
return result;
}
/**
* Needed as getElements returns new Nodes each time.
*/
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (level != other.level)
return false;
if (objectId != other.objectId)
return false;
return true;
}
}
private static class ClassNode extends Node
{
private SetInt distinctObjects;
private ClassNode(IClass clazz, int level)
{
super(clazz.getObjectId(), level);
this.label = clazz.getName();
}
public SetInt getDistinctObjects(boolean mergeFromRoots)
{
if (distinctObjects == null) // lazy init
{
distinctObjects = new SetInt();
for (int[] path : paths)
{
int index = mergeFromRoots ? path.length - level - 1 : level;
distinctObjects.add(path[index]);
}
}
return distinctObjects;
}
/**
* Simple equals to satisfy FindBugs.
*/
@Override
public boolean equals(Object o)
{
return super.equals(o);
}
@Override
public int hashCode()
{
return super.hashCode();
}
}
/* package */static class TreeByObject extends Tree implements IIconProvider, IDecorator
{
private TreeByObject(ISnapshot snapshot, List<int[]> paths)
{
super(snapshot, paths);
}
public Column[] getColumns()
{
return new Column[] {
new Column(Messages.Column_ClassName).decorator(this), //
new Column(Messages.MultiplePath2GCRootsQuery_ReferencedObjects, int.class), //
new Column(Messages.Column_ShallowHeap, Bytes.class), //
new Column(Messages.MultiplePath2GCRootsQuery_Column_RefShallowHeap, Bytes.class)
.sorting(Column.SortDirection.DESC), //
new Column(Messages.Column_RetainedHeap, Bytes.class).noTotals() };
}
protected List<Node> prepare(int level, List<int[]> paths)
{
HashMapIntObject<Node> id2node = new HashMapIntObject<Node>();
for (int ii = 0; ii < paths.size(); ii++)
{
int[] path = paths.get(ii);
if (path.length - level > 0)
{
int objectId = path[path.length - level - 1];
Node n = id2node.get(objectId);
if (n == null)
id2node.put(objectId, n = new Node(objectId, level));
n.paths.add(path);
}
}
return Arrays.asList(id2node.getAllValues(new Node[0]));
}
public final Object getColumnValue(Object row, int columnIndex)
{
try
{
Node node = (Node) row;
switch (columnIndex)
{
case 0:
if (node.label == null)
{
IObject obj = snapshot.getObject(node.objectId);
node.label = obj.getDisplayName();
node.shallowHeap = new Bytes(obj.getUsedHeapSize());
}
return node.label;
case 1:
return node.paths.size();
case 2:
if (node.shallowHeap.getValue() == -1)
node.shallowHeap = new Bytes(snapshot.getHeapSize(node.objectId));
return node.shallowHeap;
case 3:
if (node.refShallowHeap.getValue() == -1)
node.refShallowHeap = new Bytes(snapshot.getHeapSize(node.getReferencedObjects()));
return node.refShallowHeap;
case 4:
if (node.retainedHeap.getValue() == -1)
node.retainedHeap = new Bytes(snapshot.getRetainedHeapSize(node.objectId));
return node.retainedHeap;
}
}
catch (SnapshotException e)
{
throw new RuntimeException(e);
}
return null;
}
public IContextObject getContext(final Object row)
{
return new IContextObject()
{
public int getObjectId()
{
return ((Node) row).objectId;
}
};
}
public String prefix(Object row)
{
Node n = (Node) row;
if (n.level > 0 && n.attribute == null)
fillInAttribute(n);
return n.attribute;
}
private void fillInAttribute(Node node)
{
try
{
// get parent object -> it doesn't matter which path
int[] aPath = node.paths.get(0);
IObject heapObject = snapshot.getObject(aPath[aPath.length - node.level]);
long parentAddress = snapshot.mapIdToAddress(node.objectId);
StringBuilder s = new StringBuilder(64);
if (heapObject instanceof IObjectArray)
{
// Arrays can be huge, extracting references could be huge
IObjectArray heapArray = (IObjectArray)heapObject;
int length = heapArray.getLength();
int step = 65536;
int maxarray = 1024 * 1024;
int maxattribute = 150;
boolean big = length > maxarray;
if (big)
{
length = maxarray;
}
for (int i = 0; i < length; i += step)
{
long l[] = heapArray.getReferenceArray(i, Math.min(step, length - i));
for (int j = 0; j < l.length; ++j)
{
if (l[j] == parentAddress)
{
if (s.length() > 0)
s.append(", "); //$NON-NLS-1$
int sl = s.length();
s.append('[');
s.append(i + j);
s.append(']');
if (s.length() > maxattribute)
{
// Remove space after comma?
if (sl > 0)
sl--;
s.delete(sl, s.length());
s.append("..."); //$NON-NLS-1$
big = false;
i = length;
break;
}
}
}
}
if (big)
{
// Don't add ellipsis if nothing else added
if (s.length() > 0)
s.append(",..."); //$NON-NLS-1$
}
}
else
{
List<NamedReference> refs = heapObject.getOutboundReferences();
for (NamedReference reference : refs)
{
if (reference.getObjectAddress() == parentAddress)
{
if (s.length() > 0)
s.append(", "); //$NON-NLS-1$
s.append(reference.getName());
}
}
}
node.attribute = s.toString();
}
catch (SnapshotException e)
{
throw new RuntimeException(e);
}
}
public String suffix(Object row)
{
Node node = (Node) row;
if (node.gcRoots == null && snapshot.isGCRoot(node.objectId))
{
try
{
node.gcRoots = GCRootInfo.getTypeSetAsString(snapshot.getGCRootInfo(node.objectId));
}
catch (SnapshotException e)
{
throw new RuntimeException(e);
}
}
return node.gcRoots;
}
public URL getIcon(Object row)
{
Node n = (Node) row;
return hasChildren(row)
? Icons.outbound(snapshot, n.objectId) : Icons.forObject(snapshot, n.objectId);
}
@Override
public boolean hasChildren(Object row)
{
Node n = (Node) row;
for (int p[] : n.paths)
{
if (p[0] != n.objectId)
return true;
}
return false;
}
@Override
public ResultMetaData getResultMetaData()
{
ResultMetaData.Builder builder = new ResultMetaData.Builder();
builder.addContext(new ContextProvider(Messages.MultiplePath2GCRootsQuery_PathNodeObject) {
@Override
public IContextObject getContext(Object row)
{
return TreeByObject.this.getContext(row);
}
public URL getIcon()
{
return Icons.getURL("heapobjects/out/instance_obj.gif"); //$NON-NLS-1$
}
});
builder.addContext(new ContextProvider(Messages.MultiplePath2GCRootsQuery_ReferencedObjects) {
@Override
public IContextObject getContext(final Object row)
{
return new IContextObjectSet()
{
public int getObjectId()
{
return -1;
}
public int[] getObjectIds()
{
return ((Node) row).getReferencedObjects();
}
public String getOQL()
{
return null;
}
};
}
public URL getIcon()
{
return Icons.OBJECT_INSTANCE;
}
});
return builder.build();
}
@Override
public Grouping getGroupedBy()
{
return Grouping.FROM_GC_ROOTS;
}
}
/* package */static class TreeByObjectSelected extends TreeByObject implements ISelectionProvider
{
int[] selection;
private TreeByObjectSelected(ISnapshot snapshot, List<int[]> paths, int[] selection)
{
super(snapshot, paths);
this.selection = selection;
}
public boolean isExpanded(Object row)
{
Node node = (Node) row;
if (node.level >= selection.length)
return false;
return eval(node);
}
public boolean isSelected(Object row)
{
Node node = (Node) row;
if (node.level != selection.length - 1)
return false;
return eval(node);
}
private boolean eval(Node node)
{
boolean selected = true;
int[] path = node.paths.get(0);
for (int ii = 0; selected && ii < selection.length && ii < node.level; ii++)
selected = selection[ii] == path[path.length - ii - 1];
return selected;
}
}
/* package */static class TreeByClass extends Tree implements IIconProvider
{
boolean mergeFromRoots = true;
private TreeByClass(ISnapshot snapshot, List<int[]> paths, boolean mergeFromRoots)
{
super(snapshot, paths);
this.mergeFromRoots = mergeFromRoots;
}
public Column[] getColumns()
{
return new Column[] {
new Column(Messages.Column_ClassName), //
new Column(Messages.Column_Objects, int.class).noTotals(), //
new Column(Messages.MultiplePath2GCRootsQuery_ReferencedObjects, int.class), //
new Column(Messages.MultiplePath2GCRootsQuery_Column_RefShallowHeap, Bytes.class)
.sorting(Column.SortDirection.DESC) };
}
protected List<Node> prepare(int level, List<int[]> paths)
{
try
{
HashMapIntObject<ClassNode> id2node = new HashMapIntObject<ClassNode>();
for (int ii = 0; ii < paths.size(); ii++)
{
int[] path = paths.get(ii);
if (path.length - level > 0)
{
int objectId = path[mergeFromRoots ? path.length - level - 1 : level];
IClass clazz = snapshot.getClassOf(objectId);
ClassNode n = id2node.get(clazz.getObjectId());
if (n == null)
id2node.put(clazz.getObjectId(), n = new ClassNode(clazz, level));
n.paths.add(path);
}
}
return Arrays.asList(id2node.getAllValues(new Node[0]));
}
catch (SnapshotException e)
{
throw new RuntimeException(e);
}
}
public final Object getColumnValue(Object row, int columnIndex)
{
try
{
ClassNode node = (ClassNode) row;
switch (columnIndex)
{
case 0:
return node.label;
case 1:
return node.getDistinctObjects(mergeFromRoots).size();
case 2:
return node.paths.size();
case 3:
if (node.refShallowHeap.getValue() == -1)
node.refShallowHeap = new Bytes(snapshot.getHeapSize(node.getReferencedObjects()));
return node.refShallowHeap;
}
}
catch (SnapshotException e)
{
throw new RuntimeException(e);
}
return null;
}
public IContextObject getContext(final Object row)
{
return new IContextObjectSet()
{
public int getObjectId()
{
return ((Node) row).objectId;
}
public int[] getObjectIds()
{
return ((ClassNode) row).getDistinctObjects(mergeFromRoots).toArray();
}
public String getOQL()
{
return null;
}
};
}
public URL getIcon(Object row)
{
Node n = (Node) row;
if (mergeFromRoots)
{
if (!hasChildren(row))
return Icons.CLASS;
return Icons.CLASS_OUT;
}
else
return n.level == 0 ? Icons.CLASS : Icons.CLASS_IN;
}
@Override
public boolean hasChildren(Object row)
{
Node n = (Node) row;
for (int p[] : n.paths)
{
if (n.level != p.length - 1)
return true;
}
return false;
}
@Override
public Grouping getGroupedBy()
{
return mergeFromRoots ? Grouping.FROM_GC_ROOTS_BY_CLASS : Grouping.FROM_OBJECTS_BY_CLASS;
}
@Override
public ResultMetaData getResultMetaData()
{
ResultMetaData.Builder builder = new ResultMetaData.Builder();
builder.addContext(new ContextProvider(Messages.Column_Objects) {
@Override
public IContextObject getContext(Object row)
{
return TreeByClass.this.getContext(row);
}
public URL getIcon()
{
return mergeFromRoots ? Icons.CLASS_OUT : Icons.CLASS_IN;
}
});
builder.addContext(new ContextProvider(Messages.MultiplePath2GCRootsQuery_ReferencedObjects) {
@Override
public IContextObject getContext(final Object row)
{
return new IContextObjectSet()
{
public int getObjectId()
{
return -1;
}
public int[] getObjectIds()
{
return ((ClassNode) row).getReferencedObjects();
}
public String getOQL()
{
return null;
}
};
}
public URL getIcon()
{
return Icons.OBJECT_INSTANCE;
}
});
return builder.build();
}
}
/* package */static class TreeByClassSelected extends TreeByClass implements ISelectionProvider
{
int[] selection;
private TreeByClassSelected(ISnapshot snapshot, List<int[]> paths, int[] selection, boolean mergeFromRoots)
{
super(snapshot, paths, mergeFromRoots);
this.selection = selection;
}
public boolean isExpanded(Object row)
{
Node node = (Node) row;
if (node.level >= selection.length)
return false;
return eval(node);
}
/**
* Select if the end of a path.
*/
public boolean isSelected(Object row)
{
Node node = (Node) row;
for (int path[] : node.paths)
{
if (path.length - 1 == node.level)
return true;
}
return false;
}
private boolean eval(Node node)
{
boolean selected = true;
int[] path = node.paths.get(0);
for (int ii = 0; selected && ii < selection.length && ii < node.level; ii++)
{
try
{
selected = selection[ii] == snapshot.getClassOf(path[path.length - ii - 1]).getObjectId();
//selected = selection[ii] == path[path.length - ii - 1];
}
catch (SnapshotException e)
{
selected = false;
}
}
return selected;
}
}
}