Skip to content

Commit c46b0cd

Browse files
[jni] Add lints (#1304)
* [jni] 1 - `avoid_catching_errors` lint (#1304) commit-id:604708d0 * [jni] 2 - enable `strict-casts` (#1305) commit-id:189f62a0 * [jni] 3 - enable `strict-inference` (#1306) commit-id:e7cedd32 * [jni] 4 - `collection_methods_unrelated_type` lint (#1307) commit-id:bbafa365 * [jni] 5 - `comment_references` lint (#1308) commit-id:e2a88619 * [jni] 6 - `directives_ordering` lint (#1309) commit-id:149f4316 * [jni] 7 - `lines_longer_than_80_chars` lint (#1310) commit-id:f2193f01 * [jni] 8 - `omit_local_variable_types` lint (#1311) commit-id:e36dc423 * [jni] 9 - `only_throw_errors` lint (#1312) commit-id:a36c6844 * [jni] 10 - `prefer_relative_imports` lint (#1313) commit-id:d0bed3a2 * [jni] 11 - `prefer_single_quotes` lint (#1314) commit-id:621e125c * [jni] 12 - `unnecessary_parenthesis` lint (#1315) commit-id:c4cddf62 * [jni] 13 - `unreachable_from_main` lint (#1316) commit-id:50ece28e * [jni] 14 - `use_super_parameters` lint (#1317) commit-id:fc5ebc97
1 parent 282b8af commit c46b0cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+562
-583
lines changed

pkgs/jni/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Added lifetime event handling for the thread-local JNI env. Now
44
`jvm.DetachNativeThread` is called when the thread detaches as recommended
55
[here](https://developer.android.com/training/articles/perf-jni#threads).
6+
- Removed `JValueChar.fromString` constructor.
67

78
## 0.9.2
89

pkgs/jni/analysis_options.yaml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,12 @@ analyzer:
55
todo: ignore
66
exclude: [build/**, third_party/**]
77
language:
8-
strict-casts: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
9-
strict-inference: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
8+
strict-casts: true
9+
strict-inference: true
1010
strict-raw-types: true
1111

1212
linter:
1313
rules:
1414
dangling_library_doc_comments: true
1515
prefer_final_locals: true
1616
prefer_const_declarations: true
17-
avoid_catching_errors: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
18-
avoid_dynamic_calls: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
19-
collection_methods_unrelated_type: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
20-
comment_references: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
21-
directives_ordering: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
22-
lines_longer_than_80_chars: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
23-
omit_local_variable_types: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
24-
only_throw_errors: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
25-
prefer_relative_imports: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
26-
prefer_single_quotes: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
27-
type_annotate_public_apis: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
28-
unnecessary_parenthesis: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
29-
unreachable_from_main: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.
30-
use_super_parameters: false # TODO(https://github.com/dart-lang/native/issues/1282): Fix.

pkgs/jni/bin/setup.dart

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@
55
import 'dart:io';
66

77
import 'package:args/args.dart';
8-
import 'package:package_config/package_config.dart';
9-
108
import 'package:jni/src/build_util/build_util.dart';
9+
import 'package:package_config/package_config.dart';
1110

1211
const jniNativeBuildDirective =
1312
'# jni_native_build (Build with jni:setup. Do not delete this line.)';
1413

1514
// When changing this constant here, also change corresponding path in
1615
// test/test_util.
17-
const _defaultRelativeBuildPath = "build/jni_libs";
16+
const _defaultRelativeBuildPath = 'build/jni_libs';
1817

19-
const _buildPath = "build-path";
20-
const _srcPath = "add-source";
18+
const _buildPath = 'build-path';
19+
const _srcPath = 'add-source';
2120
const _packageName = 'add-package';
22-
const _verbose = "verbose";
23-
const _cmakeArgs = "cmake-args";
21+
const _verbose = 'verbose';
22+
const _cmakeArgs = 'cmake-args';
2423

2524
Future<void> runCommand(
2625
String exec, List<String> args, String workingDir) async {
@@ -30,11 +29,11 @@ Future<void> runCommand(
3029
current += Platform.pathSeparator;
3130
}
3231
if (workingDir.startsWith(current)) {
33-
workingDir.replaceFirst(current, "");
32+
workingDir.replaceFirst(current, '');
3433
}
3534

3635
final cmd = "$exec ${args.join(" ")}";
37-
stderr.writeln("+ [$workingDir] $cmd");
36+
stderr.writeln('+ [$workingDir] $cmd');
3837
int status;
3938
if (options.verbose) {
4039
final process = await Process.start(
@@ -58,8 +57,8 @@ Future<void> runCommand(
5857
var out = process.stdout;
5958
var err = process.stderr;
6059
if (stdout.supportsAnsiEscapes) {
61-
out = "$ansiRed$out$ansiDefault";
62-
err = "$ansiRed$err$ansiDefault";
60+
out = '$ansiRed$out$ansiDefault';
61+
err = '$ansiRed$err$ansiDefault';
6362
}
6463
stdout.writeln(out);
6564
stderr.writeln(err);
@@ -72,11 +71,11 @@ Future<void> runCommand(
7271

7372
class Options {
7473
Options(ArgResults arg)
75-
: buildPath = arg[_buildPath],
76-
sources = arg[_srcPath],
77-
packages = arg[_packageName],
78-
cmakeArgs = arg[_cmakeArgs],
79-
verbose = arg[_verbose] ?? false;
74+
: buildPath = arg[_buildPath] as String?,
75+
sources = arg[_srcPath] as List<String>,
76+
packages = arg[_packageName] as List<String>,
77+
cmakeArgs = arg[_cmakeArgs] as List<String>,
78+
verbose = (arg[_verbose] as bool?) ?? false;
8079

8180
String? buildPath;
8281
List<String> sources;
@@ -100,11 +99,11 @@ void verboseLog(String msg) {
10099
Future<String> findSources(String packageName, String subDirectory) async {
101100
final packageConfig = await findPackageConfig(Directory.current);
102101
if (packageConfig == null) {
103-
throw UnsupportedError("Please run from project root.");
102+
throw UnsupportedError('Please run from project root.');
104103
}
105104
final package = packageConfig[packageName];
106105
if (package == null) {
107-
throw UnsupportedError("Cannot find package: $packageName");
106+
throw UnsupportedError('Cannot find package: $packageName');
108107
}
109108
return package.root.resolve(subDirectory).toFilePath();
110109
}
@@ -114,12 +113,12 @@ Future<String> findSources(String packageName, String subDirectory) async {
114113
Future<Map<String, String>> findDependencySources() async {
115114
final packageConfig = await findPackageConfig(Directory.current);
116115
if (packageConfig == null) {
117-
throw UnsupportedError("Please run the command from project root.");
116+
throw UnsupportedError('Please run the command from project root.');
118117
}
119118
final sources = <String, String>{};
120119
for (var package in packageConfig.packages) {
121-
final src = package.root.resolve("src/");
122-
final cmakeLists = src.resolve("CMakeLists.txt");
120+
final src = package.root.resolve('src/');
121+
final cmakeLists = src.resolve('CMakeLists.txt');
123122
final cmakeListsFile = File.fromUri(cmakeLists);
124123
if (cmakeListsFile.existsSync()) {
125124
final firstLine = cmakeListsFile.readAsLinesSync().first;
@@ -134,13 +133,13 @@ Future<Map<String, String>> findDependencySources() async {
134133
/// Returns the name of file built using sources in [cDir]
135134
String getTargetName(Directory cDir) {
136135
for (final file in cDir.listSync(recursive: true)) {
137-
if (file.path.endsWith(".c")) {
136+
if (file.path.endsWith('.c')) {
138137
final cFileName = file.uri.pathSegments.last;
139-
final librarySuffix = Platform.isWindows ? "dll" : "so";
138+
final librarySuffix = Platform.isWindows ? 'dll' : 'so';
140139
return cFileName.substring(0, cFileName.length - 1) + librarySuffix;
141140
}
142141
}
143-
throw Exception("Could not find a C file in ${cDir.path}");
142+
throw Exception('Could not find a C file in ${cDir.path}');
144143
}
145144

146145
void main(List<String> arguments) async {
@@ -161,8 +160,8 @@ void main(List<String> arguments) async {
161160
final rest = argResults.rest;
162161

163162
if (rest.isNotEmpty) {
164-
stderr.writeln("one or more unrecognized arguments: $rest");
165-
stderr.writeln("usage: dart run jni:setup <options>");
163+
stderr.writeln('one or more unrecognized arguments: $rest');
164+
stderr.writeln('usage: dart run jni:setup <options>');
166165
stderr.writeln(parser.usage);
167166
exitCode = 1;
168167
return;
@@ -175,19 +174,19 @@ void main(List<String> arguments) async {
175174
}
176175
if (sources.isEmpty) {
177176
final dependencySources = await findDependencySources();
178-
stderr.writeln("selecting source directories for dependencies: "
179-
"${dependencySources.keys}");
177+
stderr.writeln('selecting source directories for dependencies: '
178+
'${dependencySources.keys}');
180179
sources.addAll(dependencySources.values);
181180
} else {
182-
stderr.writeln("selecting source directories: $sources");
181+
stderr.writeln('selecting source directories: $sources');
183182
}
184183
if (sources.isEmpty) {
185184
stderr.writeln('No source paths to build!');
186185
exitCode = 1;
187186
return;
188187
}
189188

190-
final currentDirUri = Uri.directory(".");
189+
final currentDirUri = Uri.directory('.');
191190
final buildPath = options.buildPath ??
192191
currentDirUri.resolve(_defaultRelativeBuildPath).toFilePath();
193192
final buildDir = Directory(buildPath);
@@ -216,41 +215,41 @@ void main(List<String> arguments) async {
216215
return;
217216
}
218217

219-
verboseLog("srcPath: $srcPath");
220-
verboseLog("buildPath: $buildPath");
218+
verboseLog('srcPath: $srcPath');
219+
verboseLog('buildPath: $buildPath');
221220

222221
final targetFileUri = buildDir.uri.resolve(getTargetName(srcDir));
223222
final targetFile = File.fromUri(targetFileUri);
224223
if (!needsBuild(targetFile, srcDir)) {
225-
verboseLog("Last modified of ${targetFile.path}: "
226-
"${targetFile.lastModifiedSync()}.");
224+
verboseLog('Last modified of ${targetFile.path}: '
225+
'${targetFile.lastModifiedSync()}.');
227226
stderr.writeln('Target newer than source, skipping build.');
228227
continue;
229228
}
230229

231230
// Note: creating temp dir in .dart_tool/jni instead of SystemTemp
232231
// because latter can fail tests on Windows CI, when system temp is on
233232
// separate drive or something.
234-
final jniDirUri = Uri.directory(".dart_tool").resolve("jni");
233+
final jniDirUri = Uri.directory('.dart_tool').resolve('jni');
235234
final jniDir = Directory.fromUri(jniDirUri);
236235
await jniDir.create(recursive: true);
237-
final tempDir = await jniDir.createTemp("jni_native_build_");
236+
final tempDir = await jniDir.createTemp('jni_native_build_');
238237
final cmakeArgs = <String>[];
239238
cmakeArgs.addAll(options.cmakeArgs);
240239
// Pass absolute path of srcDir because cmake command is run in temp dir
241240
cmakeArgs.add(srcDir.absolute.path);
242-
await runCommand("cmake", cmakeArgs, tempDir.path);
243-
await runCommand("cmake", ["--build", "."], tempDir.path);
241+
await runCommand('cmake', cmakeArgs, tempDir.path);
242+
await runCommand('cmake', ['--build', '.'], tempDir.path);
244243
final dllDirUri =
245-
Platform.isWindows ? tempDir.uri.resolve("Debug") : tempDir.uri;
244+
Platform.isWindows ? tempDir.uri.resolve('Debug') : tempDir.uri;
246245
final dllDir = Directory.fromUri(dllDirUri);
247246
for (var entry in dllDir.listSync()) {
248247
verboseLog(entry.toString());
249248
final dllSuffix = Platform.isWindows
250-
? "dll"
249+
? 'dll'
251250
: Platform.isMacOS
252-
? "dylib"
253-
: "so";
251+
? 'dylib'
252+
: 'so';
254253
if (entry.path.endsWith(dllSuffix)) {
255254
final dllName = entry.uri.pathSegments.last;
256255
final target = buildDir.uri.resolve(dllName);

pkgs/jni/example/pubspec.lock

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,18 @@ packages:
213213
dependency: transitive
214214
description:
215215
name: leak_tracker
216-
sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
216+
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
217217
url: "https://pub.dev"
218218
source: hosted
219-
version: "10.0.5"
219+
version: "10.0.4"
220220
leak_tracker_flutter_testing:
221221
dependency: transitive
222222
description:
223223
name: leak_tracker_flutter_testing
224-
sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
224+
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
225225
url: "https://pub.dev"
226226
source: hosted
227-
version: "3.0.5"
227+
version: "3.0.3"
228228
leak_tracker_testing:
229229
dependency: transitive
230230
description:
@@ -261,18 +261,18 @@ packages:
261261
dependency: transitive
262262
description:
263263
name: material_color_utilities
264-
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
264+
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
265265
url: "https://pub.dev"
266266
source: hosted
267-
version: "0.11.1"
267+
version: "0.8.0"
268268
meta:
269269
dependency: transitive
270270
description:
271271
name: meta
272-
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
272+
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
273273
url: "https://pub.dev"
274274
source: hosted
275-
version: "1.15.0"
275+
version: "1.12.0"
276276
mime:
277277
dependency: transitive
278278
description:
@@ -309,10 +309,10 @@ packages:
309309
dependency: transitive
310310
description:
311311
name: platform
312-
sha256: "9b71283fc13df574056616011fb138fd3b793ea47cc509c189a6c3fa5f8a1a65"
312+
sha256: "12220bb4b65720483f8fa9450b4332347737cf8213dd2840d8b2c823e47243ec"
313313
url: "https://pub.dev"
314314
source: hosted
315-
version: "3.1.5"
315+
version: "3.1.4"
316316
plugin_platform_interface:
317317
dependency: transitive
318318
description:
@@ -450,26 +450,26 @@ packages:
450450
dependency: "direct dev"
451451
description:
452452
name: test
453-
sha256: "7ee44229615f8f642b68120165ae4c2a75fe77ae2065b1e55ae4711f6cf0899e"
453+
sha256: "7ee446762c2c50b3bd4ea96fe13ffac69919352bd3b4b17bac3f3465edc58073"
454454
url: "https://pub.dev"
455455
source: hosted
456-
version: "1.25.7"
456+
version: "1.25.2"
457457
test_api:
458458
dependency: transitive
459459
description:
460460
name: test_api
461-
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
461+
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
462462
url: "https://pub.dev"
463463
source: hosted
464-
version: "0.7.2"
464+
version: "0.7.0"
465465
test_core:
466466
dependency: transitive
467467
description:
468468
name: test_core
469-
sha256: "55ea5a652e38a1dfb32943a7973f3681a60f872f8c3a05a14664ad54ef9c6696"
469+
sha256: "2bc4b4ecddd75309300d8096f781c0e3280ca1ef85beda558d33fcbedc2eead4"
470470
url: "https://pub.dev"
471471
source: hosted
472-
version: "0.6.4"
472+
version: "0.6.0"
473473
typed_data:
474474
dependency: transitive
475475
description:
@@ -490,10 +490,10 @@ packages:
490490
dependency: transitive
491491
description:
492492
name: vm_service
493-
sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc
493+
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
494494
url: "https://pub.dev"
495495
source: hosted
496-
version: "14.2.4"
496+
version: "14.2.1"
497497
watcher:
498498
dependency: transitive
499499
description:

pkgs/jni/lib/internal_helpers_for_jnigen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import 'dart:ffi' as ffi;
1010

1111
export 'src/accessors.dart';
1212
export 'src/jni.dart' show ProtectedJniExtensions;
13-
export 'src/types.dart' show referenceType;
1413
export 'src/jreference.dart';
1514
export 'src/method_invocation.dart';
15+
export 'src/types.dart' show referenceType;
1616

1717
/// Temporary fix for the macOS arm64 varargs problem.
1818
///

pkgs/jni/lib/src/accessors.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'dart:ffi';
66

7-
import 'package:jni/jni.dart';
7+
import '../jni.dart';
88

99
void _check(JThrowablePtr exception) {
1010
if (exception != nullptr) {

pkgs/jni/lib/src/errors.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:jni/src/third_party/generated_bindings.dart';
5+
import 'third_party/generated_bindings.dart';
66

77
// TODO(#567): Add the fact that [JException] is now a [JObject] to the
88
// CHANGELOG.

pkgs/jni/lib/src/jarray.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import 'dart:typed_data';
99

1010
import 'package:collection/collection.dart';
1111
import 'package:ffi/ffi.dart';
12-
import 'package:jni/internal_helpers_for_jnigen.dart';
13-
import 'package:jni/src/third_party/generated_bindings.dart';
1412

13+
import '../internal_helpers_for_jnigen.dart';
1514
import 'jni.dart';
1615
import 'jobject.dart';
16+
import 'third_party/generated_bindings.dart';
1717
import 'types.dart';
1818

1919
final class JArrayType<E> extends JObjType<JArray<E>> {

0 commit comments

Comments
 (0)