Skip to content

Commit f27747d

Browse files
authored
Merge pull request #124 from kgibm/issue123
[Fix #123] Add Select Objects by Field query
2 parents 34251d6 + ebf2d24 commit f27747d

File tree

13 files changed

+226
-1
lines changed

13 files changed

+226
-1
lines changed
314 Bytes
Loading

plugins/org.eclipse.mat.api/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<query impl="org.eclipse.mat.inspections.BiggestObjectsPieQuery"/>
5353
<query impl="org.eclipse.mat.inspections.ClassLoaderExplorerQuery"/>
5454
<query impl="org.eclipse.mat.inspections.GCRootsQuery"/>
55+
<query impl="org.eclipse.mat.inspections.SelectObjectsByFieldQuery"/>
5556

5657
<!-- basics -->
5758
<query impl="org.eclipse.mat.inspections.WasteInCharArraysQuery" />
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 IBM.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* IBM - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.mat.inspections;
14+
15+
import org.eclipse.mat.collect.SetInt;
16+
import org.eclipse.mat.internal.Messages;
17+
import org.eclipse.mat.query.IQuery;
18+
import org.eclipse.mat.query.IResult;
19+
import org.eclipse.mat.query.annotations.Argument;
20+
import org.eclipse.mat.query.annotations.CommandName;
21+
import org.eclipse.mat.query.annotations.HelpUrl;
22+
import org.eclipse.mat.query.annotations.Icon;
23+
import org.eclipse.mat.snapshot.ISnapshot;
24+
import org.eclipse.mat.snapshot.model.IObject;
25+
import org.eclipse.mat.snapshot.query.IHeapObjectArgument;
26+
import org.eclipse.mat.snapshot.query.ObjectListResult;
27+
import org.eclipse.mat.util.IProgressListener;
28+
import org.eclipse.mat.util.MessageUtil;
29+
30+
import com.ibm.icu.text.NumberFormat;
31+
32+
@CommandName("select_objects_by_field")
33+
@Icon("/META-INF/icons/select_objects_by_field.gif")
34+
@HelpUrl("/org.eclipse.mat.ui.help/reference/inspections/select_objects_by_field.html")
35+
public class SelectObjectsByFieldQuery implements IQuery
36+
{
37+
@Argument
38+
public ISnapshot snapshot;
39+
40+
@Argument(flag = Argument.UNFLAGGED)
41+
public IHeapObjectArgument objects;
42+
43+
@Argument(isMandatory = true)
44+
public String field;
45+
46+
public IResult execute(IProgressListener listener) throws Exception
47+
{
48+
int[] objectIds = objects.getIds(listener);
49+
50+
SetInt finalObjects = new SetInt();
51+
listener.beginTask(MessageUtil.format(Messages.SelectObjectsByFieldQuery_Evaluating,
52+
NumberFormat.getNumberInstance().format(objectIds.length)), objectIds.length);
53+
for (int objectId : objectIds)
54+
{
55+
if (listener.isCanceled())
56+
{
57+
break;
58+
}
59+
IObject object = snapshot.getObject(objectId);
60+
Object resolved = object.resolveValue(field);
61+
if (resolved != null && resolved instanceof IObject)
62+
{
63+
finalObjects.add(((IObject) resolved).getObjectId());
64+
}
65+
listener.worked(1);
66+
}
67+
listener.done();
68+
69+
return new ObjectListResult.Outbound(snapshot, finalObjects.toArray());
70+
}
71+
}

plugins/org.eclipse.mat.api/src/org/eclipse/mat/inspections/annotations.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ with the specified name are considered non-existing.
247247
RetainedSetQuery.fieldNames.help = List of field names
248248
RetainedSetQuery.objects.help = Set of objects to include in the analysis.
249249

250+
SelectObjectsByFieldQuery.name = Select Objects by Field
251+
SelectObjectsByFieldQuery.category = Java Basics
252+
SelectObjectsByFieldQuery.help = Selects unique objects by the specified field (supports nesting).
253+
SelectObjectsByFieldQuery.objects.help = Set of objects to process.
254+
SelectObjectsByFieldQuery.field.help = Selects unique objects by the specified field (supports nesting) on the objects to process.
255+
250256
SimpleComparison.name = Simple Comparison
251257
SimpleComparison.category = Java Basics
252258
SimpleComparison.help = A comparison of the results of running a query on two snapshots.

plugins/org.eclipse.mat.api/src/org/eclipse/mat/internal/Messages.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,8 @@ public class Messages extends NLS
586586
public static String RetainedSizeDerivedData_Label_Approximate;
587587
public static String RetainedSizeDerivedData_Label_Precise;
588588

589+
public static String SelectObjectsByFieldQuery_Evaluating;
590+
589591
public static String Service_ErrorMsg_MismatchKeysServices;
590592

591593
public static String ShowInDominatorQuery_ProgressName;

plugins/org.eclipse.mat.api/src/org/eclipse/mat/internal/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ RetainedSizeDerivedData_ErrorMsg_IllegalContextObject=Context provider ''{0}'' r
520520
RetainedSizeDerivedData_ErrorMsg_IllegalObjectId=Context provider ''{0}'' returned an context object with an illegal object id for ''{1}}''. Return null instead.
521521
RetainedSizeDerivedData_Label_Approximate=Calculate Minimum Retained Size (quick approx.)
522522
RetainedSizeDerivedData_Label_Precise=Calculate Precise Retained Size
523+
SelectObjectsByFieldQuery_Evaluating=Evaluating {0} objects
523524
Service_ErrorMsg_MismatchKeysServices=Number of keys does not correspond to the number of values for the service: 0x{0}
524525
ShowInDominatorQuery_ProgressName=Show in Dominator Tree
525526
SimpleComparison_ProgressName=Simple Comparison

plugins/org.eclipse.mat.tests/META-INF/tests/allqueries.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
<query name="org.eclipse.mat.inspections.FindStringsQuery">
115115
<command>find_strings -pattern .*abc.* java.lang.String</command>
116116
</query>
117+
<query name="org.eclipse.mat.inspections.SelectObjectsByFieldQuery">
118+
<command>select_objects_by_field java.lang.Thread -field name</command>
119+
</query>
117120
<query name="org.eclipse.mat.inspections.SystemPropertiesQuery">
118121
<command>system_properties</command>
119122
</query>

plugins/org.eclipse.mat.tests/src/org/eclipse/mat/tests/snapshot/GeneralSnapshotTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,20 @@ public void groupByValue() throws SnapshotException
14361436
assertThat(table.getRowCount(), greaterThan(1));
14371437
}
14381438

1439+
@Test
1440+
public void selectObjectsByField() throws SnapshotException
1441+
{
1442+
SnapshotQuery query = SnapshotQuery.parse("select_objects_by_field java.lang.Thread -field name", snapshot);
1443+
IResult t = query.execute(new CheckedWorkProgressListener(collector));
1444+
assertNotNull(t);
1445+
IResultTree result = (IResultTree)t;
1446+
// Need field names
1447+
assumeThat(snapshot.getSnapshotInfo().getProperty("$heapFormat"), not(equalTo((Serializable)"DTFJ-PHD")));
1448+
assumeThat(snapshot.getSnapshotInfo().getProperty("$heapFormat"), not(equalTo((Serializable)"DTFJ-Javacore")));
1449+
// More than zero results
1450+
assertThat(result.getElements().size(), greaterThan(0));
1451+
}
1452+
14391453
/**
14401454
* Test exporting as HPROF
14411455
* @param compress whether to compress the generated HPROF file
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright (c) 2025 SAP AG and others.
4+
All rights reserved. This program and the accompanying materials
5+
are made available under the terms of the Eclipse Public License 2.0
6+
which accompanies this distribution, and is available at
7+
https://www.eclipse.org/legal/epl-2.0/
8+
9+
SPDX-License-Identifier: EPL-2.0
10+
11+
Contributors:
12+
IBM - initial implementation
13+
-->
14+
<!DOCTYPE reference PUBLIC "-//OASIS//DTD DITA Reference//EN" "reference.dtd" >
15+
<reference id="ref_inspections_select_objects_by_field" xml:lang="en-us">
16+
<title>Select Objects by Field</title>
17+
<shortdesc>Selects unique objects by the specified field (supports field nesting).
18+
</shortdesc>
19+
<prolog>
20+
<copyright>
21+
<copyryear year=""></copyryear>
22+
<copyrholder>
23+
Copyright (c) 2025 SAP AG and others.
24+
All rights reserved. This program and the accompanying materials
25+
are made available under the terms of the Eclipse Public License 2.0
26+
which accompanies this distribution, and is available at
27+
https://www.eclipse.org/legal/epl-2.0/
28+
</copyrholder>
29+
</copyright>
30+
</prolog>
31+
32+
<refbody>
33+
<section>
34+
<title>Motivation</title>
35+
<p>Resolve the field value on a set of objects and retrieve that new set of objects.</p>
36+
</section>
37+
<section>
38+
<title>Arguments</title>
39+
<simpletable>
40+
<sthead>
41+
<stentry>Argument</stentry>
42+
<stentry>Description</stentry>
43+
</sthead>
44+
<strow>
45+
<stentry>objects</stentry>
46+
<stentry>An arbitrary set of objects as input.</stentry>
47+
</strow>
48+
<strow>
49+
<stentry>-field</stentry>
50+
<stentry>A dot notation to specify a field to select on the input objects.</stentry>
51+
</strow>
52+
</simpletable>
53+
</section>
54+
<section id="result">
55+
<title>Result</title>
56+
</section>
57+
</refbody>
58+
</reference>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE html
3+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
5+
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6+
7+
<meta name="generator" content="DITA-OT" /><meta name="DC.type" content="reference" />
8+
<meta name="DC.title" content="Select Objects by Field" />
9+
<meta name="abstract" content="Selects unique objects by the specified field (supports field nesting)." />
10+
<meta name="description" content="Selects unique objects by the specified field (supports field nesting)." />
11+
<meta name="copyright" content="Copyright (c) 2025 SAP AG 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/ " type="primary" />
12+
<meta name="DC.rights.owner" content="Copyright (c) 2025 SAP AG 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/ " type="primary" />
13+
<meta name="DC.format" content="XHTML" />
14+
<meta name="DC.identifier" content="ref_inspections_select_objects_by_field" />
15+
<meta name="DC.language" content="en-us" />
16+
<link rel="stylesheet" type="text/css" href="../../styles/commonltr.css" />
17+
<title>Select Objects by Field</title>
18+
</head>
19+
<body id="ref_inspections_select_objects_by_field">
20+
21+
<h1 class="title topictitle1" id="ariaid-title1">Select Objects by Field</h1>
22+
23+
24+
25+
26+
<div class="body refbody"><p class="shortdesc">Selects unique objects by the specified field (supports field nesting).
27+
</p>
28+
29+
<div class="section"><h2 class="title sectiontitle">Motivation</h2>
30+
31+
<p class="p">Resolve the field value on a set of objects and retrieve that new set of objects.</p>
32+
33+
</div>
34+
35+
<div class="section"><h2 class="title sectiontitle">Arguments</h2>
36+
37+
<table cellpadding="4" cellspacing="0" summary="" border="1" class="simpletable"><col style="width:50%" /><col style="width:50%" /><thead><tr class="sthead">
38+
<th style="vertical-align:bottom;text-align:left;" id="d1790e47" class="stentry">Argument</th>
39+
40+
<th style="vertical-align:bottom;text-align:left;" id="d1790e50" class="stentry">Description</th>
41+
42+
</tr>
43+
</thead><tbody><tr class="strow">
44+
<td style="vertical-align:top;" headers="d1790e47" class="stentry">objects</td>
45+
46+
<td style="vertical-align:top;" headers="d1790e50" class="stentry">An arbitrary set of objects as input.</td>
47+
48+
</tr>
49+
<tr class="strow">
50+
<td style="vertical-align:top;" headers="d1790e47" class="stentry">-field</td>
51+
52+
<td style="vertical-align:top;" headers="d1790e50" class="stentry">A dot notation to specify a field to select on the input objects.</td>
53+
54+
</tr>
55+
</tbody></table>
56+
57+
</div>
58+
59+
<div class="section" id="ref_inspections_select_objects_by_field__result"><h2 class="title sectiontitle">Result</h2>
60+
61+
</div>
62+
63+
</div>
64+
65+
</body>
66+
</html>

0 commit comments

Comments
 (0)