3

From what I read about Error Prone, I see that it will actually suggest fixes for style errors in your code. i.e from https://errorprone.info/docs/installation:

ERROR: example/myproject/BUILD:29:1: Java compilation in rule '//example/myproject:hello'
examples/maven/error_prone_should_flag/src/main/java/Main.java:20: error: [DeadException] Exception created but not thrown
    new Exception();
    ^
    (see http://errorprone.info/bugpattern/DeadException)
  Did you mean 'throw new Exception();'?
1 error

What I do not see, is if there is a way to auto-apply these suggested changes. I am running error-prone from the command line. Any and all help is appreciated! Let me know if I can clarify anything.

Tyler R
  • 474
  • 2
  • 6
  • 15

2 Answers2

3

There is not a way to auto-apply them directly.

However, you can get Error Prone to spit out a patch file containing the fixes. Refer to the patching documentation:

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

...

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

(Note the disclaimer about this being experimental)

Community
  • 1
  • 1
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
2

While not current documented, it is possible to directly apply the suggested changes to the affected source code. One does this by passing -XepPatchLocation:IN_PLACE:

Modifying the original example, the invocation then becomes:

-XepPatchChecks:MissingOverride,DefaultCharset,DeadException
-XepPatchLocation:IN_PLACE

It is strongly recommended to use this feature only when the original file is managed by a version control system. The result can then easily be inspected using e.g. git diff and reverted using e.g. git checkout -- ..

Stephan202
  • 59,965
  • 13
  • 127
  • 133