Skip to content

[jnigen] Remove TypeUsage indirection #2186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 11, 2025
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
33 changes: 17 additions & 16 deletions pkgs/jnigen/example/in_app_java/lib/android_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_types
// ignore_for_file: constant_identifier_names
// ignore_for_file: comment_references
// ignore_for_file: doc_directive_unknown
// ignore_for_file: file_names
// ignore_for_file: inference_failure_on_untyped_parameter
Expand Down Expand Up @@ -782,7 +783,7 @@ class EmojiCompat$Config extends jni$_.JObject {
jni$_.JniResult Function(jni$_.Pointer<jni$_.Void>,
jni$_.JMethodIDPtr, int, jni$_.Pointer<jni$_.Void>)>();

/// from: `public androidx.emoji2.text.EmojiCompat$Config setUseEmojiAsDefaultStyle(boolean z, java.util.List list)`
/// from: `public androidx.emoji2.text.EmojiCompat$Config setUseEmojiAsDefaultStyle(boolean z, java.util.List<java.lang.Integer> list)`
/// The returned object must be released after use, by calling the [release] method.
EmojiCompat$Config setUseEmojiAsDefaultStyle$1(
bool z,
Expand Down Expand Up @@ -3737,7 +3738,7 @@ class DefaultEmojiCompatConfig$DefaultEmojiCompatConfigHelper
jni$_.Pointer<jni$_.Void>,
int)>();

/// from: `public java.util.List queryIntentContentProviders(android.content.pm.PackageManager packageManager, android.content.Intent intent, int i)`
/// from: `public java.util.List<android.content.pm.ResolveInfo> queryIntentContentProviders(android.content.pm.PackageManager packageManager, android.content.Intent intent, int i)`
/// The returned object must be released after use, by calling the [release] method.
jni$_.JList<jni$_.JObject?> queryIntentContentProviders(
jni$_.JObject packageManager,
Expand Down Expand Up @@ -3949,7 +3950,7 @@ class DefaultEmojiCompatConfig$DefaultEmojiCompatConfigHelper_API19
jni$_.Pointer<jni$_.Void>,
int)>();

/// from: `public java.util.List queryIntentContentProviders(android.content.pm.PackageManager packageManager, android.content.Intent intent, int i)`
/// from: `public java.util.List<android.content.pm.ResolveInfo> queryIntentContentProviders(android.content.pm.PackageManager packageManager, android.content.Intent intent, int i)`
/// The returned object must be released after use, by calling the [release] method.
jni$_.JList<jni$_.JObject?> queryIntentContentProviders(
jni$_.JObject packageManager,
Expand Down Expand Up @@ -5448,7 +5449,7 @@ class Build extends jni$_.JObject {
jni$_.JMethodIDPtr,
)>();

/// from: `static public java.util.List getFingerprintedPartitions()`
/// from: `static public java.util.List<android.os.Build$Partition> getFingerprintedPartitions()`
/// The returned object must be released after use, by calling the [release] method.
static jni$_.JList<Build$Partition?>? getFingerprintedPartitions() {
return _getFingerprintedPartitions(_class.reference.pointer,
Expand Down Expand Up @@ -5703,7 +5704,7 @@ class HashMap<$K extends jni$_.JObject?, $V extends jni$_.JObject?>
jni$_.JniResult Function(jni$_.Pointer<jni$_.Void>,
jni$_.JMethodIDPtr, jni$_.Pointer<jni$_.Void>)>();

/// from: `public void <init>(java.util.Map map)`
/// from: `public void <init>(java.util.Map<? extends K, ? extends V> map)`
/// The returned object must be released after use, by calling the [release] method.
factory HashMap.new$3(
jni$_.JMap<$K?, $V?>? map, {
Expand Down Expand Up @@ -5868,7 +5869,7 @@ class HashMap<$K extends jni$_.JObject?, $V extends jni$_.JObject?>
jni$_.JThrowablePtr Function(jni$_.Pointer<jni$_.Void>,
jni$_.JMethodIDPtr, jni$_.Pointer<jni$_.Void>)>();

/// from: `public void putAll(java.util.Map map)`
/// from: `public void putAll(java.util.Map<? extends K, ? extends V> map)`
void putAll(
jni$_.JMap<$K?, $V?>? map,
) {
Expand Down Expand Up @@ -5969,7 +5970,7 @@ class HashMap<$K extends jni$_.JObject?, $V extends jni$_.JObject?>
jni$_.JMethodIDPtr,
)>();

/// from: `public java.util.Set keySet()`
/// from: `public java.util.Set<K> keySet()`
/// The returned object must be released after use, by calling the [release] method.
jni$_.JSet<$K?>? keySet() {
return _keySet(reference.pointer, _id_keySet as jni$_.JMethodIDPtr)
Expand All @@ -5993,7 +5994,7 @@ class HashMap<$K extends jni$_.JObject?, $V extends jni$_.JObject?>
jni$_.JMethodIDPtr,
)>();

/// from: `public java.util.Collection values()`
/// from: `public java.util.Collection<V> values()`
/// The returned object must be released after use, by calling the [release] method.
jni$_.JObject? values() {
return _values(reference.pointer, _id_values as jni$_.JMethodIDPtr)
Expand All @@ -6017,7 +6018,7 @@ class HashMap<$K extends jni$_.JObject?, $V extends jni$_.JObject?>
jni$_.JMethodIDPtr,
)>();

/// from: `public java.util.Set entrySet()`
/// from: `public java.util.Set<java.util.Map$Entry<K, V>> entrySet()`
/// The returned object must be released after use, by calling the [release] method.
jni$_.JSet<jni$_.JObject?>? entrySet() {
return _entrySet(reference.pointer, _id_entrySet as jni$_.JMethodIDPtr)
Expand Down Expand Up @@ -6231,7 +6232,7 @@ class HashMap<$K extends jni$_.JObject?, $V extends jni$_.JObject?>
jni$_.Pointer<jni$_.Void>,
jni$_.Pointer<jni$_.Void>)>();

/// from: `public V computeIfAbsent(K object, java.util.function.Function function)`
/// from: `public V computeIfAbsent(K object, java.util.function.Function<? super K, ? extends V> function)`
/// The returned object must be released after use, by calling the [release] method.
$V? computeIfAbsent(
$K? object,
Expand Down Expand Up @@ -6269,7 +6270,7 @@ class HashMap<$K extends jni$_.JObject?, $V extends jni$_.JObject?>
jni$_.Pointer<jni$_.Void>,
jni$_.Pointer<jni$_.Void>)>();

/// from: `public V computeIfPresent(K object, java.util.function.BiFunction biFunction)`
/// from: `public V computeIfPresent(K object, java.util.function.BiFunction<? super K, ? super V, ? extends V> biFunction)`
/// The returned object must be released after use, by calling the [release] method.
$V? computeIfPresent(
$K? object,
Expand Down Expand Up @@ -6307,7 +6308,7 @@ class HashMap<$K extends jni$_.JObject?, $V extends jni$_.JObject?>
jni$_.Pointer<jni$_.Void>,
jni$_.Pointer<jni$_.Void>)>();

/// from: `public V compute(K object, java.util.function.BiFunction biFunction)`
/// from: `public V compute(K object, java.util.function.BiFunction<? super K, ? super V, ? extends V> biFunction)`
/// The returned object must be released after use, by calling the [release] method.
$V? compute(
$K? object,
Expand Down Expand Up @@ -6344,7 +6345,7 @@ class HashMap<$K extends jni$_.JObject?, $V extends jni$_.JObject?>
jni$_.Pointer<jni$_.Void>,
jni$_.Pointer<jni$_.Void>)>();

/// from: `public V merge(K object, V object1, java.util.function.BiFunction biFunction)`
/// from: `public V merge(K object, V object1, java.util.function.BiFunction<? super V, ? super V, ? extends V> biFunction)`
/// The returned object must be released after use, by calling the [release] method.
$V? merge(
$K? object,
Expand Down Expand Up @@ -6375,7 +6376,7 @@ class HashMap<$K extends jni$_.JObject?, $V extends jni$_.JObject?>
jni$_.JThrowablePtr Function(jni$_.Pointer<jni$_.Void>,
jni$_.JMethodIDPtr, jni$_.Pointer<jni$_.Void>)>();

/// from: `public void forEach(java.util.function.BiConsumer biConsumer)`
/// from: `public void forEach(java.util.function.BiConsumer<? super K, ? super V> biConsumer)`
void forEach(
jni$_.JObject? biConsumer,
) {
Expand All @@ -6401,7 +6402,7 @@ class HashMap<$K extends jni$_.JObject?, $V extends jni$_.JObject?>
jni$_.JThrowablePtr Function(jni$_.Pointer<jni$_.Void>,
jni$_.JMethodIDPtr, jni$_.Pointer<jni$_.Void>)>();

/// from: `public void replaceAll(java.util.function.BiFunction biFunction)`
/// from: `public void replaceAll(java.util.function.BiFunction<? super K, ? super V, ? extends V> biFunction)`
void replaceAll(
jni$_.JObject? biFunction,
) {
Expand Down Expand Up @@ -6449,7 +6450,7 @@ class HashMap<$K extends jni$_.JObject?, $V extends jni$_.JObject?>
jni$_.JniResult Function(
jni$_.Pointer<jni$_.Void>, jni$_.JMethodIDPtr, int)>();

/// from: `static public java.util.HashMap newHashMap(int i)`
/// from: `static public java.util.HashMap<K, V> newHashMap(int i)`
/// The returned object must be released after use, by calling the [release] method.
static HashMap<$K?, $V?>?
newHashMap<$K extends jni$_.JObject?, $V extends jni$_.JObject?>(
Expand Down
3 changes: 2 additions & 1 deletion pkgs/jnigen/example/kotlin_plugin/lib/kotlin_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_types
// ignore_for_file: constant_identifier_names
// ignore_for_file: comment_references
// ignore_for_file: doc_directive_unknown
// ignore_for_file: file_names
// ignore_for_file: inference_failure_on_untyped_parameter
Expand Down Expand Up @@ -92,7 +93,7 @@ class Example extends jni$_.JObject {
jni$_.JniResult Function(jni$_.Pointer<jni$_.Void>,
jni$_.JMethodIDPtr, jni$_.Pointer<jni$_.Void>)>();

/// from: `public final java.lang.Object thinkBeforeAnswering(kotlin.coroutines.Continuation continuation)`
/// from: `public final java.lang.Object thinkBeforeAnswering(kotlin.coroutines.Continuation<? super java.lang.String> continuation)`
/// The returned object must be released after use, by calling the [release] method.
core$_.Future<jni$_.JString> thinkBeforeAnswering() async {
final $p = jni$_.ReceivePort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_types
// ignore_for_file: constant_identifier_names
// ignore_for_file: comment_references
// ignore_for_file: doc_directive_unknown
// ignore_for_file: file_names
// ignore_for_file: inference_failure_on_untyped_parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_types
// ignore_for_file: constant_identifier_names
// ignore_for_file: comment_references
// ignore_for_file: doc_directive_unknown
// ignore_for_file: file_names
// ignore_for_file: inference_failure_on_untyped_parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_types
// ignore_for_file: constant_identifier_names
// ignore_for_file: comment_references
// ignore_for_file: doc_directive_unknown
// ignore_for_file: file_names
// ignore_for_file: inference_failure_on_untyped_parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_types
// ignore_for_file: constant_identifier_names
// ignore_for_file: comment_references
// ignore_for_file: doc_directive_unknown
// ignore_for_file: file_names
// ignore_for_file: inference_failure_on_untyped_parameter
Expand Down
44 changes: 20 additions & 24 deletions pkgs/jnigen/lib/src/bindings/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ extension on String {

extension on DeclaredType {
List<T> mapTypeParameters<T>(
T Function(bool isNullable, TypeUsage definedType) mapper,
T Function(bool isNullable, ReferredType definedType) mapper,
) {
final result = <T>[];
final offset = classDecl.allTypeParams.length - params.length;
for (var i = 0; i < classDecl.allTypeParams.length; ++i) {
final param = i >= offset ? params[i - offset] : TypeUsage.object;
final param = i >= offset ? params[i - offset] : DeclaredType.object;
result.add(mapper(classDecl.allTypeParams[i].isNullable, param));
}
return result;
Expand Down Expand Up @@ -157,6 +157,7 @@ import 'package:jni/jni.dart' as $_jni;
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_types
// ignore_for_file: constant_identifier_names
// ignore_for_file: comment_references
// ignore_for_file: doc_directive_unknown
// ignore_for_file: file_names
// ignore_for_file: inference_failure_on_untyped_parameter
Expand Down Expand Up @@ -387,7 +388,7 @@ ${modifier}final $classRef = $_jni.JClass.forName(r'$internalName');
final ctorTypeClassesDef = typeParams
.map((typeParam) => 'this.$typeParam,')
.join(_newLine(depth: 2));
final superClass = node.classDecl.superclass!.type as DeclaredType;
final superClass = node.classDecl.superclass! as DeclaredType;
final superTypeClassesCall = superClass.classDecl.isObject
? ''
: superClass.params
Expand Down Expand Up @@ -666,16 +667,12 @@ final class _$implClassName$typeParamsDef with $implClassName$typeParamsCall {
annotations: [Annotation.nullable],
params: node.allTypeParams
.map(
(typeParam) => TypeUsage(
shorthand: '',
kind: Kind.typeVariable,
typeJson: {},
)..type = (TypeVar(name: typeParam.name)
(typeParam) => TypeVar(name: typeParam.name)
..origin = TypeParam(
name: typeParam.name,
annotations: [Annotation.nonNull],
bounds: typeParam.bounds,
)),
),
)
.toList(),
)..classDecl = node)
Expand Down Expand Up @@ -818,7 +815,7 @@ class _TypeGenerator extends TypeVisitor<String> {
arrayType: true,
isTopTypeNullable: true,
);
if (innerType.kind == Kind.primitive) {
if (innerType is PrimitiveType) {
return '$_jni.J${innerType.accept(typeGenerator)}Array$nullable';
}
return '$_jArray<${innerType.accept(typeGenerator)}>$nullable';
Expand Down Expand Up @@ -973,7 +970,7 @@ class _TypeClassGenerator extends TypeVisitor<_TypeClass> {
final type = includeNullability && node.isNullable && isTopTypeNullable
? 'NullableType'
: 'Type';
if (node.elementType.kind == Kind.primitive) {
if (node.elementType is PrimitiveType) {
return _TypeClass(
'$ifConst$_jni.J${innerType}Array$type()',
innerTypeClass.canBeConst,
Expand Down Expand Up @@ -1222,9 +1219,9 @@ ${modifier}final _id_$name =
}

void writeDocs(Field node, {required bool writeReleaseInstructions}) {
final originalDecl = '${node.type.shorthand} ${node.name}';
final originalDecl = '${node.type} ${node.name}';
s.writeln(' /// from: `${node.modifiers.join(' ')} $originalDecl`');
if (node.type.kind != Kind.primitive && writeReleaseInstructions) {
if (node.type is! PrimitiveType && writeReleaseInstructions) {
s.writeln(_releaseInstruction);
}
node.javadoc?.accept(_DocGenerator(s, depth: 1));
Expand Down Expand Up @@ -1369,10 +1366,10 @@ ${modifier}final _$name = $_protectedExtension
// Docs
s.write(' /// from: `');
s.writeAll(node.modifiers.map((m) => '$m '));
s.write('${node.returnType.shorthand} ${node.name}(');
s.writeAll(node.params.map((p) => '${p.type.shorthand} ${p.name}'), ', ');
s.write('${node.returnType} ${node.name}(');
s.writeAll(node.params.map((p) => '${p.type} ${p.name}'), ', ');
s.writeln(')`');
if (node.returnType.kind != Kind.primitive || node.isConstructor) {
if (node.returnType is! PrimitiveType || node.isConstructor) {
s.writeln(_releaseInstruction);
}
node.javadoc?.accept(_DocGenerator(s, depth: 1));
Expand Down Expand Up @@ -1584,7 +1581,7 @@ class _ParamReference extends Visitor<Param, String> {

@override
String visit(Param node) {
if (node.type.kind == Kind.primitive) {
if (node.type is PrimitiveType) {
return '';
}
final nullable = node.isNullable ? '?' : '';
Expand All @@ -1606,7 +1603,7 @@ class _ParamCall extends Visitor<Param, String> {
@override
String visit(Param node) {
final nativeSuffix = node.type.accept(const _ToNativeSuffix());
final nonPrimitive = node.type.kind == Kind.primitive ? '' : r'_$';
final nonPrimitive = node.type is PrimitiveType ? '' : r'_$';
final paramCall = '$nonPrimitive${node.finalName}$nativeSuffix';
return paramCall;
}
Expand Down Expand Up @@ -1945,12 +1942,11 @@ class _InterfaceParamCast extends Visitor<Param, void> {
),
)
.name;
final nullable =
node.isNullable && node.type.kind != Kind.primitive ? '?' : '!';
final nullable = node.isNullable && node.type is! PrimitiveType ? '?' : '!';
s.write('\$a![$paramIndex]$nullable.as($typeClass, releaseOriginal: true)');
if (node.type.kind == Kind.primitive) {
if (node.type is PrimitiveType) {
// Convert to Dart type.
final name = (node.type.type as PrimitiveType).name;
final name = node.type.name;
s.write('.${name}Value(releaseOriginal: true)');
}
s.writeln(',');
Expand Down Expand Up @@ -2000,8 +1996,8 @@ class _CallMethodName extends Visitor<Method, String> {
return 'globalEnv_NewObject';
}
final String type;
if (node.returnType.kind == Kind.primitive) {
type = (node.returnType.type as PrimitiveType).name.capitalize();
if (node.returnType is PrimitiveType) {
type = node.returnType.name.capitalize();
} else {
type = 'Object';
}
Expand Down
16 changes: 8 additions & 8 deletions pkgs/jnigen/lib/src/bindings/kotlin_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,23 @@ class _KotlinMethodProcessor extends Visitor<Method, void> {
if (function.isSuspend) {
const kotlinContinutationType = 'kotlin.coroutines.Continuation';
assert(node.params.isNotEmpty &&
node.params.last.type.kind == Kind.declared &&
node.params.last.type is DeclaredType &&
node.params.last.type.name == kotlinContinutationType);
var continuationType =
(node.params.last.type.type as DeclaredType).params.firstOrNull;
(node.params.last.type as DeclaredType).params.firstOrNull;
if (continuationType != null &&
continuationType.kind == Kind.wildcard &&
(continuationType.type as Wildcard).superBound != null) {
continuationType = (continuationType.type as Wildcard).superBound!;
continuationType is Wildcard &&
continuationType.superBound != null) {
continuationType = continuationType.superBound!;
}
node.asyncReturnType = continuationType == null
? TypeUsage.object
? DeclaredType.object
: continuationType.clone();
node.asyncReturnType!.accept(_KotlinTypeProcessor(function.returnType));

// The continuation object is always non-null.
node.returnType.type.annotations ??= [];
node.returnType.type.annotations!.add(Annotation.nonNull);
node.returnType.annotations ??= [];
node.returnType.annotations!.add(Annotation.nonNull);
} else {
node.returnType.accept(_KotlinTypeProcessor(function.returnType));
}
Expand Down
Loading
Loading