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 @@ -722,7 +722,7 @@ private boolean canPrintPseudoProperty(MethodDeclaration m) {
return true;
}

private void printPseudoProperty(MethodDeclaration m) {
private void printPseudoProperty(MethodDeclaration m, boolean isKotlinCompanion) {
ExecutableElement methodElement = m.getExecutableElement();
String methodName = nameTable.getMethodSelector(methodElement);
String propertyName = NameTable.lowercaseFirst(methodName.replaceFirst("get", ""));
Expand All @@ -738,12 +738,13 @@ private void printPseudoProperty(MethodDeclaration m) {

TypeMirror returnType = m.getReturnTypeMirror();
ExecutableElement setter =
ElementUtil.findSetterMethod(propertyName, returnType, declaringClass, false);
ElementUtil.findSetterMethod(
propertyName, returnType, declaringClass, ElementUtil.isStatic(methodElement));

newline();
printf(
"@property (%snonatomic, %s, %s%s) %s %s;",
ElementUtil.isStatic(methodElement) ? "class, " : "",
ElementUtil.isStatic(methodElement) && !isKotlinCompanion ? "class, " : "",
"getter=" + methodName,
setter != null ? "setter=" + nameTable.getMethodSelector(setter) : "readonly",
shouldAddNullableAnnotation(methodElement) ? ", nullable" : "",
Expand Down Expand Up @@ -781,7 +782,7 @@ private void printMethodDeclaration(
JavadocGenerator.printDocComment(getBuilder(), m.getJavadoc());

if (canPrintPseudoProperty(m)) {
printPseudoProperty(m);
printPseudoProperty(m, isKotlinCompanion);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,24 @@ public static void doSomething() {}
assertInTranslation(impl, "+ (id<FooCompanion>)companion {");
assertInTranslation(impl, "return (id<FooCompanion>)self;");
}

public void testCompanionPropertiesForPropertyMethods() throws IOException {
String source =
"""
import com.google.j2objc.annotations.Property;
@com.google.j2objc.annotations.GenerateObjCCompanion
public class Foo {
@Property("readonly, nonnull")
public static String getBar() {
return "bar";
}
}
""";
String header = translateSourceFile(source, "Foo", "Foo.h");
assertInTranslation(header, "@protocol FooCompanion");
assertInTranslation(header, "@property (nonatomic, getter=getBar, readonly) NSString * bar;");
assertInTranslation(
header, "@property (class, nonatomic, getter=getBar, readonly) NSString * bar;");
}
}