From d7d6c18531265fb525e7ceb7465b9d7d64e3d5fd Mon Sep 17 00:00:00 2001 From: J2ObjC Team Date: Fri, 20 Nov 2026 15:35:39 -0800 Subject: [PATCH] Generate `-dealloc` methods for Java records in `DestructorGenerator`. `DestructorGenerator` previously overrode `endVisit(AnnotationTypeDeclaration)` and `endVisit(TypeDeclaration)` but omitted `endVisit(RecordDeclaration)`. Because `RecordDeclaration` extends `AbstractTypeDeclaration` directly, J2ObjC never generated `- (void)dealloc` for Java `record` types. Under manual reference counting (`-use-reference-counting`), record constructors retain non-primitive component fields via `JreStrongAssign`, so every object-typed component leaked on deallocation. Under ARC/strict-field-assign, this also skipped `JreStrictFieldStrongRelease`, `finalize`, and `@OnDealloc` hooks on records. Add `endVisit(RecordDeclaration)` delegating to `addDeallocMethod(node)` and add unit tests in `DestructorGeneratorTest` covering MRC, ARC, `strict-field-assign`, primitive-only records, `@Weak`, `@RetainedWith`, `static` fields, `@OnDealloc`/`finalize()`, nested/local records, and generic records with compact constructors. PiperOrigin-RevId: 1026801600 --- .../j2objc/translate/DestructorGenerator.java | 6 ++ .../translate/DestructorGeneratorTest.java | 101 ++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/translator/src/main/java/com/google/devtools/j2objc/translate/DestructorGenerator.java b/translator/src/main/java/com/google/devtools/j2objc/translate/DestructorGenerator.java index 1d3bcc4113..14fe7da1ec 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/translate/DestructorGenerator.java +++ b/translator/src/main/java/com/google/devtools/j2objc/translate/DestructorGenerator.java @@ -31,6 +31,7 @@ import com.google.devtools.j2objc.ast.MethodInvocation; import com.google.devtools.j2objc.ast.NativeStatement; import com.google.devtools.j2objc.ast.PrefixExpression; +import com.google.devtools.j2objc.ast.RecordDeclaration; import com.google.devtools.j2objc.ast.SimpleName; import com.google.devtools.j2objc.ast.Statement; import com.google.devtools.j2objc.ast.SuperMethodInvocation; @@ -79,6 +80,11 @@ public void endVisit(AnnotationTypeDeclaration node) { addDeallocMethod(node); } + @Override + public void endVisit(RecordDeclaration node) { + addDeallocMethod(node); + } + @Override public void endVisit(TypeDeclaration node) { if (!node.isInterface()) { diff --git a/translator/src/test/java/com/google/devtools/j2objc/translate/DestructorGeneratorTest.java b/translator/src/test/java/com/google/devtools/j2objc/translate/DestructorGeneratorTest.java index 9fdf28d339..83663f7b3c 100644 --- a/translator/src/test/java/com/google/devtools/j2objc/translate/DestructorGeneratorTest.java +++ b/translator/src/test/java/com/google/devtools/j2objc/translate/DestructorGeneratorTest.java @@ -346,4 +346,105 @@ public void testOnDeallocARC() throws IOException { " JreCheckFinalize(self, [Test class]);", "}"); } + + private static final String RECORD_SOURCE = + """ + import com.google.j2objc.annotations.RetainedWith; + import com.google.j2objc.annotations.Weak; + public record Test(int i, T t, @Weak Object w, @RetainedWith Runnable r) { + static Object s = new Object(); + public Test { + if (i < 0) { + throw new IllegalArgumentException(); + } + } + } + """; + + public void testRecordFieldReleaseReferenceCounting() throws IOException { + options.setMemoryManagementOption(Options.MemoryManagementOption.REFERENCE_COUNTING); + assertTranslatedLines( + translateSourceFile(RECORD_SOURCE, "Test", "Test.m"), + "- (void)dealloc {", + " RELEASE_(t_);", + " JreRetainedWithRelease(self, r_);", + " [super dealloc];", + "}"); + // Primitive-only record with static field should not generate -dealloc. + assertNotInTranslation( + translateSourceFile( + "public record Empty(int x) { static Object s = new Object(); }", "Empty", "Empty.m"), + "dealloc"); + } + + public void testRecordFieldReleaseARC() throws IOException { + options.setMemoryManagementOption(Options.MemoryManagementOption.ARC); + assertNotInTranslation(translateSourceFile(RECORD_SOURCE, "Test", "Test.m"), "dealloc"); + } + + public void testRecordFieldReleaseStrictField() throws IOException { + options.setStrictFieldAssign(true); + options.setStrictFieldLoad(true); + options.setMemoryManagementOption(Options.MemoryManagementOption.REFERENCE_COUNTING); + assertTranslatedLines( + translateSourceFile(RECORD_SOURCE, "Test", "Test.m"), + "- (void)dealloc {", + " JreStrictFieldStrongRelease(&t_);", + " JreStrictFieldRetainedWithRelease(self, &r_);", + " [super dealloc];", + "}"); + options.setMemoryManagementOption(Options.MemoryManagementOption.ARC); + assertTranslatedLines( + translateSourceFile(RECORD_SOURCE, "Test", "Test.m"), + "- (void)dealloc {", + " JreStrictFieldStrongRelease(&t_);", + " JreStrictFieldStrongRelease(&r_);", + "}"); + } + + public void testRecordOnDeallocAndFinalize() throws IOException { + String source = + """ + import com.google.j2objc.annotations.OnDealloc; + public record Test(Object o) { + @OnDealloc private void close() {} + protected void finalize() {} + } + """; + options.setMemoryManagementOption(Options.MemoryManagementOption.REFERENCE_COUNTING); + assertTranslatedLines( + translateSourceFile(source, "Test", "Test.m"), + "- (void)dealloc {", + " Test_close(self);", + " JreCheckFinalize(self, [Test class]);", + " RELEASE_(o_);", + " [super dealloc];", + "}"); + options.setMemoryManagementOption(Options.MemoryManagementOption.ARC); + assertTranslatedLines( + translateSourceFile(source, "Test", "Test.m"), + "- (void)dealloc {", + " Test_close(self);", + " JreCheckFinalize(self, [Test class]);", + "}"); + } + + public void testNestedAndLocalRecordFieldRelease() throws IOException { + options.setMemoryManagementOption(Options.MemoryManagementOption.REFERENCE_COUNTING); + String source = + """ + public class Outer { + private record Entry(String key) {} + Object create(String s) { + record Local(String s) {} + return new Entry(new Local(s).s()); + } + } + """; + String translation = translateSourceFile(source, "Outer", "Outer.m"); + assertTranslatedLines( + translation, "- (void)dealloc {", " RELEASE_(key_);", " [super dealloc];", "}"); + assertTranslatedLines( + translation, "- (void)dealloc {", " RELEASE_(s_);", " [super dealloc];", "}"); + } }