-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[jnigen] Use .jar metadata to generate generic types (#158)
Closes #132. ASM backend, parses classes, fields and method signatures from the metadata to add generic type information to other-wise type-erased generics.
- Loading branch information
1 parent
bc631e4
commit cb5b9d7
Showing
22 changed files
with
1,195 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
469 changes: 469 additions & 0 deletions
469
pkgs/jnigen/example/in_app_java/src/android_utils/android_utils.c
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
.../main/java/com/github/dart_lang/jnigen/apisummarizer/disasm/AsmClassSignatureVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
package com.github.dart_lang.jnigen.apisummarizer.disasm; | ||
|
||
import com.github.dart_lang.jnigen.apisummarizer.elements.ClassDecl; | ||
import com.github.dart_lang.jnigen.apisummarizer.elements.TypeParam; | ||
import com.github.dart_lang.jnigen.apisummarizer.elements.TypeUsage; | ||
import org.objectweb.asm.signature.SignatureVisitor; | ||
|
||
public class AsmClassSignatureVisitor extends SignatureVisitor { | ||
private final ClassDecl decl; | ||
private int interfaceIndex = -1; | ||
|
||
public AsmClassSignatureVisitor(ClassDecl decl) { | ||
super(AsmConstants.API); | ||
this.decl = decl; | ||
} | ||
|
||
@Override | ||
public void visitFormalTypeParameter(String name) { | ||
var typeParam = new TypeParam(); | ||
typeParam.name = name; | ||
decl.typeParams.add(typeParam); | ||
} | ||
|
||
@Override | ||
public SignatureVisitor visitClassBound() { | ||
var typeUsage = new TypeUsage(); | ||
// ClassDecl initially has no type parameters. In visitFormalTypeParameter we add them | ||
// and sequentially visitClassBound and visitInterfaceBound. | ||
decl.typeParams.get(decl.typeParams.size() - 1).bounds.add(typeUsage); | ||
return new AsmTypeUsageSignatureVisitor(typeUsage); | ||
} | ||
|
||
@Override | ||
public SignatureVisitor visitInterfaceBound() { | ||
var typeUsage = new TypeUsage(); | ||
decl.typeParams.get(decl.typeParams.size() - 1).bounds.add(typeUsage); | ||
return new AsmTypeUsageSignatureVisitor(typeUsage); | ||
} | ||
|
||
@Override | ||
public SignatureVisitor visitSuperclass() { | ||
return new AsmTypeUsageSignatureVisitor(decl.superclass); | ||
} | ||
|
||
@Override | ||
public SignatureVisitor visitInterface() { | ||
interfaceIndex++; | ||
return new AsmTypeUsageSignatureVisitor(decl.interfaces.get(interfaceIndex)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...main/java/com/github/dart_lang/jnigen/apisummarizer/disasm/AsmMethodSignatureVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
package com.github.dart_lang.jnigen.apisummarizer.disasm; | ||
|
||
import com.github.dart_lang.jnigen.apisummarizer.elements.Method; | ||
import com.github.dart_lang.jnigen.apisummarizer.elements.TypeParam; | ||
import com.github.dart_lang.jnigen.apisummarizer.elements.TypeUsage; | ||
import org.objectweb.asm.signature.SignatureVisitor; | ||
|
||
public class AsmMethodSignatureVisitor extends SignatureVisitor { | ||
private final Method method; | ||
private int paramIndex = -1; | ||
|
||
public AsmMethodSignatureVisitor(Method method) { | ||
super(AsmConstants.API); | ||
this.method = method; | ||
} | ||
|
||
@Override | ||
public void visitFormalTypeParameter(String name) { | ||
var typeParam = new TypeParam(); | ||
typeParam.name = name; | ||
method.typeParams.add(typeParam); | ||
} | ||
|
||
@Override | ||
public SignatureVisitor visitClassBound() { | ||
var typeUsage = new TypeUsage(); | ||
// Method initially has no type parameters. In visitFormalTypeParameter we add them | ||
// and sequentially visitClassBound and visitInterfaceBound. | ||
method.typeParams.get(method.typeParams.size() - 1).bounds.add(typeUsage); | ||
return new AsmTypeUsageSignatureVisitor(typeUsage); | ||
} | ||
|
||
@Override | ||
public SignatureVisitor visitInterfaceBound() { | ||
var typeUsage = new TypeUsage(); | ||
method.typeParams.get(method.typeParams.size() - 1).bounds.add(typeUsage); | ||
return new AsmTypeUsageSignatureVisitor(typeUsage); | ||
} | ||
|
||
@Override | ||
public SignatureVisitor visitReturnType() { | ||
return new AsmTypeUsageSignatureVisitor(method.returnType); | ||
} | ||
|
||
@Override | ||
public SignatureVisitor visitParameterType() { | ||
paramIndex++; | ||
return new AsmTypeUsageSignatureVisitor(method.params.get(paramIndex).type); | ||
} | ||
|
||
@Override | ||
public SignatureVisitor visitExceptionType() { | ||
// Do nothing. | ||
return new AsmTypeUsageSignatureVisitor(new TypeUsage()); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...n/java/com/github/dart_lang/jnigen/apisummarizer/disasm/AsmTypeUsageSignatureVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
package com.github.dart_lang.jnigen.apisummarizer.disasm; | ||
|
||
import com.github.dart_lang.jnigen.apisummarizer.elements.TypeUsage; | ||
import java.util.ArrayList; | ||
import org.objectweb.asm.signature.SignatureVisitor; | ||
|
||
public class AsmTypeUsageSignatureVisitor extends SignatureVisitor { | ||
private final TypeUsage typeUsage; | ||
|
||
public AsmTypeUsageSignatureVisitor(TypeUsage typeUsage) { | ||
super(AsmConstants.API); | ||
this.typeUsage = typeUsage; | ||
} | ||
|
||
@Override | ||
public void visitBaseType(char descriptor) { | ||
typeUsage.kind = TypeUsage.Kind.PRIMITIVE; | ||
var name = ""; | ||
switch (descriptor) { | ||
case 'Z': | ||
name = "boolean"; | ||
break; | ||
case 'B': | ||
name = "byte"; | ||
break; | ||
case 'C': | ||
name = "char"; | ||
break; | ||
case 'D': | ||
name = "double"; | ||
break; | ||
case 'F': | ||
name = "float"; | ||
break; | ||
case 'I': | ||
name = "int"; | ||
break; | ||
case 'J': | ||
name = "long"; | ||
break; | ||
case 'L': | ||
name = "object"; | ||
break; | ||
case 'S': | ||
name = "short"; | ||
break; | ||
case 'V': | ||
name = "void"; | ||
break; | ||
} | ||
typeUsage.shorthand = name; | ||
typeUsage.type = new TypeUsage.PrimitiveType(name); | ||
} | ||
|
||
@Override | ||
public SignatureVisitor visitArrayType() { | ||
typeUsage.kind = TypeUsage.Kind.ARRAY; | ||
typeUsage.shorthand = "java.lang.Object[]"; | ||
var elementType = new TypeUsage(); | ||
typeUsage.type = new TypeUsage.Array(elementType); | ||
return new AsmTypeUsageSignatureVisitor(elementType); | ||
} | ||
|
||
@Override | ||
public void visitTypeVariable(String name) { | ||
typeUsage.kind = TypeUsage.Kind.TYPE_VARIABLE; | ||
typeUsage.shorthand = name; | ||
typeUsage.type = new TypeUsage.TypeVar(name); | ||
} | ||
|
||
@Override | ||
public void visitClassType(String name) { | ||
typeUsage.kind = TypeUsage.Kind.DECLARED; | ||
typeUsage.shorthand = name.substring(0, name.length()).replace('/', '.'); | ||
var components = name.split("[/$]"); | ||
var simpleName = components[components.length - 1]; | ||
typeUsage.type = new TypeUsage.DeclaredType(name, simpleName, new ArrayList<>()); | ||
} | ||
|
||
@Override | ||
public SignatureVisitor visitTypeArgument(char wildcard) { | ||
// TODO(#141) support wildcards | ||
// TODO(#144) support extend/super clauses | ||
assert (typeUsage.type instanceof TypeUsage.DeclaredType); | ||
var typeArg = new TypeUsage(); | ||
((TypeUsage.DeclaredType) typeUsage.type).params.add(typeArg); | ||
return new AsmTypeUsageSignatureVisitor(typeArg); | ||
} | ||
|
||
@Override | ||
public void visitInnerClassType(String name) { | ||
super.visitInnerClassType(name); | ||
// TODO(#139) support nested generic classes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.