diff --git a/pkgs/jnigen/CHANGELOG.md b/pkgs/jnigen/CHANGELOG.md index c180922bf..a8d51a74e 100644 --- a/pkgs/jnigen/CHANGELOG.md +++ b/pkgs/jnigen/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.12.2-wip + +- Now excludes invalid identifiers by default. + ## 0.12.1 - Support implementing generic functions in interfaces. diff --git a/pkgs/jnigen/lib/src/bindings/excluder.dart b/pkgs/jnigen/lib/src/bindings/excluder.dart index 6169875ed..c9fba9ab5 100644 --- a/pkgs/jnigen/lib/src/bindings/excluder.dart +++ b/pkgs/jnigen/lib/src/bindings/excluder.dart @@ -11,6 +11,21 @@ extension on ClassMember { bool get isPrivate => !isPublic; } +// TODO(https://github.com/dart-lang/native/issues/1164): Kotlin compiler +// appends the method name with a dash and a hash code when arguments contain +// inline classes. This is because inline classes do not have any runtime type +// and the typical operator overloading supported by JVM cannot work for them. +// +// Once we support inline classes, we can relax the following constraints. +final _validDartIdentifier = RegExp(r'^[a-zA-Z_$][a-zA-Z0-9_$]*$'); + +extension on String { + bool get isInvalidDartIdentifier => + !_validDartIdentifier.hasMatch(this) && + this != '' && + this != ''; +} + class Excluder extends Visitor { final Config config; @@ -24,6 +39,11 @@ class Excluder extends Visitor { if (excluded) { log.fine('Excluded class ${classDecl.binaryName}'); } + if (classDecl.name.isInvalidDartIdentifier) { + log.warning('Excluded class ${classDecl.binaryName}: the name is not a' + ' valid Dart identifer'); + return true; + } return excluded; }); final classExcluder = _ClassExcluder(config); @@ -51,6 +71,12 @@ class _ClassExcluder extends Visitor { if (excluded) { log.fine('Excluded method ${node.binaryName}#${method.name}'); } + if (method.name.isInvalidDartIdentifier) { + log.warning( + 'Excluded method ${node.binaryName}#${method.name}: the name is not' + ' a valid Dart identifer'); + return false; + } return !excluded; }).toList(); node.fields = node.fields.where((field) { @@ -59,6 +85,12 @@ class _ClassExcluder extends Visitor { if (excluded) { log.fine('Excluded field ${node.binaryName}#${field.name}'); } + if (field.name.isInvalidDartIdentifier) { + log.warning( + 'Excluded field ${node.binaryName}#${field.name}: the name is not' + ' a valid Dart identifer'); + return false; + } return !excluded; }).toList(); } diff --git a/pkgs/jnigen/pubspec.yaml b/pkgs/jnigen/pubspec.yaml index 5aa30b5cb..ff495d62e 100644 --- a/pkgs/jnigen/pubspec.yaml +++ b/pkgs/jnigen/pubspec.yaml @@ -4,7 +4,7 @@ name: jnigen description: A Dart bindings generator for Java and Kotlin that uses JNI under the hood to interop with Java virtual machine. -version: 0.12.1 +version: 0.12.2-wip repository: https://github.com/dart-lang/native/tree/main/pkgs/jnigen environment: