xref: /aosp_15_r20/external/cronet/third_party/jni_zero/codegen/placeholder_java_type.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2024 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5
6def Generate(java_class,
7             nested_classes,
8             script_name,
9             proxy_interface=None,
10             proxy_natives=None):
11  """Generate an empty placeholder java class with a given name and package.
12
13  The placeholder files allow compiling FooJni.java without requiring access to
14  Foo.java or any of its deps/imports.
15  """
16  sb = []
17  sb.append(f"""\
18//
19// This file is an empty placeholder. It was generated by {script_name}
20//
21
22package {java_class.package_with_dots};
23
24public interface {java_class.name} {{
25""")
26  if nested_classes:
27    for clazz in nested_classes:
28      # The proxy interface has to exist for real in the placeholder file for
29      # Foo.java because FooJni implements it and uses @Override.
30      if clazz == proxy_interface:
31        sb.append(f'  public interface {clazz.nested_name} {{\n')
32        for native in proxy_natives:
33          parameter_list = ', '.join(f'{param.java_type.to_java()} {param.name}'
34                                     for param in native.params)
35          sb.append(
36              f'    public {native.return_type.to_java()} {native.name}({parameter_list});\n'
37          )
38        sb.append('  }\n')
39      else:
40        sb.append(f'  public interface {clazz.nested_name} {{}}\n')
41
42  sb.append('}\n')
43  return ''.join(sb)
44