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
Replace javax.annotation.Generated with custom gRPC annotation #9179
Comments
FYI, I just tried upgrading a project from Spring Boot 2.7.4 to 3.0.0-M5. One of the first issues I run into is:
Spring Boot 3 (Spring Framework 6) is moving to Jakarta EE 9 APIs (jakarta.) instead of EE 8 (javax.). Depending on |
Would it be possible to have an option to skip using javax.annotation as described by #9153? |
Our gRPC users in OpenLiberty are affected by this too when using EE9/EE10. The recommended solution is not to bundle an extra compile time [ADDED gh] dependency (why would users want to use Tomcat for this when they are used to OpenLiberty) but to use https://openliberty.io/blog/2021/03/17/eclipse-transformer.html (I would have added this to #9153 but it is locked.) |
IMO, using eclipse-transformer is a workaround. gRPC should support EE9/EE10 too. |
Nobody needs to bundle an extra dependency. annotations-api is a compile-only dependency.
Nobody is depending on Tomcat. It is the annotations-api, which has no actual logic inside. |
I was aware of the retention policy of the annotation, I was referring to #9153 where you say "Our README recommends a compile-time dependency on org.apache.tomcat:annotations-api:6.0.53", by users I meant developers who would need something like the above reference in their build, apologies for my loose language causing any loss of meaning. |
A workaroud for Gradle, add it to implementation 'javax.annotation:javax.annotation-api:1.3.2' |
Hello @ejona86 Is there any news on this issue? With the upgrade to Spring Boot 3, this is something blocking and will affect a lot of projects. It would be nice if this annotation could be configurable or even to be able to skip this annotation. Thank you very much |
I don't really see any new problem here. "Add the dependency like everyone else." You simply were able to side-step the dependency before. I do not recommend bundling; a project that compiles generated code is the one that should have the compile-only dependency. Nothing is broken. Everyone can build. Nobody is blocked. Nothing has changed in my original issue description: before we make any change we need to understand what tools are able to consume. Anyone is free to audit parts of the tooling ecosystem and reported their findings. |
As an application developer, I am developing a JavaEE 9 application and using gRPC. I used this maven project
It is not a solution to use JEE 8 to build this library because it will not be compatible to my JEE9 application. That's why I made this comment |
Compatible? If you add the snippet from the README:
What error do you get? My understanding is that should work without issue. |
I don't understand why I need to add an artifact from 2017, that was renamed, to get my java-grpc application working. Furthermore when javax no longer exists. If you don't have inconvenience, we can contribute to solve this issue. |
It appears ErrorProne accepts any annotation, as long as the name is "Generated": @oburgosm, we'd be happy for you to contribute. But that contribution would need to at least start with investigating what annotations the tooling ecosystem would accept, as mentioned when this issue was opened. |
I use the following workaround to not require to include the useless My project is based on Spring Boot 3. The code generation runs during the For all Maven users, simply add this to your plugins and you are good to go.
If you want to use the Tomcat annotation instead, adapt the hth |
Please ensure that this is not a SOURCE level annotation so that Jacoco can detect it. |
Lombok is an example of a widely used library in java. In the latest version 1.18.30 the user can configure which Generated annotation should be added, or if it should not be added at all. The default behavior is no Generated annotation is added. Key : lombok.addJakartaGeneratedAnnotation Generate @jakarta.annotation.Generated on all generated code (default: false). Examples: Key : lombok.addJavaxGeneratedAnnotation Generate @javax.annotation.Generated on all generated code (default: follow lombok.addGeneratedAnnotation). Examples: @ejona86 as the developer could you possible consider to listen to the request of the users of the library that you are developing and make the Generated annotation configurable similar to Lombok? |
This is all it should take - surprised there are no PRs for this issue? diff which seems to allow for customization of the protoc generated service codediff --git a/compiler/src/java_plugin/cpp/java_generator.cpp b/compiler/src/java_plugin/cpp/java_generator.cpp
index cf8445035..68cea8eb7 100644
--- a/compiler/src/java_plugin/cpp/java_generator.cpp
+++ b/compiler/src/java_plugin/cpp/java_generator.cpp
@@ -1217,7 +1217,8 @@ void PrintImports(Printer* p) {
void GenerateService(const ServiceDescriptor* service,
protobuf::io::ZeroCopyOutputStream* out,
ProtoFlavor flavor,
- bool disable_version) {
+ bool disable_version,
+ bool jakarta_over_javax) {
// All non-generated classes must be referred by fully qualified names to
// avoid collision with generated classes.
std::map<std::string, std::string> vars;
@@ -1249,7 +1250,9 @@ void GenerateService(const ServiceDescriptor* service,
vars["MethodDescriptor"] = "io.grpc.MethodDescriptor";
vars["StreamObserver"] = "io.grpc.stub.StreamObserver";
vars["Iterator"] = "java.util.Iterator";
- vars["Generated"] = "javax.annotation.Generated";
+ vars["Generated"] = jakarta_over_javax
+ ? "javax.annotation.Generated"
+ : "jakarta.annotation.Generated";
vars["GrpcGenerated"] = "io.grpc.stub.annotations.GrpcGenerated";
vars["ListenableFuture"] =
"com.google.common.util.concurrent.ListenableFuture";
diff --git a/compiler/src/java_plugin/cpp/java_generator.h b/compiler/src/java_plugin/cpp/java_generator.h
index f3ec49f8e..4e1f78c9b 100644
--- a/compiler/src/java_plugin/cpp/java_generator.h
+++ b/compiler/src/java_plugin/cpp/java_generator.h
@@ -68,7 +68,8 @@ std::string ServiceClassName(const impl::protobuf::ServiceDescriptor* service);
void GenerateService(const impl::protobuf::ServiceDescriptor* service,
impl::protobuf::io::ZeroCopyOutputStream* out,
ProtoFlavor flavor,
- bool disable_version);
+ bool disable_version,
+ bool jakarta_over_javax);
} // namespace java_grpc_generator
diff --git a/compiler/src/java_plugin/cpp/java_plugin.cpp b/compiler/src/java_plugin/cpp/java_plugin.cpp
index 2eed9d260..9e9a73022 100644
--- a/compiler/src/java_plugin/cpp/java_plugin.cpp
+++ b/compiler/src/java_plugin/cpp/java_plugin.cpp
@@ -59,12 +59,15 @@ class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator {
java_grpc_generator::ProtoFlavor flavor =
java_grpc_generator::ProtoFlavor::NORMAL;
+ bool jakarta_over_javax = false;
bool disable_version = false;
for (size_t i = 0; i < options.size(); i++) {
if (options[i].first == "lite") {
flavor = java_grpc_generator::ProtoFlavor::LITE;
} else if (options[i].first == "noversion") {
disable_version = true;
+ } else if (options[i].first == "jakarta") {
+ jakarta_over_javax = true;
}
}
@@ -77,7 +80,7 @@ class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator {
std::unique_ptr<protobuf::io::ZeroCopyOutputStream> output(
context->Open(filename));
java_grpc_generator::GenerateService(
- service, output.get(), flavor, disable_version);
+ service, output.get(), flavor, disable_version, jakarta_over_javax);
}
return true;
} |
When using the following replacement in gradle: dependencies {
modules {
module("javax.annotation:javax.annotation-api") {
replacedBy("jakarta.annotation:jakarta.annotation-api", "Javax is replaced by Jakarta")
}
}
} Defining a compile-only dependency like I am also surprised that three years after the renaming was decided and two years after it was carried out, there is still no solution in place for such an important project. |
I hope that #10786 will be merged soon; until then, it seems we are blocked. |
"I highly doubt jakarta.annotation.Generated would ever be appropriate, even with it being the new home for the annotation" is simply the wrong point of view, the solution is to reconsider for the sake of the pain your users are going through, like with empathy or something, maybe. |
@tristanlins, that would be a problem. It seems completely broken because the two packages are completely unrelated (yes, they are mirrors of each other, but from the JVM's perspective they have no relationship). Is there other code rewriting combined with that such that the replacement makes sense? Or is that simply a hack to disallow @alexanderankin, @tristanlins is the first person here to describe a problem caused as far as I can tell. The most common complaint is a variation of "I don't like it." |
Ok I am glad that we are making progress on communicating the issue at
least then. I think this generally is how it progresses, first there are
complaints, then there are blockers.
…On Thu, Feb 15, 2024, 10:58 AM Eric Anderson ***@***.***> wrote:
@tristanlins <https://github.com/tristanlins>, that would be a problem.
It seems completely broken because the two packages are completely
unrelated (yes, they are mirrors of each other, but from the JVM's
perspective they have no relationship). Is there other code rewriting
combined with that such that the replacement makes sense? Or is that simply
a hack to disallow javax.annotation:javax.annotation-api?
@alexanderankin <https://github.com/alexanderankin>, @tristanlins
<https://github.com/tristanlins> is the first person here to describe a
*problem* caused as far as I can tell. The most common complaint is a
variation of "I don't like it."
—
Reply to this email directly, view it on GitHub
<#9179 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACECGJGYFRE74PPZVUFU4FTYTYWD7AVCNFSM5WFIC3NKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJUGYZTSNRQG4ZQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
This is not correct, it only applies when comparing javax.annotation-api version 1 and jakarta.annotation-api version 2. However, there is a jakarta.annotation-api version 1, which is an "identical" mirror of javax.annotation-api version 1. The issue arises when you have javax.annotation-api and jakarta.annotation-api, both in version 1.3, (as transitive dependencies) in the classpath. This can lead to sporadic ClassCastExceptions with the message "Cannot cast X to X," especially during annotation scanning. The problem is that javax.annotation-api is not a compile-only dependency. Some annotations have runtime retention, which requires the class to be found in the classpath at runtime. Examples of such annotations are We have not determined exactly why the JVM (or in our case, Tomcat) loaded the same class from the javax.annotation-api JAR sometimes and from the jakarta.annotation-api JAR other times. We only know that this situation can occur. |
@ejona86 I asked a colleague who analyzed it back then. This led to sporadic ClassCastExceptions during Tomcat startup and Tomcat's annotation scanning. Although the classes have the same names, they are NOT compatible with each other. Our only option was to ensure that only one of the two JARs is used. |
Ah, so this is because "Jakarta completely changed the API without changing the Maven package name." It's that jakarta 1 and 2 actually had no relation.
Yeah, you need only one in the classpath at a time. The This issue is still around mostly because nobody's done investigation into the Although I just looked into:
Maybe ErrorProne is the only thing that cares. It isn't an annotation processor and instead integrates itself into the javac processing directly. Few tools do that. Protobuf isn't using any annotation that I can tell, at least going back to 3.8.0. |
) ### Rationale for this change Remove runtime dependencies on [Category B](https://apache.org/legal/resolved.html#category-b) dependencies. ### What changes are included in this PR? - logback: move to test-only - eclipse: remove dependency, vendor the Netty implementation we originally used I wanted to remove javax.annotation.Generated but gRPC doesn't yet let us do that (grpc/grpc-java#9179). That's ~okay though since effectively that's a build only dependency. ### Are these changes tested? #40901 ### Are there any user-facing changes? No. **This PR contains a "Critical Fix".** License issues do not cause runtime issues but are important as an Apache project. * GitHub Issue: #40896 Authored-by: David Li <li.davidm96@gmail.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
apache#40904) Remove runtime dependencies on [Category B](https://apache.org/legal/resolved.html#category-b) dependencies. - logback: move to test-only - eclipse: remove dependency, vendor the Netty implementation we originally used I wanted to remove javax.annotation.Generated but gRPC doesn't yet let us do that (grpc/grpc-java#9179). That's ~okay though since effectively that's a build only dependency. No. **This PR contains a "Critical Fix".** License issues do not cause runtime issues but are important as an Apache project. * GitHub Issue: apache#40896 Authored-by: David Li <li.davidm96@gmail.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
apache#40904) Remove runtime dependencies on [Category B](https://apache.org/legal/resolved.html#category-b) dependencies. - logback: move to test-only - eclipse: remove dependency, vendor the Netty implementation we originally used I wanted to remove javax.annotation.Generated but gRPC doesn't yet let us do that (grpc/grpc-java#9179). That's ~okay though since effectively that's a build only dependency. No. **This PR contains a "Critical Fix".** License issues do not cause runtime issues but are important as an Apache project. * GitHub Issue: apache#40896 Authored-by: David Li <li.davidm96@gmail.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
examples of how to omit the javax annotation here #10927 (comment) |
apache#40904) ### Rationale for this change Remove runtime dependencies on [Category B](https://apache.org/legal/resolved.html#category-b) dependencies. ### What changes are included in this PR? - logback: move to test-only - eclipse: remove dependency, vendor the Netty implementation we originally used I wanted to remove javax.annotation.Generated but gRPC doesn't yet let us do that (grpc/grpc-java#9179). That's ~okay though since effectively that's a build only dependency. ### Are these changes tested? apache#40901 ### Are there any user-facing changes? No. **This PR contains a "Critical Fix".** License issues do not cause runtime issues but are important as an Apache project. * GitHub Issue: apache#40896 Authored-by: David Li <li.davidm96@gmail.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
Since Java 9 dropped
javax.annotation.Generated
users have had to explicitly depend on a dep (typically Tomcat's annotation API) to get the annotation. It'd be nice not to need that the extra dep.But even more important is that the removal of Generated from Java 9 upset the ecosystem as a whole and fragmented it so badly that I believe many tools are no longer assuming they can predict which annotation will be used and are heuristics like "is the annotation named 'Generated'" to determine whether they should it as generated code.
If we do an investigation and find that indeed all the tools we may care about (linters, static analyzers, IDEs) are observing Generated annotations in any package, then we can make our own
io.grpc.Generated
. Unfortunately, I expect theio.grpc.GrpcGenerated
annotation may not suffice because its name is not exactly "Generated." We'll also need to figure out what retention it needs.Tools to investigate (off the top of my head): Error Prone, IntelliJ, Eclipse, Android linter, Find Bugs, Checkstyle. The tools to investigate should be those that may be used by gRPC users, not just those directly used by gRPC maintainers.
javax.annotation.processing.Generated
is not a relevant replacement; see #3633. I highly doubtjakarta.annotation.Generated
would ever be appropriate, even with it being the new home for the annotation; it'd only have an advantage if Nullable goes that way as well, which seems unlikely. But that'd also take investigation of Kotlin and other null-caring tools.The text was updated successfully, but these errors were encountered: