Annotate generated sources with @Generated?
See original GitHub issueI’m compiling my artifact with Java 8, and using the Error Prone compiler plugin. It’s generating a warning because of the following generated code:
MockMvcRequestSpecification request = given()
.body(new String(fileToBytes(this, "shouldReturnRenderResponseSuccess_request_renderRequest.json")));
The corresponding section of the contract looks like this:
request {
method POST()
url "/render"
body(file("renderRequest.json")) // this generates the code that fails the error-prone check
The error-prone warning looks like this:
[WARNING] /Users/steve.kingsland/.../My_contractTest.java:[46,11] [DefaultCharset] Implicit use of the platform default charset, which can result in differing behaviour between JVM executions or incorrect behavior if the encoding of the data source doesn't match expectations.
(see https://errorprone.info/bugpattern/DefaultCharset)
Did you mean '.body(new String(fileToBytes(this, "shouldReturnRenderResponseSuccess_request_renderRequest.json"), UTF_8));' or '.body(new String(fileToBytes(this, "shouldReturnRenderResponseSuccess_request_renderRequest.json"), Charset.defaultCharset()));'?
I’ve configured the Maven Java compiler plugin to fail the build if there are warnings, so this fails the build.
I’ve identified the following options to solve this problem:
- don’t use spring cloud contract;
- don’t use the error prone compile plugin;
- use a different charset in the contract than the system default (e.g. ASCII), so it will pass the charset to the
new String()
call and thus won’t trigger an error prone warning; - configure error prone to ignore the generated source code from spring cloud contract (e.g. via
-XepExcludedPaths:.*/generated-test-sources/.*
); - configure error prone to ignore all generated source code, via -XepDisableWarningsInGeneratedCode
Has anyone else run into this problem? I think option 5) is probably my best bet, but it requires that the SCC generated classes are annotated with @javax.annotation.Generated
or @javax.annotation.processing.Generated
. Is that reasonable?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Maven generated-sources/annotations - java - Stack Overflow
Currently I'm having an issue with generating sources from AWS Workflow. I'm using Maven apt-maven-plugin and aspectj-maven-plugin . These ...
Read more >target/generated-sources/annotations and target ... - YouTrack
The main problem is that IntelliJ is trying to recompile the generated sources files in generated-sources/annotations rather than letting the Java compiler ...
Read more >Generated sources and forked instrumentation | Clover
In our project, source gets generated through annotation processors (i.e "transparently" through maven-compiler-plugin). I have seen a couple of bug reports ...
Read more >Working with source generated by an annotation processor
I'm using an annotation processor that creates Java source. Here it is QueryDSL, but I guess it doesn't matter what annotation processor is ......
Read more >Generated (Java Platform SE 7 ) - Oracle Help Center
The Generated annotation is used to mark source code that has been generated. It can also be used to differentiate user written code...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yeah, I actually considered doing exactly that, thanks! That’s what I was referring to when I wrote:
I didn’t do that because it feels like a workaround that’s a bit hacky, and also I wasn’t sure if there would be any characters in the various
renderRequest.json
files that we use in this test that aren’t in the UTF-8 character set, and there might be more than one place . So I ended up using the-XepExcludedPaths:.*/generated-test-sources/.*
approach instead, and that’s working great.We could link it with https://github.com/spring-cloud/spring-cloud-contract/issues/1401 and automatically add the
Generated
annotation to be added.