Skip to content

Commit

Permalink
[jnigen] Support nullability annotations on non-types (#1894)
Browse files Browse the repository at this point in the history
  • Loading branch information
HosseinYousefi authored Jan 15, 2025
1 parent 87ef391 commit a6bb0f1
Show file tree
Hide file tree
Showing 17 changed files with 588 additions and 427 deletions.
2 changes: 2 additions & 0 deletions pkgs/jnigen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 0.13.1-wip

- Fixed a bug where Kotlin wildcards would crash the code generation.
- Support nullability annotations that are on Java elements like methods and
fields instead of directly on the return type or field type.

## 0.13.0

Expand Down
279 changes: 136 additions & 143 deletions pkgs/jnigen/example/in_app_java/lib/android_utils.dart

Large diffs are not rendered by default.

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 @@ -102,7 +102,8 @@ class Example extends jni$_.JObject {
reference.pointer,
_id_thinkBeforeAnswering as jni$_.JMethodIDPtr,
_$continuation.pointer)
.object<jni$_.JObject>(const jni$_.JObjectType());
.object<jni$_.JObject>(const jni$_.JObjectType())
.release();
_$continuation.release();
final $o =
jni$_.JGlobalReference(jni$_.JObjectPtr.fromAddress(await $p.first));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ protected AsmAnnotationVisitor(JavaAnnotation annotation) {
@Override
public void visit(String name, Object value) {
annotation.properties.put(name, value);
super.visit(name, value);
}

@Override
public void visitEnum(String name, String descriptor, String value) {
annotation.properties.put(
name, new JavaAnnotation.EnumVal(Type.getType(descriptor).getClassName(), value));
super.visitEnum(name, descriptor, value);
}

@Override
Expand Down Expand Up @@ -57,12 +59,14 @@ protected AnnotationArrayVisitor(List<Object> list) {
@Override
public void visit(String unused, Object value) {
list.add(value);
super.visit(unused, value);
}

@Override
public void visitEnum(String unused, String descriptor, String value) {
var type = Type.getType(descriptor);
list.add(new JavaAnnotation.EnumVal(type.getClassName(), value));
super.visitEnum(unused, descriptor, value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void visitFormalTypeParameter(String name) {
var typeParam = new TypeParam();
typeParam.name = name;
decl.typeParams.add(typeParam);
super.visitFormalTypeParameter(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void visitInnerClass(String name, String outerName, String innerName, int
visited.get(binaryName).modifiers = TypeUtils.access(access);
visited.get(binaryName).outerClassBinaryName = outerClass.get(binaryName);
}
super.visitInnerClass(name, outerName, innerName, access);
}

@Override
Expand Down Expand Up @@ -185,6 +186,7 @@ public AnnotationVisitor visitTypeAnnotation(
public void visitEnd() {
var classToAdd = popVisiting();
visited.put(classToAdd.binaryName, classToAdd);
super.visitEnd();
}

private ClassDecl peekVisiting() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void visitFormalTypeParameter(String name) {
var typeParam = new TypeParam();
typeParam.name = name;
method.typeParams.add(typeParam);
super.visitFormalTypeParameter(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected AsmMethodVisitor(Method method) {
@Override
public void visitParameter(String name, int access) {
paramNames.add(name);
super.visitParameter(name, access);
}

@Override
Expand Down Expand Up @@ -97,5 +98,6 @@ public void visitEnd() {
method.params.get(i).name = paramNames.get(i);
}
}
super.visitEnd();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void visitBaseType(char descriptor) {
}
typeUsage.shorthand = name;
typeUsage.type = new TypeUsage.PrimitiveType(name);
super.visitBaseType(descriptor);
}

@Override
Expand All @@ -70,6 +71,7 @@ public void visitTypeVariable(String name) {
typeUsage.kind = TypeUsage.Kind.TYPE_VARIABLE;
typeUsage.shorthand = name;
typeUsage.type = new TypeUsage.TypeVar(name);
super.visitTypeVariable(name);
}

@Override
Expand All @@ -79,13 +81,15 @@ public void visitClassType(String name) {
var components = name.split("[/$]");
var simpleName = components[components.length - 1];
typeUsage.type = new TypeUsage.DeclaredType(typeUsage.shorthand, simpleName, new ArrayList<>());
super.visitClassType(name);
}

@Override
public void visitTypeArgument() {
assert (typeUsage.type instanceof TypeUsage.DeclaredType);
var typeArg = new TypeUsage("?", TypeUsage.Kind.WILDCARD, new TypeUsage.Wildcard(null, null));
((TypeUsage.DeclaredType) typeUsage.type).params.add(typeArg);
super.visitTypeArgument();
}

@Override
Expand Down Expand Up @@ -125,5 +129,6 @@ public void visitInnerClassType(String name) {
declaredType.binaryName += "$" + name;
declaredType.simpleName = name;
declaredType.typeParamIndices.add(declaredType.params.size());
super.visitInnerClassType(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void visit(String name, Object value) {
case "xi":
extraInt = (int) value;
}
super.visit(name, value);
}

@Override
Expand Down
Loading

0 comments on commit a6bb0f1

Please sign in to comment.