GeezerVison is showing github.com — [java] Improve DoNotCallSystemExit: permit call in main(), flag System.halt · Issue #2157 · pmd/pmd
[java] Improve DoNotCallSystemExit: permit call in main(), flag System.halt #2157DescriptionAffects PMD Version:
6.20.0.
Rule:
DoNotCallSystemExit
Description:
This rule must make an exception if the exit methods are called from main(). This rule could add a property to dissuade System.exit() being called from any method other than main().
package pmdtests;
import java.util.concurrent.ThreadLocalRandom;
public final class ThrowSwitch {
private ThrowSwitch() {
throw new IllegalStateException("Private constructor");
}
@SuppressWarnings({"fallthrough", "PMD.MissingBreakInSwitch"})
public static void main(String... args) {
int errCode = getSimulatedErrorCode();
switch (errCode) {
case 1:
case 2:
case 3:
case 4:
case 5:
throw new AssertionError("Error code: " + errCode);
default:
System.exit(1);
}
}
public static int getSimulatedErrorCode() {
return ThreadLocalRandom.current().nextInt(0, 6);
}
private static void anotherExit() {
System.exit(1);
}
}
Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other] Activitylinusjf commented on Aug 4, 2020 adangel commented on Sep 26, 2020
Ideally, calling halt should flag the violation with a higher severity than normal. Is that possible with PMD?
Nope, that's not possible - unless we create two separate rules. Then you can overwrite the priority for each separately. linusjf commented on Sep 26, 2020 Any plans to support multiple instances of a rule, differentiated by id, like Checkstyle does? The different configurations then make them two separate rules---in effect.
I'm also inclined to agree with @oowekyala that at some point all the Xpath rules are better if rewritten in Java. That also makes them more amenable to configuration.
PS: I know it seems like a silly query, but frankly, do any of you respond when it isn't? adangel commented on Oct 3, 2020 @Fernal73 apart from not being able to configure the same rule twice - there is a fundamental problem: If the rule both covers System.exit and System.halt - we have only one rule that does both. Regardless how often you configure it, it always uses the same configuration for both constructs.
If there is really the need to handle System.ext and System.halt differently, then we either need two separate rules or we need the rule to be configurable to either detect only one or both (like adding properties ignoreSystemHalt or ignoreSystemExit).
However, I'm not convinced yet, that we need this additional logic (trying to keep it simple). What's the difference anyway? Either you want to allow terminating the VM in your program or you don't. I don't see, that either of these two methods are less harmful for a web application, so I don't see a point for handling it differently.
I'm more concerned about the name of the rule: Right now, it is called "DoNotCallSystemExit" and it does exactly what it says. But with #2803 we'll extend the very same rule to additionally detect System.halt, so consequently, we should maybe rename the rule as well to "DoNotCallSystemExitOrHalt"? Or more general "DoNotTerminateVM"?
Note: The rule description starts with "Web applications...". If you develop a web application and you are implementing a more complex main method than just calling a web application framework (like spring boot), then you might have actually a different problem - like you are not developing a web application but still have this PMD rule enabled. If you are developing a normal application, there is of course nothing wrong with calling System.exit when you need it. This is just because, if you are developing a web application, your webapp might be run together with other webapps, and if you exit the VM you shutdown all the other webapps that are running in the same application server.
Any plans to support multiple instances of a rule, differentiated by id, like Checkstyle does?
I keep it in mind, but haven't thought it through yet (like many other things) changed the title [-][java] Rule DoNotCallSystemExit must permit call in main()[/-] [+][java] Improve DoNotCallSystemExit: permit call in main(), flag System.halt[/+] on Oct 3, 2020linusjf commented on Oct 3, 2020 The semantic consequences of System.exit and System.halt differ. Hence, the suggestion. System.halt is more egregious.
DoNotTerminateVM is concise. I like my rule names shorter. linusjf commented on Oct 3, 2020 Is it worth considering a flag that denotes that only command line, web or enterprise rules are to be executed? There would need to be a classification of rules as such. The default category would be all. This would be preferable to creating separate sets of rules for different application types. adangel commented on Oct 10, 2020
Is it worth considering a flag that denotes that only command line, web or enterprise rules are to be executed? There would need to be a classification of rules as such. The default category would be all. This would be preferable to creating separate sets of rules for different application types.
We have some ideas for tagging rules etc. But nothing done yet. See also https://github.com/pmd/pmd/wiki/Schema-version-7.0.0
Tagging would be a way to group rules together. I'd not call it a category, because we have well defined 7 8 categories already.
I'll rename the rule to DoNotTerminateVM, as described here: https://pmd.github.io/latest/pmd_devdocs_rule_deprecation_policy.html#renaming-rules added 2 commits that reference this issue on Oct 10, 2020This issue will close once commit 710f97d is merged into the 'main' branch.710f97d This issue will close once commit e76b0b6 is merged into the 'main' branch.e76b0b6 linusjf commented on Oct 11, 2020
Is it worth considering a flag that denotes that only command line, web or enterprise rules are to be executed? There would need to be a classification of rules as such. The default category would be all. This would be preferable to creating separate sets of rules for different application types.
We have some ideas for tagging rules etc. But nothing done yet. See also https://github.com/pmd/pmd/wiki/Schema-version-7.0.0
Tagging would be a way to group rules together. I'd not call it a category, because we have well defined 7 8 categories already.
I'll rename the rule to DoNotTerminateVM, as described here: https://pmd.github.io/latest/pmd_devdocs_rule_deprecation_policy.html#renaming-rules
I think it's a good idea if users wish to retain all the PMD rules in one humongous xml file but wish to retain the flexibility of executing only the rules they need. A property set within the xml file would not quite serve that purpose. Whether you 'tag' it or 'classify' it is immaterial. Those are implementation details.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|