use of com.google.errorprone.InvalidCommandLineOptionException in project error-prone by google.
the class ScannerSupplier method applyOverrides.
/**
* Applies options to this {@link ScannerSupplier}.
*
* <p>Command-line options to override check severities may do any of the following:
* <ul>
* <li>Enable a check that is currently off</li>
* <li>Disable a check that is currently on</li>
* <li>Change the severity of a check that is on, promoting a warning to an error or demoting
* an error to a warning</li>
* </ul>
*
* @param errorProneOptions an {@link ErrorProneOptions} object that encapsulates the overrides
* for this compilation
* @throws InvalidCommandLineOptionException if the override map attempts to disable a check
* that may not be disabled
*/
@CheckReturnValue
public ScannerSupplier applyOverrides(ErrorProneOptions errorProneOptions) throws InvalidCommandLineOptionException {
Map<String, Severity> severityOverrides = errorProneOptions.getSeverityMap();
if (severityOverrides.isEmpty() && !errorProneOptions.isEnableAllChecks() && !errorProneOptions.isDropErrorsToWarnings() && !errorProneOptions.isDisableAllChecks()) {
return this;
}
// Initialize result allChecks map and enabledChecks set with current state of this Supplier.
ImmutableBiMap<String, BugCheckerInfo> checks = getAllChecks();
Map<String, SeverityLevel> severities = new LinkedHashMap<>(severities());
Set<String> disabled = new HashSet<>(disabled());
if (errorProneOptions.isEnableAllChecks()) {
disabled.forEach(c -> severities.put(c, checks.get(c).defaultSeverity()));
disabled.clear();
}
if (errorProneOptions.isDropErrorsToWarnings()) {
getAllChecks().values().stream().filter(c -> c.defaultSeverity() == SeverityLevel.ERROR && c.suppressibility().disableable()).forEach(c -> severities.put(c.canonicalName(), SeverityLevel.WARNING));
}
if (errorProneOptions.isDisableAllChecks()) {
getAllChecks().values().stream().filter(c -> c.suppressibility().disableable()).forEach(c -> disabled.add(c.canonicalName()));
}
// Process overrides
severityOverrides.forEach((checkName, newSeverity) -> {
BugCheckerInfo check = getAllChecks().get(checkName);
if (check == null) {
if (errorProneOptions.ignoreUnknownChecks()) {
return;
}
throw new InvalidCommandLineOptionException(checkName + " is not a valid checker name");
}
switch(newSeverity) {
case OFF:
if (!check.suppressibility().disableable()) {
throw new InvalidCommandLineOptionException(check.canonicalName() + " may not be disabled");
}
severities.remove(check.canonicalName());
disabled.add(check.canonicalName());
break;
case DEFAULT:
severities.put(check.canonicalName(), check.defaultSeverity());
disabled.remove(check.canonicalName());
break;
case WARN:
// Demoting an enabled check from an error to a warning is a form of disabling
if (!disabled().contains(check.canonicalName()) && !check.suppressibility().disableable() && check.defaultSeverity() == SeverityLevel.ERROR) {
throw new InvalidCommandLineOptionException(check.canonicalName() + " is not disableable and may not be demoted to a warning");
}
severities.put(check.canonicalName(), SeverityLevel.WARNING);
disabled.remove(check.canonicalName());
break;
case ERROR:
severities.put(check.canonicalName(), SeverityLevel.ERROR);
disabled.remove(check.canonicalName());
break;
default:
throw new IllegalStateException("Unexpected severity level: " + newSeverity);
}
});
return new ScannerSupplierImpl(checks, ImmutableMap.copyOf(severities), ImmutableSet.copyOf(disabled));
}
use of com.google.errorprone.InvalidCommandLineOptionException in project error-prone by google.
the class RefasterRuleCompiler method run.
private Result run(String[] argv, Context context) {
try {
argv = prepareCompilation(argv, context);
} catch (InvalidCommandLineOptionException e) {
System.err.println(e.getMessage());
System.err.flush();
return Result.CMDERR;
}
try {
Result compileResult = new Main("RefasterRuleCompiler", new PrintWriter(new OutputStreamWriter(System.err, StandardCharsets.UTF_8))).compile(argv, context);
System.err.flush();
return compileResult;
} catch (InvalidCommandLineOptionException e) {
System.err.println(e.getMessage());
System.err.flush();
return Result.CMDERR;
}
}
use of com.google.errorprone.InvalidCommandLineOptionException in project error-prone by google.
the class ScannerSupplierTest method applyOverridesThrowsExceptionWhenDisablingNonDisablableCheck.
@Test
public void applyOverridesThrowsExceptionWhenDisablingNonDisablableCheck() throws Exception {
ScannerSupplier ss = ScannerSupplier.fromBugCheckerClasses(ErrorProneJavaCompilerTest.UnsuppressibleArrayEquals.class);
ErrorProneOptions epOptions = ErrorProneOptions.processArgs(ImmutableList.of("-Xep:ArrayEquals:OFF"));
InvalidCommandLineOptionException exception = expectThrows(InvalidCommandLineOptionException.class, () -> ss.applyOverrides(epOptions));
assertThat(exception.getMessage()).contains("may not be disabled");
}
use of com.google.errorprone.InvalidCommandLineOptionException in project error-prone by google.
the class ScannerSupplierTest method applyOverridesThrowsExceptionWhenDemotingNonDisablableCheck.
@Test
public void applyOverridesThrowsExceptionWhenDemotingNonDisablableCheck() throws Exception {
ScannerSupplier ss = ScannerSupplier.fromBugCheckerClasses(ErrorProneJavaCompilerTest.UnsuppressibleArrayEquals.class);
ErrorProneOptions epOptions = ErrorProneOptions.processArgs(ImmutableList.of("-Xep:ArrayEquals:WARN"));
InvalidCommandLineOptionException exception = expectThrows(InvalidCommandLineOptionException.class, () -> ss.applyOverrides(epOptions));
assertThat(exception.getMessage()).contains("may not be demoted to a warning");
}
Aggregations