In addition to reporting errors as part of your regular compile phase, Error Prone can offer suggested replacements:
error: [DeadException] Exception created but not thrown
new RuntimeException();
^
(see https://errorprone.info/bugpattern/DeadException)
Did you mean 'throw new RuntimeException();'?
1 error
Here, Error Prone is suggesting to fix this issue by prepending a throw
keyword to the code.
While you can, of course, manually make these changes to your source code, you can also use Error Prone to modify the source code with the suggested replacements. This is useful when first adding Error Prone enforcement to an existing codebase, or for fixing warning-level issues that don’t break the build (like MissingOverride or DefaultCharset).
To apply the suggested fixes for checks built in to the Error Prone compiler, you’ll add two compiler flags to your compiler invocation:
-XepPatchChecks:MissingOverride,DefaultCharset,DeadException
-XepPatchLocation:/full/path/to/your/source/root
The first flag determines which checks to try and create fixes from. If a check
doesn’t generally emit suggested fixes (e.g.: InputStreamSlowMultibyteRead),
then it won’t do anything here. The second flag determines where a file named
error-prone.patch will be emitted. This will be a unified diff patch file
relative to that source root. You can inspect the patch file directly, and apply
it to your source with:
cd /full/path/to/your/source/root
patch -p0 -u -i error-prone.patch
To apply the suggested fixes directly, without the intermediate
error-prone.patch files, use -XepPatchChecks as documented above, but use
the special value IN_PLACE for -XepPatchLocation:
-XepPatchChecks:MissingOverride,DefaultCharset,DeadException
-XepPatchLocation:IN_PLACE
Same as before, the first flag determines which checks to try and fix. But the there is will be no patch files generated, for you to manually apply. Error Prone will update the code directly.
NOTE: This feature is experimental, and subject to change. If you have any feedback about this process, please let us know via the Google Group.