estevenson | c0b135ff | 2016-11-17 16:03:51 | [diff] [blame] | 1 | # Accessing C++ Enums In Java |
| 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | ## Introduction |
| 6 | |
| 7 | Accessing C++ enums in Java is implemented via a Python script which analyzes |
| 8 | the C++ enum and spits out the corresponding Java class. The enum needs to be |
| 9 | annotated in a particular way. By default, the generated class name will be the |
| 10 | same as the name of the enum. If all the names of the enum values are prefixed |
| 11 | with the MACRO\_CASED\_ name of the enum those prefixes will be stripped from |
| 12 | the Java version. |
| 13 | |
| 14 | ## Features |
| 15 | * Customize the package name of the generated class using the |
| 16 | `GENERATED_JAVA_ENUM_PACKAGE` directive (required) |
| 17 | * Customize the class name using the `GENERATED_JAVA_CLASS_NAME_OVERRIDE` |
| 18 | directive (optional) |
| 19 | * Strip enum entry prefixes to make the generated classes less verbose using |
| 20 | the `GENERATED_JAVA_PREFIX_TO_STRIP` directive (optional) |
| 21 | * Supports |
| 22 | [`@IntDef`](https://developer.android.com/reference/android/support/annotation/IntDef.html) |
qyearsley | c0dc6f4 | 2016-12-02 22:13:39 | [diff] [blame] | 23 | * Copies comments that directly precede enum entries into the generated Java |
estevenson | c0b135ff | 2016-11-17 16:03:51 | [diff] [blame] | 24 | class |
| 25 | |
| 26 | ## Usage |
| 27 | |
| 28 | 1. Add directives to your C++ enum |
| 29 | |
| 30 | ```cpp |
| 31 | // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome |
| 32 | // GENERATED_JAVA_CLASS_NAME_OVERRIDE: FooBar |
| 33 | // GENERATED_JAVA_PREFIX_TO_STRIP: BAR_ |
| 34 | enum SomeEnum { |
| 35 | BAR_A, |
| 36 | BAR_B, |
| 37 | BAR_C = BAR_B, |
| 38 | }; |
| 39 | ``` |
| 40 | |
| 41 | 2. Add a new build target |
| 42 | |
Nate Fischer | 93c5915 | 2019-02-05 01:25:08 | [diff] [blame] | 43 | ```gn |
estevenson | c0b135ff | 2016-11-17 16:03:51 | [diff] [blame] | 44 | import("//build/config/android/rules.gni") |
| 45 | |
| 46 | java_cpp_enum("foo_generated_enum") { |
| 47 | sources = [ |
| 48 | "base/android/native_foo_header.h", |
| 49 | ] |
| 50 | } |
| 51 | ``` |
| 52 | |
| 53 | 3. Add the new target to the desired android_library targets srcjar_deps: |
| 54 | |
Nate Fischer | 93c5915 | 2019-02-05 01:25:08 | [diff] [blame] | 55 | ```gn |
estevenson | c0b135ff | 2016-11-17 16:03:51 | [diff] [blame] | 56 | android_library("base_java") { |
| 57 | srcjar_deps = [ |
| 58 | ":foo_generated_enum", |
| 59 | ] |
| 60 | } |
| 61 | ``` |
| 62 | |
| 63 | 4. The generated file `org/chromium/chrome/FooBar.java` would contain: |
| 64 | |
| 65 | ```java |
| 66 | package org.chromium.chrome; |
| 67 | |
| 68 | import android.support.annotation.IntDef; |
| 69 | |
| 70 | import java.lang.annotation.Retention; |
| 71 | import java.lang.annotation.RetentionPolicy; |
| 72 | |
dgn | 2b5c5e82 | 2017-05-12 20:33:39 | [diff] [blame] | 73 | @IntDef({ |
| 74 | FooBar.A, FooBar.B, FooBar.C |
| 75 | }) |
| 76 | @Retention(RetentionPolicy.SOURCE) |
| 77 | public @interface FooBar { |
| 78 | int A = 0; |
| 79 | int B = 1; |
| 80 | int C = 1; |
estevenson | c0b135ff | 2016-11-17 16:03:51 | [diff] [blame] | 81 | } |
| 82 | ``` |
| 83 | |
| 84 | ## Formatting Notes |
| 85 | |
| 86 | * Handling long package names: |
| 87 | |
Nate Fischer | 93c5915 | 2019-02-05 01:25:08 | [diff] [blame] | 88 | ```cpp |
estevenson | c0b135ff | 2016-11-17 16:03:51 | [diff] [blame] | 89 | // GENERATED_JAVA_ENUM_PACKAGE: ( |
| 90 | // org.chromium.chrome.this.package.is.too.long.to.fit.on.a.single.line) |
| 91 | ``` |
| 92 | |
| 93 | * Enum entries |
| 94 | * Single line enums should look like this: |
| 95 | |
Nate Fischer | 93c5915 | 2019-02-05 01:25:08 | [diff] [blame] | 96 | ```cpp |
| 97 | // GENERATED_JAVA_ENUM_PACKAGE: org.foo |
| 98 | enum NotificationActionType { BUTTON, TEXT }; |
| 99 | ``` |
estevenson | c0b135ff | 2016-11-17 16:03:51 | [diff] [blame] | 100 | |
| 101 | * Multi-line enums should have one enum entry per line, like this: |
| 102 | |
Nate Fischer | 93c5915 | 2019-02-05 01:25:08 | [diff] [blame] | 103 | ```cpp |
| 104 | // GENERATED_JAVA_ENUM_PACKAGE: org.foo |
| 105 | enum NotificationActionType { |
| 106 | BUTTON, |
| 107 | TEXT |
| 108 | }; |
| 109 | ``` |
estevenson | c0b135ff | 2016-11-17 16:03:51 | [diff] [blame] | 110 | |
| 111 | * Multi-line enum entries are allowed but should be formatted like this: |
| 112 | |
Nate Fischer | 93c5915 | 2019-02-05 01:25:08 | [diff] [blame] | 113 | ```cpp |
| 114 | // GENERATED_JAVA_ENUM_PACKAGE: org.foo |
| 115 | enum NotificationActionType { |
| 116 | LongKeyNumberOne, |
| 117 | LongKeyNumberTwo, |
| 118 | ... |
| 119 | LongKeyNumberThree = |
| 120 | LongKeyNumberOne | LongKeyNumberTwo | ... |
| 121 | }; |
| 122 | ``` |
estevenson | c0b135ff | 2016-11-17 16:03:51 | [diff] [blame] | 123 | |
| 124 | * Preserving comments |
| 125 | |
| 126 | ```cpp |
| 127 | // GENERATED_JAVA_ENUM_PACKAGE: org.chromium |
| 128 | enum CommentEnum { |
| 129 | // This comment will be preserved. |
| 130 | ONE, |
| 131 | TWO, // This comment will NOT be preserved. |
| 132 | THREE |
| 133 | } |
| 134 | ``` |
| 135 | |
| 136 | ```java |
| 137 | ... |
dgn | 2b5c5e82 | 2017-05-12 20:33:39 | [diff] [blame] | 138 | public @interface CommentEnum { |
estevenson | c0b135ff | 2016-11-17 16:03:51 | [diff] [blame] | 139 | ... |
| 140 | /** |
| 141 | * This comment will be preserved. |
| 142 | */ |
dgn | 2b5c5e82 | 2017-05-12 20:33:39 | [diff] [blame] | 143 | int ONE = 0; |
| 144 | int TWO = 1; |
| 145 | int THREE = 2; |
estevenson | c0b135ff | 2016-11-17 16:03:51 | [diff] [blame] | 146 | } |
| 147 | ``` |
| 148 | |
| 149 | ## Code |
| 150 | * [Generator |
| 151 | code](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_enum.py?dr=C&sq=package:chromium) |
| 152 | and |
| 153 | [Tests](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_enum_tests.py?dr=C&q=java_cpp_enum_tests&sq=package:chromium&l=1) |
| 154 | * [GN |
| 155 | template](https://cs.chromium.org/chromium/src/build/config/android/rules.gni?q=java_cpp_enum.py&sq=package:chromium&dr=C&l=458) |