Skip to content

Commit 803476c

Browse files
[jnigen] Fix doclet type parameter parsing (#1960)
1 parent 52f76ad commit 803476c

File tree

7 files changed

+42
-10
lines changed

7 files changed

+42
-10
lines changed

pkgs/jni/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
`Iterable`.
1212
- Added `JObject.isA`, which checks whether a `JObject` is a instance of a
1313
java class.
14-
- Do not require JWT when building for desktop.
14+
- Do not require JAWT when building for desktop.
1515
- Added `JByteArray.from`, which allows a `JByteArray` to be constructed
1616
from an `Iterable<int>`.
1717

pkgs/jnigen/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.14.0-wip
2+
3+
- Fixed a bug where the source parser would not have all of the type paremeters
4+
of the types.
5+
16
## 0.13.1
27

38
- Fixed a bug where Kotlin wildcards would crash the code generation.

pkgs/jnigen/java/src/main/java/com/github/dart_lang/jnigen/apisummarizer/doclet/ElementBuilders.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.github.dart_lang.jnigen.apisummarizer.elements.*;
88
import com.github.dart_lang.jnigen.apisummarizer.util.StreamUtil;
99
import com.sun.source.doctree.DocCommentTree;
10+
import java.util.ArrayList;
1011
import java.util.HashMap;
1112
import java.util.List;
1213
import java.util.stream.Collectors;
@@ -144,10 +145,22 @@ public TypeUsage typeUsage(TypeMirror type) {
144145
? env.elements.getBinaryName((TypeElement) element).toString()
145146
: element.getSimpleName().toString();
146147
assert (type instanceof DeclaredType);
147-
List<TypeUsage> params =
148+
List<TypeUsage> params = new ArrayList<>();
149+
List<Integer> typeParamIndices = new ArrayList<>();
150+
var enclosingType = ((DeclaredType) type).getEnclosingType();
151+
if (enclosingType instanceof DeclaredType) {
152+
var enclosingTypeUsage = (TypeUsage.DeclaredType) typeUsage(enclosingType).type;
153+
params.addAll(enclosingTypeUsage.params);
154+
typeParamIndices.addAll(enclosingTypeUsage.typeParamIndices);
155+
}
156+
if (!params.isEmpty()) {
157+
typeParamIndices.add(params.size());
158+
}
159+
params.addAll(
148160
((DeclaredType) type)
149-
.getTypeArguments().stream().map(this::typeUsage).collect(Collectors.toList());
161+
.getTypeArguments().stream().map(this::typeUsage).collect(Collectors.toList()));
150162
u.type = new TypeUsage.DeclaredType(name, element.getSimpleName().toString(), params);
163+
((TypeUsage.DeclaredType) u.type).typeParamIndices = typeParamIndices;
151164
break;
152165
}
153166
case TYPEVAR:

pkgs/jnigen/pubspec.yaml

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

55
name: jnigen
66
description: A Dart bindings generator for Java and Kotlin that uses JNI under the hood to interop with Java virtual machine.
7-
version: 0.13.1
7+
version: 0.14.0-wip
88
repository: https://github.com/dart-lang/native/tree/main/pkgs/jnigen
99
issue_tracker: https://github.com/dart-lang/native/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Ajnigen
1010

pkgs/jnigen/test/simple_package_test/generate.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ void compileJavaSources(String workingDir, List<String> files) async {
6363
}
6464
}
6565

66-
Config getConfig() {
66+
Config getConfig({SummarizerBackend backend = SummarizerBackend.asm}) {
6767
compileJavaSources(javaPath, javaFiles);
6868
final dartWrappersRoot = Uri.directory(
6969
join(testRoot, 'bindings'),
7070
);
7171
final config = Config(
7272
sourcePath: [Uri.directory(javaPath)],
7373
classPath: [Uri.directory(javaPath)],
74-
summarizerOptions: SummarizerOptions(backend: SummarizerBackend.asm),
74+
summarizerOptions: SummarizerOptions(backend: backend),
7575
classes: [
7676
'com.github.dart_lang.jnigen.simple_package',
7777
'com.github.dart_lang.jnigen.pkg2',

pkgs/jnigen/test/simple_package_test/generated_files_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
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:jnigen/src/config/config.dart';
6+
import 'package:test/test.dart';
7+
58
import '../test_util/test_util.dart';
69
import 'generate.dart';
710

@@ -12,4 +15,10 @@ void main() async {
1215
'Generate and compare bindings for simple_package java files',
1316
getConfig(),
1417
);
18+
19+
test('Generate and analyze bindings for simple_package java files - doclet',
20+
() async {
21+
await generateAndAnalyzeBindings(
22+
getConfig(backend: SummarizerBackend.doclet));
23+
});
1524
}

pkgs/jnigen/test/summary_test.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ void registerCommonTests(Classes classes) {
6464
expect(example.getMethod('getNumber').modifiers, isPublic);
6565
expect(example.getMethod('privateMethod').modifiers, isPrivate);
6666
expect(example.getMethod('protectedMethod').modifiers, isProtected);
67-
print(example.fields.map((f) => f.name).toList());
6867
expect(example.getField('OFF').modifiers, isPublic);
6968
expect(example.getField('number').modifiers, isPrivate);
7069
expect(example.getField('protectedField').modifiers, isProtected);
@@ -150,13 +149,19 @@ void registerCommonTests(Classes classes) {
150149
expect(mapType.params, hasLength(2));
151150
final strType = mapType.params[0];
152151
expect(strType.name, 'java.lang.String');
153-
// TODO(#141): Wildcard implementation.
154-
/*
155152
final wildcardType = mapType.params[1];
156153
expect(wildcardType.kind, equals(Kind.wildcard));
157154
expect((wildcardType.type as Wildcard).extendsBound?.name,
158155
equals('java.lang.CharSequence'));
159-
*/
156+
});
157+
158+
test('typeParameters', () {
159+
final grandParent = classes.getClass('generics', 'GrandParent');
160+
final stringParent = grandParent.getMethod('stringParent');
161+
final returnType = stringParent.returnType.type as DeclaredType;
162+
expect(returnType.params, hasLength(2));
163+
expect(returnType.params[0].type, isA<TypeVar>());
164+
expect(returnType.params[1].type, isA<DeclaredType>());
160165
});
161166

162167
test('superclass', () {

0 commit comments

Comments
 (0)