Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ private boolean hasMethodWithSelector(TypeElement clazz, String selector) {
String functionName = function.getName();

ExecutableElement method = function.getExecutableElement();

if (method == null) {
return null;
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down