From 2c7e3624af419fe110593bc2447c000149de0c0e Mon Sep 17 00:00:00 2001 From: Wei Jun Kong Date: Wed, 22 Jul 2026 19:19:15 -0700 Subject: [PATCH] BEGIN_PUBLIC Add enable_swift_naming parameter to xplat_java_library and j2kt_native_library. END_PUBLIC PiperOrigin-RevId: 952465709 --- .../devtools/j2objc/util/NameTable.java | 7 ++++ .../gen/ObjectiveCHeaderGeneratorTest.java | 39 ++++++++++--------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/translator/src/main/java/com/google/devtools/j2objc/util/NameTable.java b/translator/src/main/java/com/google/devtools/j2objc/util/NameTable.java index e60804d472..053827bfa5 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/util/NameTable.java +++ b/translator/src/main/java/com/google/devtools/j2objc/util/NameTable.java @@ -671,6 +671,7 @@ private boolean hasMethodWithSelector(TypeElement clazz, String selector) { String functionName = function.getName(); ExecutableElement method = function.getExecutableElement(); + if (method == null) { return null; } @@ -686,6 +687,12 @@ private boolean hasMethodWithSelector(TypeElement clazz, String selector) { return null; } + if (ElementUtil.isStatic(method)) { + // Static methods without emitWrapperMethods enabled should not generate NS_SWIFT_NAME + // annotations because there is no wrapper method to call. + return null; + } + String className = getSwiftClassNameFromAnnotation(owner, false); if (className == null) { // There isn't nice naming so fallback to the normal ObjC class name diff --git a/translator/src/test/java/com/google/devtools/j2objc/gen/ObjectiveCHeaderGeneratorTest.java b/translator/src/test/java/com/google/devtools/j2objc/gen/ObjectiveCHeaderGeneratorTest.java index 1ec90ebde2..f5bb20f467 100644 --- a/translator/src/test/java/com/google/devtools/j2objc/gen/ObjectiveCHeaderGeneratorTest.java +++ b/translator/src/test/java/com/google/devtools/j2objc/gen/ObjectiveCHeaderGeneratorTest.java @@ -1690,29 +1690,32 @@ public void testSwiftNameAnnotationWithStaticFunctions() throws IOException { options.setEmitWrapperMethods(false); addSourceFile( - "@SwiftName " - + "package com.foo.bar;" - + "" - + "import com.google.j2objc.annotations.SwiftName;", + """ + @SwiftName + package com.foo.bar; + + import com.google.j2objc.annotations.SwiftName; + """, "com/foo/bar/package-info.java"); String sourceContent = - " package com.foo.bar;" - + "" - + "public class FooBar {" - + " FooBar() {}" - + " public static final class NestedBar {}" - + " public void setFooField(String fooField) {" - + " " - + " }" - + " public static String builderWithExpectedSize(int expectedSize){ return \"\"; }" - + " public static String builderWithName(String name){ return \"\"; }" - + "}"; + """ + package com.foo.bar; + + public class FooBar { + FooBar() {} + public static final class NestedBar {} + public void setFooField(String fooField) {} + public static String builderWithExpectedSize(int expectedSize){ return ""; } + public static String builderWithName(String name){ return ""; } + } + """; String translation = translateSourceFile(sourceContent, "FooBar", "com/foo/bar/FooBar.h"); assertInTranslation(translation, "NS_SWIFT_NAME(FooBar.init())"); - assertInTranslation( - translation, "NS_SWIFT_NAME(FooBar.builderWithExpectedSize(expectedSize:))"); - assertInTranslation(translation, "NS_SWIFT_NAME(FooBar.builderWithName(name:))"); + // We don't expect the static functions to have NS_SWIFT_NAME annotations because + // there isn't a wrapper method available because emitWrapperMethods is false. + assertNotInTranslation(translation, "NS_SWIFT_NAME(FooBar.builderWithExpectedSize"); + assertNotInTranslation(translation, "NS_SWIFT_NAME(FooBar.builderWithName"); } public void testSwiftNameAnnotationWithStaticFunctionsWithWapperMethods() throws IOException {