Skip to content
Merged
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 @@ -105,6 +105,7 @@ public final class ObjectiveCKmpMethodTranslator extends UnitTreeVisitor {

private static final ImmutableMap<String, String> JAVA_TO_NATIVE_TYPE_MAP =
ImmutableMap.<String, String>builder()
.put("com.google.common.collect.ImmutableList", "NSArray")
.put("java.util.List", "NSArray")
.put("java.util.Map", "NSDictionary")
.put("java.util.Set", "NSSet")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ public class ObjectiveCKmpMethodTranslatorTest extends GenerationTest {
@Override
public void setUp() throws IOException {
super.setUp();
addSourceFile(
"""
package com.google.common.collect;
public class ImmutableList<E> extends java.util.AbstractList<E> {
public static <E> ImmutableList<E> of() {
return new ImmutableList<E>();
}
public E get(int index) {
return null;
}
public int size() {
return 0;
}
}
""",
"com/google/common/collect/ImmutableList.java");
addSourceFile("public class Foo {}", "Foo.java");
addSourceFile("public class Bar {}", "Bar.java");
addSourceFile(
Expand Down Expand Up @@ -769,6 +785,58 @@ public void setList(List<Bar> list) {}
+ " toGenericWithId:list]]");
}

public void testImmutableListIsConverted() throws IOException {
addSourceFile(
"""
import com.google.common.collect.ImmutableList;

public class ImmutableListAdapter {
public static native Object fromImmutableList(ImmutableList<String> list) /*-[ return nil; ]-*/;
public static native ImmutableList<String> toImmutableList(Object list) /*-[ return nil; ]-*/;
}
""",
"ImmutableListAdapter.java");

addSourceFile(
"""
import com.google.common.collect.ImmutableList;
import com.google.j2objc.annotations.ObjectiveCKmpMethod;

public class ImmutableListTest {
@ObjectiveCKmpMethod(selector="setListWithNSArray:", adapter=ImmutableListAdapter.class)
public void setList(ImmutableList<String> list) {
return;
}

@ObjectiveCKmpMethod(selector="getListAsNSArray", adapter=ImmutableListAdapter.class)
public ImmutableList<String> getList() {
return null;
}
}
""",
"ImmutableListTest.java");

String testHeader = translateSourceFile("ImmutableListTest", "ImmutableListTest.h");
assertInTranslation(testHeader, "- (void)setListWithNSArray:(NSArray<NSString *> *)list;");
assertInTranslation(testHeader, "- (NSArray<NSString *> *)getListAsNSArray;");

String testImplementation = translateSourceFile("ImmutableListTest", "ImmutableListTest.m");
assertInTranslation(
testImplementation,
"""
- (void)setListWithNSArray:(NSArray<NSString *> *)list {
[self setListWithComGoogleCommonCollectImmutableList:[ImmutableListAdapter toImmutableListWithId:list]];
}
""");
assertInTranslation(
testImplementation,
"""
- (NSArray<NSString *> *)getListAsNSArray {
return (NSArray<NSString *> *) [ImmutableListAdapter fromImmutableListWithComGoogleCommonCollectImmutableList:[self getList]];
}
""");
}

public void testNoMethodFoundThrowsException() throws IOException {
addSourceFile(
"""
Expand Down