Skip to content

Commit 104d440

Browse files
committed
Replace magic constants with named exit codes
1 parent b23f159 commit 104d440

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

substratevm/src/com.oracle.svm.agent/src/com/oracle/svm/agent/NativeImageAgent.java

+35-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,11 @@
2424
*/
2525
package com.oracle.svm.agent;
2626

27+
import static com.oracle.svm.agent.NativeImageAgent.ExitCodes.AGENT_ERROR;
28+
import static com.oracle.svm.agent.NativeImageAgent.ExitCodes.PARSE_ERROR;
29+
import static com.oracle.svm.agent.NativeImageAgent.ExitCodes.SUCCESS;
30+
import static com.oracle.svm.agent.NativeImageAgent.ExitCodes.USAGE_ERROR;
31+
2732
import java.io.FileNotFoundException;
2833
import java.io.IOException;
2934
import java.nio.file.AtomicMoveNotSupportedException;
@@ -154,12 +159,12 @@ protected int onLoadCallback(JNIJavaVM vm, JvmtiEnv jvmti, JvmtiEventCallbacks c
154159
for (String token : tokens) {
155160
if (token.startsWith("trace-output=")) {
156161
if (traceOutputFile != null) {
157-
return usage(1, "cannot specify trace-output= more than once.");
162+
return usage("cannot specify trace-output= more than once.");
158163
}
159164
traceOutputFile = getTokenValue(token);
160165
} else if (token.startsWith("config-output-dir=") || token.startsWith("config-merge-dir=")) {
161166
if (configOutputDir != null) {
162-
return usage(1, "cannot specify more than one of config-output-dir= or config-merge-dir=.");
167+
return usage("cannot specify more than one of config-output-dir= or config-merge-dir=.");
163168
}
164169
configOutputDir = transformPath(getTokenValue(token));
165170
if (token.startsWith("config-merge-dir=")) {
@@ -197,12 +202,12 @@ protected int onLoadCallback(JNIJavaVM vm, JvmtiEnv jvmti, JvmtiEventCallbacks c
197202
} else if (token.startsWith("config-write-period-secs=")) {
198203
configWritePeriod = parseIntegerOrNegative(getTokenValue(token));
199204
if (configWritePeriod <= 0) {
200-
return usage(1, "config-write-period-secs must be an integer greater than 0");
205+
return usage("config-write-period-secs must be an integer greater than 0");
201206
}
202207
} else if (token.startsWith("config-write-initial-delay-secs=")) {
203208
configWritePeriodInitialDelay = parseIntegerOrNegative(getTokenValue(token));
204209
if (configWritePeriodInitialDelay < 0) {
205-
return usage(1, "config-write-initial-delay-secs must be an integer greater or equal to 0");
210+
return usage("config-write-initial-delay-secs must be an integer greater or equal to 0");
206211
}
207212
} else if (isBooleanOption(token, "experimental-configuration-with-origins")) {
208213
configurationWithOrigins = getBooleanTokenValue(token);
@@ -215,7 +220,7 @@ protected int onLoadCallback(JNIJavaVM vm, JvmtiEnv jvmti, JvmtiEventCallbacks c
215220
} else if (isBooleanOption(token, "track-reflection-metadata")) {
216221
trackReflectionMetadata = getBooleanTokenValue(token);
217222
} else {
218-
return usage(1, "unknown option: '" + token + "'.");
223+
return usage("unknown option: '" + token + "'.");
219224
}
220225
}
221226

@@ -225,7 +230,7 @@ protected int onLoadCallback(JNIJavaVM vm, JvmtiEnv jvmti, JvmtiEventCallbacks c
225230
}
226231

227232
if (configurationWithOrigins && !conditionalConfigUserPackageFilterFiles.isEmpty()) {
228-
return error(5, "The agent can only be used in either the configuration with origins mode or the predefined classes mode.");
233+
return error(USAGE_ERROR, "The agent can only be used in either the configuration with origins mode or the predefined classes mode.");
229234
}
230235

231236
if (configurationWithOrigins && !mergeConfigs.isEmpty()) {
@@ -250,20 +255,20 @@ protected int onLoadCallback(JNIJavaVM vm, JvmtiEnv jvmti, JvmtiEventCallbacks c
250255
callerFilter = new ComplexFilter(callerFilterHierarchyFilterNode);
251256
}
252257
if (!parseFilterFiles(callerFilter, callerFilterFiles)) {
253-
return 1;
258+
return PARSE_ERROR;
254259
}
255260
}
256261

257262
ComplexFilter accessFilter = null;
258263
if (!accessFilterFiles.isEmpty()) {
259264
accessFilter = new ComplexFilter(AccessAdvisor.copyBuiltinAccessFilterTree());
260265
if (!parseFilterFiles(accessFilter, accessFilterFiles)) {
261-
return 1;
266+
return PARSE_ERROR;
262267
}
263268
}
264269

265270
if (!conditionalConfigUserPackageFilterFiles.isEmpty() && conditionalConfigPartialRun) {
266-
return error(6, "The agent can generate conditional configuration either for the current run or in the partial mode but not both at the same time.");
271+
return error(USAGE_ERROR, "The agent can generate conditional configuration either for the current run or in the partial mode but not both at the same time.");
267272
}
268273

269274
boolean isConditionalConfigurationRun = !conditionalConfigUserPackageFilterFiles.isEmpty() || conditionalConfigPartialRun;
@@ -274,7 +279,7 @@ protected int onLoadCallback(JNIJavaVM vm, JvmtiEnv jvmti, JvmtiEventCallbacks c
274279

275280
if (configOutputDir != null) {
276281
if (traceOutputFile != null) {
277-
return usage(1, "can only once specify exactly one of trace-output=, config-output-dir= or config-merge-dir=.");
282+
return usage("can only once specify exactly one of trace-output=, config-output-dir= or config-merge-dir=.");
278283
}
279284
try {
280285
configOutputDirPath = Files.createDirectories(Path.of(configOutputDir));
@@ -289,7 +294,7 @@ protected int onLoadCallback(JNIJavaVM vm, JvmtiEnv jvmti, JvmtiEventCallbacks c
289294
} catch (Exception ignored) {
290295
process = "(unknown)";
291296
}
292-
return error(2, "Output directory '" + configOutputDirPath + "' is locked by process " + process + ", " +
297+
return error(AGENT_ERROR, "Output directory '" + configOutputDirPath + "' is locked by process " + process + ", " +
293298
"which means another agent instance is already writing to this directory. " +
294299
"Only one agent instance can safely write to a specific target directory at the same time. " +
295300
"Unless file '" + ConfigurationFile.LOCK_FILE_NAME + "' is a leftover from an earlier process that terminated abruptly, it is unsafe to delete it. " +
@@ -322,13 +327,13 @@ protected int onLoadCallback(JNIJavaVM vm, JvmtiEnv jvmti, JvmtiEventCallbacks c
322327
} else {
323328
ComplexFilter userCodeFilter = new ComplexFilter(HierarchyFilterNode.createRoot());
324329
if (!parseFilterFiles(userCodeFilter, conditionalConfigUserPackageFilterFiles)) {
325-
return 2;
330+
return PARSE_ERROR;
326331
}
327332
ComplexFilter classNameFilter;
328333
if (!conditionalConfigClassNameFilterFiles.isEmpty()) {
329334
classNameFilter = new ComplexFilter(HierarchyFilterNode.createRoot());
330335
if (!parseFilterFiles(classNameFilter, conditionalConfigClassNameFilterFiles)) {
331-
return 3;
336+
return PARSE_ERROR;
332337
}
333338
} else {
334339
classNameFilter = new ComplexFilter(HierarchyFilterNode.createInclusiveRoot());
@@ -360,16 +365,16 @@ protected int onLoadCallback(JNIJavaVM vm, JvmtiEnv jvmti, JvmtiEventCallbacks c
360365
}
361366
expectedConfigModifiedBefore = getMostRecentlyModified(configOutputDirPath, getMostRecentlyModified(configOutputLockFilePath, null));
362367
} catch (Throwable t) {
363-
return error(2, t.toString());
368+
return error(AGENT_ERROR, t.toString());
364369
}
365-
} else if (traceOutputFile != null) {
370+
} else {
366371
try {
367372
Path path = Paths.get(transformPath(traceOutputFile));
368373
TraceFileWriter writer = new TraceFileWriter(path);
369374
tracer = writer;
370375
tracingResultWriter = writer;
371376
} catch (Throwable t) {
372-
return error(2, t.toString());
377+
return error(AGENT_ERROR, t.toString());
373378
}
374379
}
375380

@@ -381,16 +386,16 @@ protected int onLoadCallback(JNIJavaVM vm, JvmtiEnv jvmti, JvmtiEventCallbacks c
381386
BreakpointInterceptor.onLoad(jvmti, callbacks, tracer, this, interceptedStateSupplier,
382387
experimentalClassLoaderSupport, experimentalClassDefineSupport, experimentalUnsafeAllocationSupport, trackReflectionMetadata);
383388
} catch (Throwable t) {
384-
return error(3, t.toString());
389+
return error(AGENT_ERROR, t.toString());
385390
}
386391
try {
387392
JniCallInterceptor.onLoad(tracer, this, interceptedStateSupplier);
388393
} catch (Throwable t) {
389-
return error(4, t.toString());
394+
return error(AGENT_ERROR, t.toString());
390395
}
391396

392397
setupExecutorServiceForPeriodicConfigurationCapture(configWritePeriod, configWritePeriodInitialDelay);
393-
return 0;
398+
return SUCCESS;
394399
}
395400

396401
private static void inform(String message) {
@@ -408,11 +413,11 @@ private static <T> T error(T result, String message) {
408413
return result;
409414
}
410415

411-
private static <T> T usage(T result, String message) {
416+
private static int usage(String message) {
412417
inform(message);
413418
inform("Example usage: -agentlib:native-image-agent=config-output-dir=/path/to/config-dir/");
414419
inform("For details, please read AutomaticMetadataCollection.md or https://www.graalvm.org/dev/reference-manual/native-image/metadata/AutomaticMetadataCollection/");
415-
return result;
420+
return USAGE_ERROR;
416421
}
417422

418423
private static AccessAdvisor createAccessAdvisor(boolean builtinHeuristicFilter, ConfigurationFilter callerFilter, ConfigurationFilter accessFilter) {
@@ -689,7 +694,14 @@ protected int onUnloadCallback(JNIJavaVM vm) {
689694
* The epilogue of this method does not tear down our VM: we don't seem to observe all
690695
* threads that end and therefore can't detach them, so we would wait forever for them.
691696
*/
692-
return 0;
697+
return SUCCESS;
698+
}
699+
700+
static class ExitCodes {
701+
static final int SUCCESS = 0;
702+
static final int USAGE_ERROR = 1;
703+
static final int PARSE_ERROR = 2;
704+
static final int AGENT_ERROR = 3;
693705
}
694706

695707
@SuppressWarnings("unused")

0 commit comments

Comments
 (0)