Skip to content

Commit

Permalink
[jnigen] Add dynamic to type parameters to avoid type checking fails (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
HosseinYousefi authored Oct 16, 2024
1 parent c638d9f commit 10077a4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
1 change: 1 addition & 0 deletions pkgs/jni/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Add `JniUtils.fromReferenceAddress` which helps with sending `JObject`s
through method channels. You can send the address of the pointer as `long` and
reconstruct the class using the helper method.
- Fixed a bug where it would be possible for a type class inference to fail.

## 0.12.0

Expand Down
5 changes: 3 additions & 2 deletions pkgs/jni/lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ abstract class JObjType<T extends JObject> extends JType<T>
}

/// Lowest common ancestor of two types in the inheritance tree.
JObjType _lowestCommonAncestor(JObjType a, JObjType b) {
JObjType<dynamic> _lowestCommonAncestor(
JObjType<dynamic> a, JObjType<dynamic> b) {
while (a.superCount > b.superCount) {
a = a.superType;
}
Expand All @@ -174,6 +175,6 @@ JObjType _lowestCommonAncestor(JObjType a, JObjType b) {
}

@internal
JObjType lowestCommonSuperType(List<JObjType> types) {
JObjType<dynamic> lowestCommonSuperType(List<JObjType<dynamic>> types) {
return types.reduce(_lowestCommonAncestor);
}
1 change: 1 addition & 0 deletions pkgs/jnigen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Now excludes invalid identifiers by default.
- Fixed a bug where if multiple jars have classes within the same package, only
one of them gets generated.
- Fixed a bug where it would be possible for a type class inference to fail.

## 0.12.1

Expand Down
4 changes: 2 additions & 2 deletions pkgs/jnigen/example/in_app_java/lib/android_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4512,10 +4512,10 @@ class HashMap<$K extends _$jni.JObject, $V extends _$jni.JObject>
_$jni.JObjType<$V>? V,
}) {
K ??= _$jni.lowestCommonSuperType([
(map.$type as _$jni.JMapType).K,
(map.$type as _$jni.JMapType<_$core.dynamic, _$core.dynamic>).K,
]) as _$jni.JObjType<$K>;
V ??= _$jni.lowestCommonSuperType([
(map.$type as _$jni.JMapType).V,
(map.$type as _$jni.JMapType<_$core.dynamic, _$core.dynamic>).V,
]) as _$jni.JObjType<$V>;
return HashMap.fromReference(
K,
Expand Down
6 changes: 5 additions & 1 deletion pkgs/jnigen/lib/src/bindings/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1454,13 +1454,17 @@ class _TypeVarLocator extends TypeVisitor<Map<String, List<OutsideInBuffer>>> {
final result = <String, List<OutsideInBuffer>>{};
final prefix = resolver.resolvePrefix(node.classDecl);
final typeClass = '$prefix${node.classDecl.typeClassName}';
final typeClassParams =
List.filled(node.classDecl.allTypeParams.length, '$_core.dynamic')
.join(', ')
.encloseIfNotEmpty('<', '>');
for (var i = 0; i < node.params.length; ++i) {
final typeParam = node.classDecl.allTypeParams[i + offset].name;
final exprs = node.params[i].accept(this);
for (final expr in exprs.entries) {
for (final buffer in expr.value) {
buffer.appendLeft('(');
buffer.prependRight(' as $typeClass).$typeParam');
buffer.prependRight(' as $typeClass$typeClassParams).$typeParam');
result[expr.key] = (result[expr.key] ?? [])..add(buffer);
}
}
Expand Down
31 changes: 18 additions & 13 deletions pkgs/jnigen/test/simple_package_test/bindings/simple_package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ class Example extends _$jni.JObject {
_$jni.JObjType<$T>? T,
}) {
T ??= _$jni.lowestCommonSuperType([
(lt.$type as _$jni.JListType).E,
(lt.$type as _$jni.JListType<_$core.dynamic>).E,
t.$type,
]) as _$jni.JObjType<$T>;
_methodWithSeveralParams(
Expand Down Expand Up @@ -2878,10 +2878,14 @@ class GrandParent_Parent_Child<$T extends _$jni.JObject,
_$jni.JObjType<$U>? U,
}) {
T ??= _$jni.lowestCommonSuperType([
($parent.$type as $GrandParent_Parent$Type).T,
($parent.$type
as $GrandParent_Parent$Type<_$core.dynamic, _$core.dynamic>)
.T,
]) as _$jni.JObjType<$T>;
S ??= _$jni.lowestCommonSuperType([
($parent.$type as $GrandParent_Parent$Type).S,
($parent.$type
as $GrandParent_Parent$Type<_$core.dynamic, _$core.dynamic>)
.S,
]) as _$jni.JObjType<$S>;
U ??= _$jni.lowestCommonSuperType([
newValue.$type,
Expand Down Expand Up @@ -3039,7 +3043,7 @@ class GrandParent_Parent<$T extends _$jni.JObject, $S extends _$jni.JObject>
_$jni.JObjType<$S>? S,
}) {
T ??= _$jni.lowestCommonSuperType([
($parent.$type as $GrandParent$Type).T,
($parent.$type as $GrandParent$Type<_$core.dynamic>).T,
]) as _$jni.JObjType<$T>;
S ??= _$jni.lowestCommonSuperType([
newValue.$type,
Expand Down Expand Up @@ -3194,7 +3198,7 @@ class GrandParent_StaticParent_Child<$S extends _$jni.JObject,
}) {
S ??= _$jni.lowestCommonSuperType([
parentValue.$type,
($parent.$type as $GrandParent_StaticParent$Type).S,
($parent.$type as $GrandParent_StaticParent$Type<_$core.dynamic>).S,
]) as _$jni.JObjType<$S>;
U ??= _$jni.lowestCommonSuperType([
value.$type,
Expand Down Expand Up @@ -3714,11 +3718,11 @@ class MyMap_MyEntry<$K extends _$jni.JObject, $V extends _$jni.JObject>
}) {
K ??= _$jni.lowestCommonSuperType([
key.$type,
($parent.$type as $MyMap$Type).K,
($parent.$type as $MyMap$Type<_$core.dynamic, _$core.dynamic>).K,
]) as _$jni.JObjType<$K>;
V ??= _$jni.lowestCommonSuperType([
value.$type,
($parent.$type as $MyMap$Type).V,
($parent.$type as $MyMap$Type<_$core.dynamic, _$core.dynamic>).V,
]) as _$jni.JObjType<$V>;
return MyMap_MyEntry.fromReference(
K,
Expand Down Expand Up @@ -4080,8 +4084,9 @@ class MyStack<$T extends _$jni.JObject> extends _$jni.JObject {
}) {
S ??= _$jni.lowestCommonSuperType([
(((((arr.$type as _$jni.JArrayType).elementType as _$jni.JObjType)
as _$jni.JArrayType)
.elementType as _$jni.JObjType) as $GrandParent$Type)
as _$jni.JArrayType)
.elementType as _$jni.JObjType)
as $GrandParent$Type<_$core.dynamic>)
.T,
]) as _$jni.JObjType<$S>;
return _fromArrayOfArrayOfGrandParents(
Expand Down Expand Up @@ -4834,7 +4839,7 @@ class GenericInterface<$T extends _$jni.JObject> extends _$jni.JObject {
_$jni.JObjType<$U>? U,
}) {
U ??= _$jni.lowestCommonSuperType([
(map.$type as _$jni.JMapType).V,
(map.$type as _$jni.JMapType<_$core.dynamic, _$core.dynamic>).V,
]) as _$jni.JObjType<$U>;
return _firstKeyOf(reference.pointer, _id_firstKeyOf as _$jni.JMethodIDPtr,
map.reference.pointer)
Expand Down Expand Up @@ -4864,7 +4869,7 @@ class GenericInterface<$T extends _$jni.JObject> extends _$jni.JObject {
_$jni.JObjType<$U>? U,
}) {
U ??= _$jni.lowestCommonSuperType([
(map.$type as _$jni.JMapType).V,
(map.$type as _$jni.JMapType<_$core.dynamic, _$core.dynamic>).V,
]) as _$jni.JObjType<$U>;
return _firstValueOf(reference.pointer,
_id_firstValueOf as _$jni.JMethodIDPtr, map.reference.pointer)
Expand Down Expand Up @@ -5591,7 +5596,7 @@ class MyInterfaceConsumer extends _$jni.JObject {
}) {
T ??= _$jni.lowestCommonSuperType([
t.$type,
(myInterface.$type as $MyInterface$Type).T,
(myInterface.$type as $MyInterface$Type<_$core.dynamic>).T,
]) as _$jni.JObjType<$T>;
_consumeOnAnotherThread(
_class.reference.pointer,
Expand Down Expand Up @@ -5651,7 +5656,7 @@ class MyInterfaceConsumer extends _$jni.JObject {
}) {
T ??= _$jni.lowestCommonSuperType([
t.$type,
(myInterface.$type as $MyInterface$Type).T,
(myInterface.$type as $MyInterface$Type<_$core.dynamic>).T,
]) as _$jni.JObjType<$T>;
_consumeOnSameThread(
_class.reference.pointer,
Expand Down

0 comments on commit 10077a4

Please sign in to comment.