Skip to content

Java 9: cannot find symbol javax.annotation.Generated #3633

Closed

Description

@fcnn

/root/grpc-kotlin-hello/proto/src/main/java/io/grpc/examples/helloworld/GreeterGrpc.java:23: error: cannot find symbol
@javax.annotation.Generated(
^
symbol: class Generated
location: package javax.annotation
1 error

FAILURE: Build failed with an exception.

Activity

  1. ejona86 commented on Oct 30, 2017

    @ejona86
    Member

    That class exists in Java 9: https://docs.oracle.com/javase/8/docs/api/javax/annotation/Generated.html . It seems like the problem is with an example that isn't maintained here.

  2. changed the title [-]java 9 not supported yet?[/-] [+]Java 9: cannot find symbol javax.annotation.Generated[/+] on Oct 30, 2017
  3. added a commit that references this issue on Oct 30, 2017
  4. ejona86 commented on Oct 30, 2017

    @ejona86
    Member

    Okay... I've now run into this as well for our example after #3637.

  5. added this to the Next milestone on Oct 30, 2017
  6. ejona86 commented on Oct 30, 2017

    @ejona86
    Member

    For gRPC's build, after #3633 I don't see a problem with @Generated for :compileJava. But I do see it for :javadoc. But in the example @Generated is broken in :compileJava, even if depending on a grpc dep that built fine (e.g., grpc-testing-proto, but installed using Java 8). :-/

  7. ejona86 commented on Oct 30, 2017

    @ejona86
    Member

    @onesea, I'd suggest adding a dependency on javax.annotation:javax.annotation-api for the moment. It's possible to pass --add-modules java.xml.ws.annotation, but it seems it might cause more problems than it solves.

  8. added a commit that references this issue on Oct 30, 2017
  9. davido commented on Mar 8, 2018

    @davido
    Contributor

    I run into the same problem while working on Bazel build with Java 9: bazelbuild/bazel#4709.

  10. davido commented on Mar 8, 2018

    @davido
    Contributor

    The way it was fixed in other java based libraries, like auto-common, auto-value and dagger, is to dynamically select the right annotation to use:

    • Java 9: javax.annotation.processing.Generated
    • Java 8: javax.annotation.Generated

    See e.g.: google/auto@f04406c, google/dagger#882.

  11. ejona86 commented on Mar 8, 2018

    @ejona86
    Member

    @davido, dynamic selection doesn't really seem to be a solution except for annotation processors.

  12. davido commented on Mar 8, 2018

    @davido
    Contributor

    @ejona86 The other two options would be to detect the Java runtime or pass explicit option to gRpc compiler, a lá: --release 9 or similar?

  13. davido commented on Mar 8, 2018

    @davido
    Contributor

    I was able to patch gRpc compiler in Bazel tree, with this one liner:

    diff --git a/third_party/grpc/compiler/src/java_plugin/cpp/java_generator.cpp b/third_party/grpc/compiler/src/java_plugin/cpp/java_generator.cpp
    index 7cb01238ac..3264f15ecf 100644
    --- a/third_party/grpc/compiler/src/java_plugin/cpp/java_generator.cpp
    +++ b/third_party/grpc/compiler/src/java_plugin/cpp/java_generator.cpp
    @@ -1203,7 +1203,7 @@ void GenerateService(const ServiceDescriptor* service,
       vars["NanoUtils"] = "io.grpc.protobuf.nano.NanoUtils";
       vars["StreamObserver"] = "io.grpc.stub.StreamObserver";
       vars["Iterator"] = "java.util.Iterator";
    -  vars["Generated"] = "javax.annotation.Generated";
    +  vars["Generated"] = "javax.annotation.processing.Generated";
       vars["ListenableFuture"] =
           "com.google.common.util.concurrent.ListenableFuture";
       vars["ExperimentalApi"] = "io.grpc.ExperimentalApi";
    

    With this, the generated source is:

    /**
     */
    @javax.annotation.processing.Generated(
        value = "by gRPC proto compiler",
        comments = "Source: src/main/protobuf/command_server.proto")
    public final class CommandServerGrpc {
    
      private CommandServerGrpc() {}
    [...]
    

    And the Bazel build on Java 9 just works:

      $ bazel --host_javabase=/usr/lib64/jvm/java-9-openjdk build --javacopt='--release 9' --java_toolchain=@bazel_tools//tools/jdk:toolchain_jdk9 src:bazel
    [...]
    INFO: From Compiling Java headers src/main/java/com/google/devtools/build/lib/worker/libworker-hjar.jar (16 files):
    WARNING: An illegal reflective access operation has occurred
    WARNING: Illegal reflective access by com.google.protobuf.UnsafeUtil (file:/home/davido/.cache/bazel/_bazel_davido/install/5906941a530f40a3f4749e5cfd148312/_embedded_binaries/embedded_tools/tools/jdk/turbine_deploy.jar) to field java.nio.Buffer.address
    WARNING: Please consider reporting this to the maintainers of com.google.protobuf.UnsafeUtil
    WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
    WARNING: All illegal access operations will be denied in a future release
    INFO: From Building src/main/java/com/google/devtools/build/lib/libbazel-main-class.jar (4 source files) and running annotation processors (OptionProcessor):
    warning: Supported source version 'RELEASE_8' from annotation processor 'com.google.devtools.common.options.processor.OptionProcessor' less than -source '1.9'
    Target //src:bazel up-to-date:
      bazel-bin/src/bazel
    INFO: Elapsed time: 149.130s, Critical Path: 111.80s
    INFO: Build completed successfully, 1986 total actions
    
  14. 14 remaining items

  15. added a commit that references this issue on Mar 10, 2018
  16. davido commented on Apr 9, 2018

    @davido
    Contributor

    @ejona86 As suggested, we added dependency to bazel on javax.annotation and added this as dependency to gRPC in Bazel tool chain, in: [1]. However, it was pointed out in the review, that this problem is going to be fixed upstream? Just wondering, as you said in comment to this issue, that you rather prefer that gRPC users will fix that problem?

    If you are planning to address that problem upstream, do you have any time frame? I'm asking because this issue is very last issue, for full JDK 9 support in Bazel.

  17. ejona86 commented on Apr 9, 2018

    @ejona86
    Member

    I'd have no problem adding the dependency to java_grpc_library. It's been done for Blaze internally, for instance. I simply didn't think about it when I fixed up gradle+Java 9.

    What's the easiest way to test out Bazel+Java 9? It should be as easy as adding a line to added_deps. @davido, if you have a working Bazel+Java 9 environment, I'd be happy to accept a PR.

  18. davido commented on Apr 9, 2018

    @davido
    Contributor

    The easiest way to reproduce the breakage on Bazel+Java 9, is to run this command in grpc-java:

      $ bazel --host_javabase=/usr/lib64/jvm/java-9-openjdk build --javacopt='--release 9' -- 
      java_toolchain=@bazel_tools//tools/jdk:toolchain_jdk9 examples:helloworld_java_grpc
     [...]
    Target //examples:helloworld_java_grpc up-to-date:
      bazel-bin/examples/libhelloworld_java_grpc.jar
    INFO: Elapsed time: 8.263s, Critical Path: 2.72s
    INFO: 217 processes: 212 remote cache hit, 1 linux-sandbox, 3 local, 1 worker.
    INFO: Build completed successfully, 243 total actions
    

    I'm working on a fix and will upload a PR in a moment.

  19. added a commit that references this issue on Apr 9, 2018
    ec645b1
  20. added a commit that references this issue on Apr 26, 2018
    23fcedf
  21. modified the milestones: Next, 1.13 on May 15, 2018
  22. locked as resolved and limited conversation to collaborators on Sep 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions