ice icon indicating copy to clipboard operation
ice copied to clipboard

`slice2java` Should Place `@Generated` Annotation on Generated Code

Open InsertCreativityHere opened this issue 7 months ago • 2 comments

Java has support for an annotation to mark code as 'generated': https://docs.oracle.com/en/java/javase/17/docs/api/java.compiler/javax/annotation/processing/Generated.html

If adding this annotation to the generated code is straightforward and doesn't break anything, we should have slice2java generate this in place of the // <auto-generated> comment it currently generates.

InsertCreativityHere avatar Jun 02 '25 20:06 InsertCreativityHere

I assume we would put this attribute on generated types. And it would not replace the comment-header in the generated files.

bernardnormier avatar Jun 02 '25 20:06 bernardnormier

Yes, I mistakenly thought that this could be applied at a file-level, but it can't. Indeed, we'd generate it on each of the generated types.

InsertCreativityHere avatar Jun 10 '25 18:06 InsertCreativityHere

I'm no longer convinced this is worth doing.

In Java8, this annotation was javax.annotation.Generated, but in Java9+ it was moved into javax.annotation.processing.Generated. And it seems like this type would require users to add requires java.compiler; to their module-info file. I don't think putting this onus on users is worth the incredibly small benefit of properly using @Generated.

Here's the full error you get without require java.compiler;:

/Users/austin/Code/zeroc/ice/java/src/com.zeroc.ice/build/generated/source/slice/main/com/zeroc/Ice/SSLEndpointType.java:11: error: package javax.annotation.processing is not visible
@javax.annotation.processing.Generated(value = "slice2java")
                 ^
  (package javax.annotation.processing is declared in module java.compiler, but module com.zeroc.ice does not read it)

What do other compilers do?

The code-gen tools that still generate @Generated all use 3rd party versions like @jakarta.annotation.Generated or @com.google.protobuf.Generated. So, we could follow suit and offer our own @com.zeroc.Slice.Generated, but I don't think it's worth the effort.

InsertCreativityHere avatar Jul 15 '25 14:07 InsertCreativityHere