Skip to content

Commit 7416f09

Browse files
committed
Remove agent profile, refine JSON serialization, and replace agent-specific concepts
- Replaced `--agent` and agent-specific schema usage with `--format json` for all JSON output. - Removed `AGENT_SCHEMA_VERSION` and replaced it with `JSON_SCHEMA_VERSION` as `mat-cli/v2`. - Simplified error handling to remove agent-specific methods and unify JSON error serialization. - Improved command suggestions for JSON-only context and streamlined serialization logic. - Updated tests to reflect the removal of `--agent` and agent-dependent concepts.
1 parent 7ddb5fd commit 7416f09

17 files changed

Lines changed: 411 additions & 575 deletions

plugins/org.eclipse.mat.cli/src/org/eclipse/mat/cli/internal/CliApplication.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ public Object start(IApplicationContext context) throws Exception
3636
{
3737
String[] args = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
3838
CliArguments parsed = null;
39-
CliArguments.OutputProfile requestedProfile = parser.detectProfile(args);
40-
CliArguments.OutputFormat requestedFormat = parser.detectFormat(args, requestedProfile);
39+
CliArguments.OutputFormat requestedFormat = parser.detectFormat(args);
4140
try
4241
{
43-
if (requestedProfile == CliArguments.OutputProfile.AGENT)
42+
if (requestedFormat == CliArguments.OutputFormat.JSON)
4443
Locale.setDefault(Locale.ENGLISH);
4544

4645
parsed = parser.parse(args);
@@ -71,32 +70,31 @@ public Object start(IApplicationContext context) throws Exception
7170
catch (CliException e)
7271
{
7372
if (parsed == null)
74-
parsed = parser.partialParse(args, requestedProfile, requestedFormat);
75-
return exit(e.getExitCode(), parsed, requestedProfile, requestedFormat, e, System.err, System.out);
73+
parsed = parser.partialParse(args, requestedFormat);
74+
return exit(e.getExitCode(), parsed, requestedFormat, e, System.err, System.out);
7675
}
7776
catch (OutOfMemoryError e)
7877
{
7978
if (parsed == null)
80-
parsed = parser.partialParse(args, requestedProfile, requestedFormat);
81-
return exit(CliExitCodes.OUT_OF_MEMORY, parsed, requestedProfile, requestedFormat, e, System.err, System.out);
79+
parsed = parser.partialParse(args, requestedFormat);
80+
return exit(CliExitCodes.OUT_OF_MEMORY, parsed, requestedFormat, e, System.err, System.out);
8281
}
8382
catch (Exception e)
8483
{
8584
if (parsed == null)
86-
parsed = parser.partialParse(args, requestedProfile, requestedFormat);
87-
return exit(CliExitCodes.EXECUTION_ERROR, parsed, requestedProfile, requestedFormat, e, System.err, System.out);
85+
parsed = parser.partialParse(args, requestedFormat);
86+
return exit(CliExitCodes.EXECUTION_ERROR, parsed, requestedFormat, e, System.err, System.out);
8887
}
8988
}
9089

91-
private Object exit(int code, CliArguments parsed, CliArguments.OutputProfile requestedProfile,
92-
CliArguments.OutputFormat requestedFormat, Throwable error, PrintStream err, PrintStream out)
90+
private Object exit(int code, CliArguments parsed, CliArguments.OutputFormat requestedFormat, Throwable error,
91+
PrintStream err, PrintStream out)
9392
{
94-
CliArguments.OutputProfile profile = parsed == null ? requestedProfile : parsed.getProfile();
9593
CliArguments.OutputFormat format = parsed == null ? requestedFormat : parsed.getFormat();
9694
boolean json = format == CliArguments.OutputFormat.JSON;
9795
if (json)
9896
{
99-
serializer.serializeError(parsed, profile, code, error, out);
97+
serializer.serializeError(parsed, format, code, error, out);
10098
}
10199
else
102100
{

plugins/org.eclipse.mat.cli/src/org/eclipse/mat/cli/internal/CliArgumentParser.java

Lines changed: 28 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,19 @@ public final class CliArgumentParser
1919
static final int DEFAULT_LIMIT = 20;
2020
static final int MAX_LIMIT = 10000;
2121
static final int DEFAULT_TREE_DEPTH = 8;
22-
static final int DEFAULT_AGENT_TREE_DEPTH = 4;
2322
static final int DEFAULT_INSPECT_OBJECT_TREE_DEPTH = 3;
2423

2524
public CliArguments parse(String[] args) throws CliException
2625
{
2726
if (args == null || args.length == 0)
28-
return new CliArguments(null, null, null, null, CliArguments.OutputProfile.DEFAULT,
29-
CliArguments.OutputFormat.TEXT, false, true, DEFAULT_LIMIT, DEFAULT_TREE_DEPTH, null, null,
30-
null, null, false, null, null);
27+
return new CliArguments(null, null, null, null, CliArguments.OutputFormat.TEXT, false, true,
28+
DEFAULT_LIMIT, DEFAULT_TREE_DEPTH, null, null, null, null, false, null, null);
3129

3230
CliCommand command = null;
3331
CliCommand subjectCommand = null;
3432
String subjectName = null;
3533
File heapFile = null;
36-
CliArguments.OutputProfile profile = CliArguments.OutputProfile.DEFAULT;
3734
CliArguments.OutputFormat format = CliArguments.OutputFormat.TEXT;
38-
boolean formatExplicit = false;
3935
boolean verbose = false;
4036
boolean help = false;
4137
int limit = DEFAULT_LIMIT;
@@ -63,25 +59,23 @@ public CliArguments parse(String[] args) throws CliException
6359
}
6460
else if ("--agent".equals(arg)) //$NON-NLS-1$
6561
{
66-
profile = CliArguments.OutputProfile.AGENT;
62+
throw removedOption("--agent"); //$NON-NLS-1$
6763
}
6864
else if (arg.startsWith("--profile=")) //$NON-NLS-1$
6965
{
70-
profile = CliArguments.OutputProfile.parse(arg.substring("--profile=".length())); //$NON-NLS-1$
66+
throw removedOption("--profile"); //$NON-NLS-1$
7167
}
7268
else if ("--profile".equals(arg)) //$NON-NLS-1$
7369
{
74-
profile = CliArguments.OutputProfile.parse(nextArg(args, ++ii, "--profile")); //$NON-NLS-1$
70+
throw removedOption("--profile"); //$NON-NLS-1$
7571
}
7672
else if (arg.startsWith("--format=")) //$NON-NLS-1$
7773
{
7874
format = CliArguments.OutputFormat.parse(arg.substring("--format=".length())); //$NON-NLS-1$
79-
formatExplicit = true;
8075
}
8176
else if ("--format".equals(arg)) //$NON-NLS-1$
8277
{
8378
format = CliArguments.OutputFormat.parse(nextArg(args, ++ii, "--format")); //$NON-NLS-1$
84-
formatExplicit = true;
8579
}
8680
else if (arg.startsWith("--limit=")) //$NON-NLS-1$
8781
{
@@ -210,22 +204,19 @@ else if (command.requiresSnapshot() && heapFile == null)
210204
}
211205
}
212206

213-
if (profile == CliArguments.OutputProfile.AGENT && !formatExplicit)
214-
format = CliArguments.OutputFormat.JSON;
215-
216207
if (command == null)
217208
{
218209
if (help)
219210
{
220-
return new CliArguments(null, null, null, null, profile, format, verbose, true, limit,
211+
return new CliArguments(null, null, null, null, format, verbose, true, limit,
221212
treeDepthLimit, objectAddress, className, selectField, fieldPath, includeSubclasses,
222213
oqlQuery, queryCommand);
223214
}
224215
throw CliException.usage("Missing command"); //$NON-NLS-1$
225216
}
226217

227218
if (!treeDepthExplicit)
228-
treeDepthLimit = defaultTreeDepth(command, profile);
219+
treeDepthLimit = defaultTreeDepth(command);
229220

230221
if (command == CliCommand.OQL)
231222
oqlQuery = resolveExclusiveInput("oql", "--query", oqlQuery, "--query-file", oqlQueryFile, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
@@ -237,53 +228,14 @@ else if (command == CliCommand.QUERY)
237228
if (!limitExplicit)
238229
limit = defaultLimit(command, queryCommand);
239230

240-
CliArguments parsed = new CliArguments(command, subjectCommand, subjectName, heapFile, profile, format, verbose,
241-
help, limit, treeDepthLimit, objectAddress, className, selectField, fieldPath,
242-
includeSubclasses, oqlQuery, queryCommand);
231+
CliArguments parsed = new CliArguments(command, subjectCommand, subjectName, heapFile, format, verbose, help,
232+
limit, treeDepthLimit, objectAddress, className, selectField, fieldPath, includeSubclasses,
233+
oqlQuery, queryCommand);
243234
validate(parsed);
244235
return parsed;
245236
}
246237

247-
public CliArguments.OutputProfile detectProfile(String[] args)
248-
{
249-
if (args == null)
250-
return CliArguments.OutputProfile.DEFAULT;
251-
252-
CliArguments.OutputProfile profile = CliArguments.OutputProfile.DEFAULT;
253-
for (int ii = 0; ii < args.length; ii++)
254-
{
255-
String arg = args[ii];
256-
if ("--agent".equals(arg)) //$NON-NLS-1$
257-
{
258-
profile = CliArguments.OutputProfile.AGENT;
259-
}
260-
else if (arg.startsWith("--profile=")) //$NON-NLS-1$
261-
{
262-
try
263-
{
264-
profile = CliArguments.OutputProfile.parse(arg.substring("--profile=".length())); //$NON-NLS-1$
265-
}
266-
catch (CliException ignore)
267-
{
268-
return profile;
269-
}
270-
}
271-
else if ("--profile".equals(arg) && ii + 1 < args.length) //$NON-NLS-1$
272-
{
273-
try
274-
{
275-
profile = CliArguments.OutputProfile.parse(args[++ii]);
276-
}
277-
catch (CliException ignore)
278-
{
279-
return profile;
280-
}
281-
}
282-
}
283-
return profile;
284-
}
285-
286-
public CliArguments.OutputFormat detectFormat(String[] args, CliArguments.OutputProfile profile)
238+
public CliArguments.OutputFormat detectFormat(String[] args)
287239
{
288240
if (args != null)
289241
{
@@ -298,7 +250,7 @@ public CliArguments.OutputFormat detectFormat(String[] args, CliArguments.Output
298250
}
299251
catch (CliException ignore)
300252
{
301-
return defaultFormat(profile);
253+
return defaultFormat();
302254
}
303255
}
304256
else if ("--format".equals(arg) && ii + 1 < args.length) //$NON-NLS-1$
@@ -309,12 +261,12 @@ else if ("--format".equals(arg) && ii + 1 < args.length) //$NON-NLS-1$
309261
}
310262
catch (CliException ignore)
311263
{
312-
return defaultFormat(profile);
264+
return defaultFormat();
313265
}
314266
}
315267
}
316268
}
317-
return defaultFormat(profile);
269+
return defaultFormat();
318270
}
319271

320272
private void validate(CliArguments arguments) throws CliException
@@ -414,10 +366,9 @@ private String readStandardInput(String optionName) throws CliException
414366
}
415367
}
416368

417-
private CliArguments.OutputFormat defaultFormat(CliArguments.OutputProfile profile)
369+
private CliArguments.OutputFormat defaultFormat()
418370
{
419-
return profile == CliArguments.OutputProfile.AGENT ? CliArguments.OutputFormat.JSON
420-
: CliArguments.OutputFormat.TEXT;
371+
return CliArguments.OutputFormat.TEXT;
421372
}
422373

423374
private int parseLimit(String value) throws CliException
@@ -462,8 +413,7 @@ private boolean isHelpToken(String token)
462413
return "--help".equals(token) || "-h".equals(token) || "help".equals(token); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
463414
}
464415

465-
public CliArguments partialParse(String[] args, CliArguments.OutputProfile profile,
466-
CliArguments.OutputFormat format)
416+
public CliArguments partialParse(String[] args, CliArguments.OutputFormat format)
467417
{
468418
CliCommand command = null;
469419
CliCommand subjectCommand = null;
@@ -477,7 +427,7 @@ public CliArguments partialParse(String[] args, CliArguments.OutputProfile profi
477427
boolean includeSubclasses = false;
478428
String oqlQuery = null;
479429
String queryCommand = null;
480-
int treeDepthLimit = defaultTreeDepth(null, profile);
430+
int treeDepthLimit = defaultTreeDepth(null);
481431
boolean treeDepthExplicit = false;
482432

483433
if (args != null)
@@ -585,9 +535,9 @@ else if (command.requiresSnapshot() && heapFile == null)
585535
}
586536

587537
if (!treeDepthExplicit)
588-
treeDepthLimit = defaultTreeDepth(command, profile);
538+
treeDepthLimit = defaultTreeDepth(command);
589539

590-
return new CliArguments(command, subjectCommand, subjectName, heapFile, profile, format, false, help,
540+
return new CliArguments(command, subjectCommand, subjectName, heapFile, format, false, help,
591541
defaultLimit(command, queryCommand), treeDepthLimit, objectAddress, className, selectField,
592542
fieldPath, includeSubclasses, oqlQuery, queryCommand);
593543
}
@@ -619,20 +569,19 @@ private String queryIdentifier(String queryCommand)
619569

620570
private boolean expectsValue(String option)
621571
{
622-
return "--profile".equals(option) || "--format".equals(option) || "--limit".equals(option) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
623-
|| "--depth".equals(option) //$NON-NLS-1$
572+
return "--format".equals(option) || "--limit".equals(option) || "--depth".equals(option) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
624573
|| "--object".equals(option) || "--class".equals(option) //$NON-NLS-1$ //$NON-NLS-2$
625574
|| "--select-field".equals(option) || "--field-path".equals(option) //$NON-NLS-1$ //$NON-NLS-2$
626575
|| "--query".equals(option) //$NON-NLS-1$
627576
|| "--query-file".equals(option) //$NON-NLS-1$
628577
|| "--command".equals(option) || "--command-file".equals(option); //$NON-NLS-1$ //$NON-NLS-2$
629578
}
630579

631-
private int defaultTreeDepth(CliCommand command, CliArguments.OutputProfile profile)
580+
private int defaultTreeDepth(CliCommand command)
632581
{
633582
if (command == CliCommand.INSPECT_OBJECT)
634583
return DEFAULT_INSPECT_OBJECT_TREE_DEPTH;
635-
return profile == CliArguments.OutputProfile.AGENT ? DEFAULT_AGENT_TREE_DEPTH : DEFAULT_TREE_DEPTH;
584+
return DEFAULT_TREE_DEPTH;
636585
}
637586

638587
private boolean hasEmptyPathSegment(String fieldPath)
@@ -651,4 +600,9 @@ private int safePartialDepth(String value, int fallback)
651600
return fallback;
652601
}
653602
}
603+
604+
private CliException removedOption(String option)
605+
{
606+
return CliException.usage(option + " has been removed. Use --format json."); //$NON-NLS-1$
607+
}
654608
}

plugins/org.eclipse.mat.cli/src/org/eclipse/mat/cli/internal/CliArguments.java

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,6 @@
1313

1414
public final class CliArguments
1515
{
16-
public enum OutputProfile
17-
{
18-
DEFAULT, AGENT;
19-
20-
public static OutputProfile parse(String value) throws CliException
21-
{
22-
if ("default".equalsIgnoreCase(value)) //$NON-NLS-1$
23-
return DEFAULT;
24-
if ("agent".equalsIgnoreCase(value)) //$NON-NLS-1$
25-
return AGENT;
26-
throw CliException.usage("Unsupported profile: " + value); //$NON-NLS-1$
27-
}
28-
}
29-
3016
public enum OutputFormat
3117
{
3218
TEXT, JSON;
@@ -45,7 +31,6 @@ public static OutputFormat parse(String value) throws CliException
4531
private final CliCommand subjectCommand;
4632
private final String subjectName;
4733
private final File heapFile;
48-
private final OutputProfile profile;
4934
private final OutputFormat format;
5035
private final boolean verbose;
5136
private final boolean help;
@@ -59,7 +44,7 @@ public static OutputFormat parse(String value) throws CliException
5944
private final String oqlQuery;
6045
private final String queryCommand;
6146

62-
CliArguments(CliCommand command, CliCommand subjectCommand, String subjectName, File heapFile, OutputProfile profile,
47+
CliArguments(CliCommand command, CliCommand subjectCommand, String subjectName, File heapFile,
6348
OutputFormat format, boolean verbose, boolean help, int limit, int treeDepthLimit,
6449
String objectAddress, String className, String selectField, String fieldPath,
6550
boolean includeSubclasses, String oqlQuery, String queryCommand)
@@ -68,7 +53,6 @@ public static OutputFormat parse(String value) throws CliException
6853
this.subjectCommand = subjectCommand;
6954
this.subjectName = subjectName;
7055
this.heapFile = heapFile;
71-
this.profile = profile;
7256
this.format = format;
7357
this.verbose = verbose;
7458
this.help = help;
@@ -103,16 +87,6 @@ public File getHeapFile()
10387
return heapFile;
10488
}
10589

106-
public OutputProfile getProfile()
107-
{
108-
return profile;
109-
}
110-
111-
public boolean isAgentProfile()
112-
{
113-
return profile == OutputProfile.AGENT;
114-
}
115-
11690
public OutputFormat getFormat()
11791
{
11892
return format;

0 commit comments

Comments
 (0)