-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdex_descriptor.go
More file actions
144 lines (125 loc) · 3.65 KB
/
dex_descriptor.go
File metadata and controls
144 lines (125 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package codegen
import (
"strings"
"github.com/AndroidGoLab/binder/tools/pkg/parser"
"github.com/AndroidGoLab/binder/tools/pkg/resolver"
)
// aidlPrimitiveToDex maps AIDL primitive type names to their DEX
// type descriptors. These are the same as Java primitive descriptors.
var aidlPrimitiveToDex = map[string]string{
"boolean": "Z",
"byte": "B",
"char": "C",
"short": "S",
"int": "I",
"long": "J",
"float": "F",
"double": "D",
"void": "V",
}
// aidlKnownToDex maps well-known AIDL types (that don't need registry
// lookup) to their DEX type descriptors.
var aidlKnownToDex = map[string]string{
"String": "Ljava/lang/String;",
"CharSequence": "Ljava/lang/CharSequence;",
"IBinder": "Landroid/os/IBinder;",
"ParcelFileDescriptor": "Landroid/os/ParcelFileDescriptor;",
"FileDescriptor": "Ljava/io/FileDescriptor;",
}
// AIDLTypeToDexDescriptor converts an AIDL type specifier to its DEX
// type descriptor string. Uses the type registry to resolve short names
// to fully qualified names.
//
// Returns empty string if the type cannot be resolved.
func AIDLTypeToDexDescriptor(
ts *parser.TypeSpecifier,
registry *resolver.TypeRegistry,
currentPkg string,
) string {
if ts == nil {
return ""
}
name := ts.Name
// Array types: prepend "[" to the element descriptor.
if ts.IsArray {
elemTS := &parser.TypeSpecifier{
Name: ts.Name,
TypeArgs: ts.TypeArgs,
}
elemDesc := AIDLTypeToDexDescriptor(elemTS, registry, currentPkg)
if elemDesc == "" {
return ""
}
return "[" + elemDesc
}
// List<T> maps to Ljava/util/List; in DEX (generic erasure).
if name == "List" {
return "Ljava/util/List;"
}
// Map<K,V> maps to Ljava/util/Map;.
if name == "Map" {
return "Ljava/util/Map;"
}
// Primitives.
if desc, ok := aidlPrimitiveToDex[name]; ok {
return desc
}
// Well-known types.
if desc, ok := aidlKnownToDex[name]; ok {
return desc
}
// Try to resolve via the type registry.
qualifiedName := resolveQualifiedName(name, registry, currentPkg)
if qualifiedName == "" {
return ""
}
// Enums in AIDL are serialized as their backing type (int, long, etc.),
// not as object references. In DEX, an enum parameter appears as "I"
// (int), not "Landroid/system/keystore2/Domain;". Check the registry
// and return the backing type descriptor for enums.
if registry != nil {
if def, ok := registry.Lookup(qualifiedName); ok {
if enumDecl, isEnum := def.(*parser.EnumDecl); isEnum {
backingType := "int"
if enumDecl.BackingType != nil {
backingType = enumDecl.BackingType.Name
}
if desc, ok := aidlPrimitiveToDex[backingType]; ok {
return desc
}
}
}
}
// Convert dot-separated qualified name to DEX descriptor:
// "android.content.AttributionSource" → "Landroid/content/AttributionSource;"
return "L" + strings.ReplaceAll(qualifiedName, ".", "/") + ";"
}
// resolveQualifiedName resolves a type name to its fully qualified
// AIDL name. If the name already contains dots, it is assumed to be
// fully qualified. Otherwise, the registry is consulted.
func resolveQualifiedName(
name string,
registry *resolver.TypeRegistry,
currentPkg string,
) string {
// Already fully qualified.
if strings.Contains(name, ".") {
return name
}
if registry == nil {
return ""
}
// Try current package first: "android.bluetooth" + "." + "IBluetoothGattCallback".
if currentPkg != "" {
candidate := currentPkg + "." + name
if _, ok := registry.Lookup(candidate); ok {
return candidate
}
}
// Fall back to short-name lookup.
qualifiedName, _, ok := registry.LookupQualifiedByShortName(name)
if ok {
return qualifiedName
}
return ""
}