Add annotation to ButtonUiModel for j2objc collection interop improvement. This CL also updates ObjectiveCKmpMethodTranslator to support generic type in the adapter. PiperOrigin-RevId: 895474525
diff --git a/translator/src/main/java/com/google/devtools/j2objc/translate/ObjectiveCKmpMethodTranslator.java b/translator/src/main/java/com/google/devtools/j2objc/translate/ObjectiveCKmpMethodTranslator.java index 471e965..f9c4a93 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/translate/ObjectiveCKmpMethodTranslator.java +++ b/translator/src/main/java/com/google/devtools/j2objc/translate/ObjectiveCKmpMethodTranslator.java
@@ -625,8 +625,64 @@ return methodName.startsWith(prefix); } + /** + * Checks if the adapter method's type matches the target candidate type. + * First attempts an exact match. If the method declares type parameters (e.g., {@code <T>}), + * it performs a looser match that treats type variables as matching any type. + */ boolean matchesType(ExecutableElement method, TypeMirror candidate, TypeUtil typeUtil) { - return typeUtil.isSameType(getType(method), candidate); + TypeMirror methodType = getType(method); + if (typeUtil.isSameType(methodType, candidate)) { + return true; + } + // If the adapter method has generic type parameters, allow type variables to act as + // wildcards. + if (!method.getTypeParameters().isEmpty()) { + return isTypeMatchIgnoringTypeVariables(methodType, candidate, typeUtil); + } + return false; + } + + /** + * Recursively checks if {@code methodType} matches {@code candidate}, treating any type + * variable in {@code methodType} as a match-all wildcard. For example, this allows + * {@code ImmutableList<T>} to match {@code ImmutableList<CustomClass>}. + */ + private boolean isTypeMatchIgnoringTypeVariables( + TypeMirror methodType, TypeMirror candidate, TypeUtil typeUtil) { + // A type variable (like 'T') matches any type in the candidate. + if (TypeUtil.isTypeVariable(methodType)) { + return true; + } + + // For declared types (like classes or interfaces), ensure the base elements are the same, + // then recursively check their type arguments. + if (TypeUtil.isDeclaredType(methodType) && TypeUtil.isDeclaredType(candidate)) { + DeclaredType mDeclared = (DeclaredType) methodType; + DeclaredType cDeclared = (DeclaredType) candidate; + + // Check if the base type (e.g., ImmutableList) matches. + if (!mDeclared.asElement().equals(cDeclared.asElement())) { + return false; + } + + List<? extends TypeMirror> mArgs = mDeclared.getTypeArguments(); + List<? extends TypeMirror> cArgs = cDeclared.getTypeArguments(); + if (mArgs.size() != cArgs.size()) { + return false; + } + + // Recursively compare each type argument. + for (int i = 0; i < mArgs.size(); i++) { + if (!isTypeMatchIgnoringTypeVariables(mArgs.get(i), cArgs.get(i), typeUtil)) { + return false; + } + } + return true; + } + + // Fallback to strict equality for other types (e.g., primitives, arrays). + return typeUtil.isSameType(methodType, candidate); } abstract TypeMirror getType(ExecutableElement method);
diff --git a/translator/src/test/java/com/google/devtools/j2objc/translate/ObjectiveCKmpMethodTranslatorTest.java b/translator/src/test/java/com/google/devtools/j2objc/translate/ObjectiveCKmpMethodTranslatorTest.java index 918abf4..2e86487 100644 --- a/translator/src/test/java/com/google/devtools/j2objc/translate/ObjectiveCKmpMethodTranslatorTest.java +++ b/translator/src/test/java/com/google/devtools/j2objc/translate/ObjectiveCKmpMethodTranslatorTest.java
@@ -937,11 +937,10 @@ addSourceFile( """ import com.google.common.collect.ImmutableList; - import my.pkg.CustomClass; public class ImmutableListAdapter { - public static native Object fromImmutableList(ImmutableList<CustomClass> list) /*-[ return nil; ]-*/; - public static native ImmutableList<CustomClass> toImmutableList(Object list) /*-[ return nil; ]-*/; + public static native <T> Object fromImmutableList(ImmutableList<T> list) /*-[ return nil; ]-*/; + public static native <T> ImmutableList<T> toImmutableList(Object list) /*-[ return nil; ]-*/; } """, "ImmutableListAdapter.java"); @@ -988,7 +987,7 @@ abstractImpl, """ - (void)setItems:(NSArray<MyPkgCustomClass *> *)items { - [self setItemsWithComGoogleCommonCollectImmutableList:[ImmutableListAdapter toImmutableListWithId:items]]; + [self setItemsWithComGoogleCommonCollectImmutableList:(ComGoogleCommonCollectImmutableList *) [ImmutableListAdapter toImmutableListWithId:items]]; } """); assertInTranslation(