question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Annotate generated sources with @Generated?

See original GitHub issue

I’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:

  1. don’t use spring cloud contract;
  2. don’t use the error prone compile plugin;
  3. 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;
  4. configure error prone to ignore the generated source code from spring cloud contract (e.g. via -XepExcludedPaths:.*/generated-test-sources/.*);
  5. 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
skingslandcommented, Aug 15, 2020

Yeah, I actually considered doing exactly that, thanks! That’s what I was referring to when I wrote:

  1. 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;

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.

1reaction
marcingrzejszczakcommented, Aug 6, 2020

We could link it with https://github.com/spring-cloud/spring-cloud-contract/issues/1401 and automatically add the Generated annotation to be added.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found