File: proxy_impl_java.py

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (84 lines) | stat: -rw-r--r-- 2,684 bytes parent folder | download | duplicates (2)
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
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import java_types


def Generate(jni_obj, *, gen_jni_class, script_name):
  proxy_class = java_types.JavaClass(
      f'{jni_obj.java_class.full_name_with_slashes}Jni')
  visibility = 'public ' if jni_obj.proxy_visibility == 'public' else ''
  interface_name = jni_obj.proxy_interface.name_with_dots
  gen_jni = gen_jni_class.name
  type_resolver = java_types.TypeResolver(proxy_class)
  type_resolver.imports = list(jni_obj.type_resolver.imports)

  sb = []
  sb.append(f"""\
//
// This file was generated by {script_name}
//
package {jni_obj.java_class.class_without_prefix.package_with_dots};

import org.jni_zero.CheckDiscard;
import org.jni_zero.JniStaticTestMocker;
import org.jni_zero.NativeLibraryLoadedStatus;
import {gen_jni_class.full_name_with_dots};
""")

  # Copy over all imports (some will be unused, but oh well).
  for c in type_resolver.imports:
    sb.append(f'import {c.full_name_with_dots};\n')

  sb.append(f"""
@CheckDiscard("crbug.com/993421")
{visibility}class {proxy_class.name} implements {interface_name} {{
  private static {interface_name} testInstance;

  public static final JniStaticTestMocker<{interface_name}> TEST_HOOKS =
      new JniStaticTestMocker<{interface_name}>() {{
    @Override
    public void setInstanceForTesting({interface_name} instance) {{
      if (!{gen_jni}.TESTING_ENABLED) {{
        throw new RuntimeException(
            "Tried to set a JNI mock when mocks aren't enabled!");
      }}
      testInstance = instance;
    }}
  }};
""")

  for native in jni_obj.proxy_natives:
    call_params = native.params.to_call_str()
    sig_params = native.params.to_java_declaration(type_resolver)
    return_type_str = native.return_type.to_java(type_resolver)
    return_prefix = ''
    if not native.return_type.is_void():
      return_prefix = f'return ({return_type_str}) '

    sb.append(f"""
  @Override
  public {return_type_str} {native.name}({sig_params}) {{
    {return_prefix}{gen_jni}.{native.proxy_name}({call_params});
  }}
""")

  sb.append(f"""
  public static {interface_name} get() {{
    if ({gen_jni}.TESTING_ENABLED) {{
      if (testInstance != null) {{
        return testInstance;
      }}
      if ({gen_jni}.REQUIRE_MOCK) {{
        throw new UnsupportedOperationException(
            "No mock found for the native implementation of {interface_name}. "
            + "The current configuration requires implementations be mocked.");
      }}
    }}
    NativeLibraryLoadedStatus.checkLoaded();
    return new {proxy_class.name}();
  }}
}}
""")
  return ''.join(sb)