Skip to content

Add @SuppressWarnings to generated classes to fix @Deprecated components#274

Closed
semyon-levin-workato wants to merge 1 commit into
Randgalt:masterfrom
semyon-levin-workato:add-suppress-warnings-to-generated-classes
Closed

Add @SuppressWarnings to generated classes to fix @Deprecated components#274
semyon-levin-workato wants to merge 1 commit into
Randgalt:masterfrom
semyon-levin-workato:add-suppress-warnings-to-generated-classes

Conversation

@semyon-levin-workato

Copy link
Copy Markdown

When a record component is annotated with @Deprecated, the generated builder code references deprecated accessors, causing compilation warnings (or errors with -Werror). This adds @SuppressWarnings({"all", "cast"}) at the class level on all generated classes, interfaces, and records.

Fixes #273.

@semyon-levin-workato

Copy link
Copy Markdown
Author

Hi @Randgalt ,

Is there any chance it gets merged and released? Would you like me to change anything in the PR?

@Randgalt

Copy link
Copy Markdown
Owner

I apologize - I've been slammed at work. I'll look at this in the next day or so.

@Randgalt

Copy link
Copy Markdown
Owner

Hi @Randgalt ,

Is there any chance it gets merged and released? Would you like me to change anything in the PR?

One thing for certain, this should be optional behavior. You should add an option to do this.

@semyon-levin-workato

Copy link
Copy Markdown
Author

I'd like to ask a few questions:

  1. If I understand correctly, the project already adds some @SuppressWarnings for some cases (with collections, if I remember correctly). Personally, I think it's absolutely fine to always suppress all warnings for generated code, but I'm ready to add a configurable option if you insist.
  2. What behaviour would you like to see as default - enabled or disabled?
  3. How would you like it to be tested? I see only one option - use https://github.com/google/compile-testing . Would you suggest anything else? If I understand correctly, tests are validating runtime behaviour currently. Here, we need to test if warnings are reported or not.

@Randgalt

Copy link
Copy Markdown
Owner

I see it one place, it was added by another dev and I don't remember the details tbh. My initial reaction is to ask why this is necessary? I know that Maven ignores the annotations directory. Doesn't Gradle too? What build system causes the problem. Can you describe how I can see the problem locally?

@semyon-levin-workato

semyon-levin-workato commented Jun 18, 2026

Copy link
Copy Markdown
Author

It's not about Maven or Gradle. It's about javac. Basically, a builder generated for this record can't be compiled with -Xlint:all and -Werror:

@RecordBuilder
public record DeprecatedComponentRecord(
  @Deprecated String oldField, // <-- this `@Deprecated` field is used in the generated builder and this cause compilation issues
  String newField
) implements DeprecatedComponentRecordBuilder.With {
}

Don't remember if With is required to fail.

I could suggest to suppress deprecated only, but, from my experience, annotation processors usually just add @SuppressWarnings("all") and don't care about anything else. Examples: Lombok, Immutables.

@Randgalt

Copy link
Copy Markdown
Owner

I did a test where I build DeprecatedComponentRecord without RecordBuilder but with the linter options and it fails:

ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.14.0:compile (default-compile) on project hey: Compilation failure
[ERROR] /Users/jordanzimmerman/dev/oss/soabase/record-builder/hey/src/main/java/temp/DeprecatedComponentRecord.java: warnings found and -Werror specified
[

@semyon-levin-workato

Copy link
Copy Markdown
Author

Are you saying javac reports an error when compiles records with deprecated components? Yes, that can happen. But it can be easily suppressed.

@Randgalt

Copy link
Copy Markdown
Owner

Are you saying javac reports an error when compiles records with deprecated components? Yes, that can happen. But it can be easily suppressed.

Yes. So, whatever you do to suppress the non-Record-Builder case can be done for the Record-Builder case.

@remal

remal commented Jun 18, 2026

Copy link
Copy Markdown

So, whatever you do to suppress the non-Record-Builder case can be done for the Record-Builder case.

Not exactly. The record is written by me. I can add @SuppressWarnings on it. The builder is generated. I can't add @SuppressWarnings there.

@Randgalt

Copy link
Copy Markdown
Owner

So, whatever you do to suppress the non-Record-Builder case can be done for the Record-Builder case.

Not exactly. The record is written by me. I can add @SuppressWarnings on it. The builder is generated. I can't add @SuppressWarnings there.

RecordBuilder copies down any annotations you write/add. If it's not doing it for a particular case we can fix that.

@Randgalt

Randgalt commented Jun 18, 2026

Copy link
Copy Markdown
Owner

For example, if I add @SuppressWarnings("all") @Deprecated String oldField to the record the generated builder has:

private DeprecatedComponentRecordBuilder(@SuppressWarnings("all") @Deprecated String oldField,

@semyon-levin-workato

Copy link
Copy Markdown
Author

It didn't help. I still see these warnings:

[WARNING] DeprecatedComponentRecordBuilder.java:[...] @Deprecated annotation has no effect on this variable declaration
[WARNING] DeprecatedComponentRecordBuilder.java:[...] oldField() in DeprecatedComponentRecord has been deprecated

The project is proprietary, so I can't copy-paste the error message fully.

It looks like javac reports that it doesn't make sense to deprecate a parameter.

Our environment:

  • Java 26
  • record-builder: 52
  • javac flags (from pom.xml:
    <arg>-AdefaultNotNull=true</arg>
    
    <arg>-Werror</arg>
    
    <arg>-Xlint:all</arg>
    <arg>-Xlint:-rawtypes</arg>
    <arg>-Xlint:-serial</arg>
    <arg>-Xlint:-processing</arg>
    <arg>-Xlint:-this-escape</arg>
    
    <arg>-XDcompilePolicy=simple</arg>
    <arg>--should-stop=ifError=FLOW</arg>
    
    <!-- also, error prone settings, but all generated classes are excluded via `-XepExcludedPaths` -->
    

The error messages above are clearly not from Error Prone.

@Randgalt

Randgalt commented Jun 20, 2026

Copy link
Copy Markdown
Owner

@semyon-levin-workato and @remal

Here's my proposed solution: #276 - please test with your use case and let me know how it works

@semyon-levin-workato

Copy link
Copy Markdown
Author

I tried you PR and, unfortunately, it doesn't help. I get these errors with it:

TestRecordBuilder.java:[46,42] oldValue() in com.workato.conductor.chassis.TestRecord has been deprecated
TestRecordBuilder.java:[62,83] oldValue() in com.workato.conductor.chassis.TestRecord has been deprecated
TestRecordBuilder.java:[97,58] @Deprecated annotation has no effect on this variable declaration
TestRecordBuilder.java:[145,60] @Deprecated annotation has no effect on this variable declaration

TestRecord looks like this:

import io.soabase.recordbuilder.core.RecordBuilder;

@RecordBuilder
public record TestRecord(
        @SuppressWarnings("all") @Deprecated String oldValue
) {
}

@Randgalt

Copy link
Copy Markdown
Owner

@semyon-levin-workato put the @SuppressWarnings on the record itself not the component.

@semyon-levin-workato

Copy link
Copy Markdown
Author

Seems you solution works. However, there are some moments to discuss. I'm adding some review comments to your PR.

@semyon-levin-workato semyon-levin-workato deleted the add-suppress-warnings-to-generated-classes branch June 23, 2026 14:29
@semyon-levin-workato

Copy link
Copy Markdown
Author

Closed this PR because it's not relevant anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add @SuppressWarnings to all generated classes

3 participants