• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2
3#
4# Copyright (C) 2019 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19import sys
20
21DEFAULT_TYPES_TO_JNI = {
22  "boolean": "Z",
23  "byte": "B",
24  "char": "C",
25  "short": "S",
26  "int": "I",
27  "long": "J",
28  "float": "F",
29  "double": "D",
30  "void": "V",
31  "String": "Ljava/lang/String;"
32}
33
34class AIDLMalformedSignatureException(Exception):
35  """Line containing AIDL signature is invalid"""
36
37def convert_type(aidl_type):
38  if aidl_type.endswith("[]"):
39    return "[" + convert_type(aidl_type[:-2])
40  if aidl_type in DEFAULT_TYPES_TO_JNI:
41    return DEFAULT_TYPES_TO_JNI[aidl_type]
42  elif aidl_type.startswith("List<") | aidl_type.startswith("java.util.List<"):
43    return "Ljava/util/List;"
44  else:
45    return "L" + aidl_type.replace(".", "/") + ";"
46
47def convert_method(aidl_signature):
48  aidl_signature = aidl_signature.split("|")
49  if len(aidl_signature) != 4:
50    raise AIDLMalformedSignatureException()
51  class_name, method_name, args, return_type = aidl_signature
52  # Filter out empty arguments since there will be trailing commas
53  args = [x for x in args.split(",") if x]
54  jni_signature = convert_type(class_name)
55  jni_signature += "->"
56  jni_signature += method_name
57  jni_signature += "("
58  params = [convert_type(x) for x in args]
59  jni_signature += "".join(params)
60  jni_signature += ")"
61  jni_signature += convert_type(return_type)
62  return jni_signature
63
64def main(argv):
65  if len(argv) != 3:
66    print("Usage: %s <aidl-mappings> <jni-signature-mappings>" % argv[0])
67    return -1
68
69  aidl_mappings, jni_signature_mappings = argv[1:]
70
71  line_index = 0
72  skip_line = False
73  with open(aidl_mappings) as input_file:
74    with open(jni_signature_mappings, "w") as output_file:
75      for line in input_file:
76        if skip_line:
77          skip_line = False
78        elif line_index % 2 == 1:
79          output_file.write(line)
80        else:
81          try:
82            stripped_line = line.strip()
83            output_file.write(convert_method(line.strip()))
84            output_file.write("\n")
85          except AIDLMalformedSignatureException:
86            print("Malformed signature %s . Skipping..." % stripped_line)
87            # The next line contains the location, need to skip it
88            skip_line = True
89        line_index += 1
90
91if __name__ == "__main__":
92  sys.exit(main(sys.argv))
93