Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiling generated libraries with Java 9+ fails due to javax.annotation.Generated #888

Open
chanseokoh opened this issue Jan 6, 2022 · 6 comments
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. semver: major Hint for users that this is an API breaking change. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@chanseokoh
Copy link
Contributor

chanseokoh commented Jan 6, 2022

For example, compiling a "self-service" generated library with ./gradlew check fails:

> Task :proto-google-cloud-retail-v2alpha-java:compileJava
/tmp/google-cloud-retail-v2alpha-java/proto-google-cloud-retail-v2alpha-java/src/main/java/com/google/cloud/retail/v2alpha/LocationName.java:27: error: cannot find symbol
import javax.annotation.Generated;

The javax.annotation package space is really a mess from the perspective of Java 9+ JPMS, and it has caused a lot of trouble to many people with Java 9+.

The right solution for Java 9+ is to drop javax.annotation.Generated and migrate to javax.annotation.processing.Generated. However, doing so will cause Java 8 to fail with the same "cannot find symbol" error. An option would be to completely drop using the annotation. However, the gRPC proto compiler generated files with the annotation, so they are out of our control:

@javax.annotation.processing.Generated(
    value = "by gRPC proto compiler",
    comments = "Source: google/cloud/retail/v2alpha/product_service.proto")

A workaround for the users hitting this issue is to declare the following dependency, but this isn't really the right solution.

implementation 'javax.annotation:javax.annotation-api:1.3.2'

Refs:
google/dagger#880
grpc/grpc-java#3633
vavr-io/vavr#2154

@chanseokoh chanseokoh added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. priority: p3 Desirable enhancement or fix. May not be included in next release. labels Jan 6, 2022
@chanseokoh chanseokoh removed the priority: p3 Desirable enhancement or fix. May not be included in next release. label Jan 10, 2022
@chanseokoh
Copy link
Contributor Author

#889 added the dependency as a workaround. Eventually when we drop Java 8, we should migrate our code to use javax.annotation.processing.Generated.

@chanseokoh chanseokoh added the priority: p3 Desirable enhancement or fix. May not be included in next release. label Jan 10, 2022
@vam-google
Copy link
Contributor

@chanseokoh Is this bug still relevant? Keeping this bug open for the Java8 support drop time is probably not practical, as we are guaranteed to not drop it till 2030 at minimum (current end of Java8 Extended Suppport by Oracle https://www.oracle.com/java/technologies/java-se-support-roadmap.html)

@chanseokoh
Copy link
Contributor Author

We need way to track this issue, so that we don't forget to fix it when the time comes.

Are we not allowed to leave a bug open for some set period of time? If so, I'll have no choice but to close it.

@suztomo
Copy link
Member

suztomo commented May 4, 2022

Why it's not happening in Java 11 and Java 17 CIs?

Screen Shot 2022-05-04 at 2 20 41 PM

@chanseokoh
Copy link
Contributor Author

chanseokoh commented May 4, 2022

Let me clarify. The workaround is in place, so things are working fine with any Java version. But the workaround is kinda ugly. The right solution is to drop javax.annotation.Generated and migrate to javax.annotation.processing.Generated after reverting the workaround. However, we can only do that when we stop building with Java 8. This issue is open to track implementing the right solution in the future.

@suztomo
Copy link
Member

suztomo commented May 4, 2022

Glad to hear that the workaround is in place. Thank you for clarification.

Memo: https://docs.oracle.com/javase/9/docs/api/javax/annotation/processing/Generated.html has @Retention(SOURCE).
I thought compiling JDK 11 targeting Java 8 would resolve this issue but it turned out that the "--release 8" flag, which is required to generate Java8-compatible bytecode, prevents us from using the annotation:

suztomo@suztomo2:~/java8-library-and-graal-sdk$ java -version
openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment 18.9 (build 11.0.12+7)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.12+7, mixed mode)
suztomo@suztomo2:~/java8-library-and-graal-sdk$ mvn compile 
...
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ java8-library-and-graal-sdk ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /usr/local/google/home/suztomo/java8-library-and-graal-sdk/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /usr/local/google/home/suztomo/java8-library-and-graal-sdk/src/main/java/org/example/BarGenerated.java:[3,35] cannot find symbol
  symbol:   class Generated
  location: package javax.annotation.processing
[ERROR] /usr/local/google/home/suztomo/java8-library-and-graal-sdk/src/main/java/org/example/BarGenerated.java:[5,2] cannot find symbol
  symbol: class Generated
[INFO] 2 errors 
...
suztomo@suztomo2:~/java8-library-and-graal-sdk$ tail pom.xml 
      <activation>
        <jdk>[9,]</jdk>
      </activation>
      <properties>
        <!-- The flag is only available in JDK 9 or higher -->
        <maven.compiler.release>8</maven.compiler.release>
      </properties>
    </profile>
  </profiles>
</project>

Removing the "--release 8" flag passes the compilation in JDK 11. But this carries the risk of generating Java8-incompatible bytecode.

@blakeli0 blakeli0 added the semver: major Hint for users that this is an API breaking change. label Dec 9, 2022
suztomo pushed a commit that referenced this issue Mar 21, 2023
🤖 I have created a release *beep* *boop*
---


## [3.0.6](https://togithub.com/googleapis/java-shared-dependencies/compare/v3.0.5...v3.0.6) (2022-11-07)


### Dependencies

* Update dependency com.fasterxml.jackson:jackson-bom to v2.14.0 ([#901](https://togithub.com/googleapis/java-shared-dependencies/issues/901)) ([4e3d116](https://togithub.com/googleapis/java-shared-dependencies/commit/4e3d1162403a236443c8dbb00cbe23bd6c6c225a))
* Update dependency com.google.api-client:google-api-client-bom to v2.0.1 ([#899](https://togithub.com/googleapis/java-shared-dependencies/issues/899)) ([d2baed5](https://togithub.com/googleapis/java-shared-dependencies/commit/d2baed57f798b7c153678ce87bd486f9808dbc46))
* Update dependency com.google.api:api-common to v2.2.2 ([#892](https://togithub.com/googleapis/java-shared-dependencies/issues/892)) ([292cd39](https://togithub.com/googleapis/java-shared-dependencies/commit/292cd39d3b5fca9be15621b5e483e3b6386ec2ef))
* Update dependency com.google.cloud:grpc-gcp to v1.3.1 ([#884](https://togithub.com/googleapis/java-shared-dependencies/issues/884)) ([f22fce6](https://togithub.com/googleapis/java-shared-dependencies/commit/f22fce69481f0ecec1c5438b9f1a938db074cf4c))
* Update dependency com.google.code.gson:gson to v2.10 ([#887](https://togithub.com/googleapis/java-shared-dependencies/issues/887)) ([cbe8973](https://togithub.com/googleapis/java-shared-dependencies/commit/cbe8973436da3c34be00d0742b3db11af106f585))
* Update dependency com.google.http-client:google-http-client-bom to v1.42.3 ([#893](https://togithub.com/googleapis/java-shared-dependencies/issues/893)) ([21e7515](https://togithub.com/googleapis/java-shared-dependencies/commit/21e7515d351cf8a53cb7590267231930bedfaf88))
* Update dependency com.google.protobuf:protobuf-bom to v3.21.9 ([#889](https://togithub.com/googleapis/java-shared-dependencies/issues/889)) ([30effe6](https://togithub.com/googleapis/java-shared-dependencies/commit/30effe65dc103a694fab5bf9537e96a0def0d4d9))
* Update dependency io.grpc:grpc-bom to v1.50.2 ([#878](https://togithub.com/googleapis/java-shared-dependencies/issues/878)) ([0e155c4](https://togithub.com/googleapis/java-shared-dependencies/commit/0e155c476ee8280921d234286eed8732997dd2a6))
* Update dependency org.checkerframework:checker-qual to v3.27.0 ([#896](https://togithub.com/googleapis/java-shared-dependencies/issues/896)) ([f6c1155](https://togithub.com/googleapis/java-shared-dependencies/commit/f6c1155bbd0a01b6a25948f7c6117a50fd85e9de))
* Update dependency org.threeten:threetenbp to v1.6.4 ([#894](https://togithub.com/googleapis/java-shared-dependencies/issues/894)) ([478ae53](https://togithub.com/googleapis/java-shared-dependencies/commit/478ae530c8140d92f4e083c5e06ecd6f4f228f05))
* Update gax.version to v2.19.5 ([#903](https://togithub.com/googleapis/java-shared-dependencies/issues/903)) ([ba1ae38](https://togithub.com/googleapis/java-shared-dependencies/commit/ba1ae389185f2fffaec10cf69ad6644389af9571))
* Update google.common-protos.version to v2.10.0 ([#900](https://togithub.com/googleapis/java-shared-dependencies/issues/900)) ([46aeddf](https://togithub.com/googleapis/java-shared-dependencies/commit/46aeddfe2ce2325ab8ae9a6654c500f847855acb))
* Update google.core.version to v2.8.23 ([#885](https://togithub.com/googleapis/java-shared-dependencies/issues/885)) ([1092bbe](https://togithub.com/googleapis/java-shared-dependencies/commit/1092bbe5f7a9d84dc1013f8a3c8e62e5c26ab2ab))
* Update google.core.version to v2.8.24 ([#890](https://togithub.com/googleapis/java-shared-dependencies/issues/890)) ([70791a5](https://togithub.com/googleapis/java-shared-dependencies/commit/70791a5ce678c5c7ebbb70e1527cab69587307ec))
* Update google.core.version to v2.8.27 ([#902](https://togithub.com/googleapis/java-shared-dependencies/issues/902)) ([a53f404](https://togithub.com/googleapis/java-shared-dependencies/commit/a53f404799ac6fd4e3005ea781f74a245fd7b7c7))
* Update iam.version to v1.6.6 ([#886](https://togithub.com/googleapis/java-shared-dependencies/issues/886)) ([122cf9d](https://togithub.com/googleapis/java-shared-dependencies/commit/122cf9d01a73d78768544b0638efc8cca995fd87))
* Update iam.version to v1.6.7 ([#895](https://togithub.com/googleapis/java-shared-dependencies/issues/895)) ([feda2e7](https://togithub.com/googleapis/java-shared-dependencies/commit/feda2e7d2d9026dffcdfe71f443199505e5bb215))

---
This PR was generated with [Release Please](https://togithub.com/googleapis/release-please). See [documentation](https://togithub.com/googleapis/release-please#release-please).
suztomo pushed a commit that referenced this issue Mar 21, 2023
🤖 I have created a release *beep* *boop*
---


## [2.8.6](googleapis/java-core@v2.8.5...v2.8.6) (2022-08-02)


### Dependencies

* update dependency com.google.api:gax-bom to v2.18.7 ([#890](googleapis/java-core#890)) ([9412848](googleapis/java-core@9412848))
* update dependency com.google.code.gson:gson to v2.9.1 ([#888](googleapis/java-core#888)) ([ea6f1a2](googleapis/java-core@ea6f1a2))

---
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. semver: major Hint for users that this is an API breaking change. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

4 participants