Skip to content

Commit d4c6bf7

Browse files
authored
[jnigen] Throw actionable error for wrong Java version (dart-lang#3130)
1 parent 7d68fee commit d4c6bf7

4 files changed

Lines changed: 103 additions & 2 deletions

File tree

‎pkgs/jnigen/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
`bool`.
88
- Kotlin suspend functions with no result (a return type of `Unit`) now return
99
`Future<void>` in Dart instead of `Future<JObject>`.
10+
- Improve error message for unsupported Java class file versions in summary generation.
1011

1112
## 0.15.0
1213

‎pkgs/jnigen/lib/src/summary/summary.dart‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ class SummaryParseException implements Exception {
2222
String toString() => message;
2323
}
2424

25+
final _unsupportedClassFileVersionRegex = RegExp(
26+
r'Unsupported class file major version\s+(\d+)',
27+
);
28+
29+
String? getActionableSummaryParseMessage(String stderr) {
30+
final match = _unsupportedClassFileVersionRegex.firstMatch(stderr);
31+
if (match == null) {
32+
return null;
33+
}
34+
35+
final majorVersion = match.group(1);
36+
return 'Cannot generate summary: Java class file version $majorVersion is '
37+
'not supported by the summarizer. This usually means your input classes '
38+
'were compiled with a newer JDK target than JNIgen supports. Use a '
39+
'supported JDK version (11 to 17) (see JNIgen README), or recompile '
40+
'your Java inputs with a lower target (for example: javac --release '
41+
'17 <your-java-files>).';
42+
}
43+
2544
/// A command based summary source which calls the ApiSummarizer command.
2645
/// [sourcePaths] and [classPaths] can be provided for the summarizer to find
2746
/// required dependencies. The [classes] argument specifies the fully qualified
@@ -179,9 +198,12 @@ Future<Classes> getSummary(Config config) async {
179198
log.info('Parsing inputs took ${stopwatch.elapsedMilliseconds} ms');
180199
} on Exception catch (e) {
181200
await process.exitCode;
201+
final stderr = stderrBuffer.toString();
202+
final message = getActionableSummaryParseMessage(stderr) ??
203+
'Cannot generate summary: $e';
182204
throw SummaryParseException.withStderr(
183-
stderrBuffer.toString(),
184-
'Cannot generate summary: $e',
205+
stderr,
206+
message,
185207
);
186208
} finally {
187209
log.writeSectionToFile('summarizer logs', stderrBuffer.toString());
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:jnigen/src/summary/summary.dart';
6+
import 'package:test/test.dart';
7+
8+
void main() {
9+
group('getActionableSummaryParseMessage', () {
10+
test('returns actionable message for unsupported classfile version', () {
11+
const stderr = '''
12+
Exception in thread "main" java.lang.RuntimeException
13+
Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 66
14+
''';
15+
16+
final message = getActionableSummaryParseMessage(stderr);
17+
18+
expect(message, isNotNull);
19+
expect(message, contains('class file version 66'));
20+
expect(message, contains('JDK version (11 to 17) (see JNIgen README)'));
21+
expect(message, contains('javac --release 17'));
22+
});
23+
24+
test('returns null for unrelated stderr', () {
25+
const stderr = 'Not found: [com.github.dart_lang.jnigen.DoesNotExist]';
26+
27+
final message = getActionableSummaryParseMessage(stderr);
28+
29+
expect(message, isNull);
30+
});
31+
});
32+
}

‎pkgs/jnigen/test/summary_generation_test.dart‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@Tags(['summarizer_test'])
99
library;
1010

11+
import 'dart:io';
1112
import 'dart:math';
1213

1314
import 'package:jnigen/src/config/config.dart';
@@ -141,6 +142,51 @@ void main() async {
141142
testAllCases(classPath: [targetDir.path]);
142143
});
143144

145+
group('Test unsupported class file version errors', () {
146+
final sourceDir = tempDir.createTempSync('unsupported_classfile_source_');
147+
final classesDir = tempDir.createTempSync('unsupported_classfile_classes_');
148+
149+
setUpAll(() async {
150+
final packageDir = Directory(join(sourceDir.path, 'com', 'example'))
151+
..createSync(recursive: true);
152+
final javaFile = File(join(packageDir.path, 'Hello.java'));
153+
javaFile.writeAsStringSync('''
154+
package com.example;
155+
public class Hello {
156+
public int value() { return 42; }
157+
}
158+
''');
159+
await compileJavaFiles(sourceDir, classesDir);
160+
161+
//in class file encoding the version as 74 so unsupported
162+
//and trigggers for test
163+
final classFile = File(
164+
join(classesDir.path, 'com', 'example', 'Hello.class'),
165+
);
166+
final classFileBytes = classFile.readAsBytesSync();
167+
classFileBytes[6] = 0x00;
168+
classFileBytes[7] = 0x4A;
169+
classFile.writeAsBytesSync(classFileBytes, flush: true);
170+
});
171+
172+
test('- should provide actionable guidance for unsupported versions',
173+
() async {
174+
final config = getSummaryGenerationConfig(classPath: [classesDir.path]);
175+
config.classes = ['com.example.Hello'];
176+
177+
try {
178+
await getSummary(config);
179+
} on SummaryParseException catch (e) {
180+
expect(e.message, contains('Java class file version 74'));
181+
expect(e.message, contains('supported JDK version (11 to 17)'));
182+
expect(e.message, contains('javac --release 17'));
183+
expect(e.message, isNot(contains('FormatException')));
184+
return;
185+
}
186+
throw AssertionError('No exception was caught');
187+
});
188+
});
189+
144190
// Test summary generation from combination of a source and class path
145191
group('Test summary generation from combination', () {
146192
final targetDir = tempDir.createTempSync('combination_test_');

0 commit comments

Comments
 (0)