blob: 7e04289aa59da0b0a5f76e7fbd360c7b838b1530 [file] [log] [blame] [view]
Ian Vollickb99472e2019-03-07 21:35:261# Accessing C++ Switches In Java
2
3[TOC]
4
5## Introduction
6
7Accessing C++ switches in Java is implemented via a Python script which analyzes
Nate Fischereb70fb42020-08-22 15:25:558the C++ switches file and generates the corresponding Java class, based on a
9template file. The template file must be specified in the GN target.
Ian Vollickb99472e2019-03-07 21:35:2610
11## Usage
12
Nate Fischerac07b2622020-10-01 20:20:14131. Create a template file (ex. `FooSwitches.java.tmpl`). Change "Copyright
14 2020" to be whatever the year is at the time of writing (as you would for any
15 other file).
Ian Vollickb99472e2019-03-07 21:35:2616 ```java
Avi Drissman7b017a992022-09-07 15:50:3817 // Copyright 2020 The Chromium Authors
Ian Vollickb99472e2019-03-07 21:35:2618 // Use of this source code is governed by a BSD-style license that can be
19 // found in the LICENSE file.
20
Nate Fischer93203242019-10-16 15:31:5821 package org.chromium.foo;
Ian Vollickb99472e2019-03-07 21:35:2622
23 // Be sure to escape any curly braces in your template by doubling as
24 // follows.
Nate Fischer93203242019-10-16 15:31:5825 /**
26 * Contains command line switches that are specific to the foo project.
27 */
28 public final class FooSwitches {{
Ian Vollickb99472e2019-03-07 21:35:2629
Nate Fischer93203242019-10-16 15:31:5830 {NATIVE_STRINGS}
Ian Vollickb99472e2019-03-07 21:35:2631
Nate Fischer93203242019-10-16 15:31:5832 // Prevents instantiation.
33 private FooSwitches() {{}}
Ian Vollickb99472e2019-03-07 21:35:2634 }}
35 ```
36
Nate Fischereb70fb42020-08-22 15:25:55372. Add a new build target and add it to the `srcjar_deps` of an
38 `android_library` target:
Ian Vollickb99472e2019-03-07 21:35:2639
40 ```gn
Nate Fischereb70fb42020-08-22 15:25:5541 if (is_android) {
42 import("//build/config/android/rules.gni")
43 }
Ian Vollickb99472e2019-03-07 21:35:2644
Nate Fischereb70fb42020-08-22 15:25:5545 if (is_android) {
46 java_cpp_strings("java_switches_srcjar") {
47 # External code should depend on ":foo_java" instead.
48 visibility = [ ":*" ]
49 sources = [
50 "//base/android/foo_switches.cc",
51 ]
52 template = "//base/android/java_templates/FooSwitches.java.tmpl"
53 }
54
55 # If there's already an android_library target, you can add
56 # java_switches_srcjar to that target's srcjar_deps. Otherwise, the best
57 # practice is to create a new android_library just for this target.
58 android_library("foo_java") {
59 srcjar_deps = [ ":java_switches_srcjar" ]
60 }
Ian Vollickb99472e2019-03-07 21:35:2661 }
62 ```
63
Nate Fischereb70fb42020-08-22 15:25:55643. The generated file `out/Default/gen/.../org/chromium/foo/FooSwitches.java`
Nate Fischer93203242019-10-16 15:31:5865 would contain:
Ian Vollickb99472e2019-03-07 21:35:2666
67 ```java
Avi Drissman7b017a992022-09-07 15:50:3868 // Copyright $YEAR The Chromium Authors
Nate Fischer93203242019-10-16 15:31:5869 // Use of this source code is governed by a BSD-style license that can be
70 // found in the LICENSE file.
Ian Vollickb99472e2019-03-07 21:35:2671
Nate Fischer93203242019-10-16 15:31:5872 package org.chromium.foo;
73
74 /**
75 * Contains command line switches that are specific to the foo project.
76 */
77 public final class FooSwitches {
78
Ian Vollickb99472e2019-03-07 21:35:2679 // ...snip...
80
Nate Fischer93203242019-10-16 15:31:5881 // This following string constants were inserted by
82 // java_cpp_strings.py
83 // From
84 // ../../base/android/foo_switches.cc
85 // Into
86 // ../../base/android/java_templates/FooSwitches.java.tmpl
87
88 // Documentation for the C++ switch is copied here.
Ian Vollickb99472e2019-03-07 21:35:2689 public static final String SOME_SWITCH = "some-switch";
90
91 // ...snip...
Nate Fischer93203242019-10-16 15:31:5892
93 // Prevents instantiation.
94 private FooSwitches() {}
Ian Vollickb99472e2019-03-07 21:35:2695 }
96 ```
97
Nate Fischerac07b2622020-10-01 20:20:1498## See also
99* [Accessing C++ Enums In Java](android_accessing_cpp_enums_in_java.md)
100* [Accessing C++ Features In Java](android_accessing_cpp_features_in_java.md)
101
Ian Vollickb99472e2019-03-07 21:35:26102## Code
103* [Generator
104code](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_strings.py?dr=C&sq=package:chromium)
105and
106[Tests](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_strings_tests.py?dr=C&sq=package:chromium)
107* [GN
108template](https://cs.chromium.org/chromium/src/build/config/android/rules.gni?sq=package:chromium&dr=C)