OLD | NEW |
(Empty) | |
| 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/I
ntDef.html) |
| 23 * Copies comments that directly preceed enum entries into the generated Java |
| 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 |
| 43 ``` |
| 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 |
| 55 ``` |
| 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 |
| 73 public class FooBar { |
| 74 @IntDef({ |
| 75 A, B, C |
| 76 }) |
| 77 @Retention(RetentionPolicy.SOURCE) |
| 78 public @interface FooBarEnum {} |
| 79 public static final int A = 0; |
| 80 public static final int B = 1; |
| 81 public static final int C = 1; |
| 82 } |
| 83 ``` |
| 84 |
| 85 ## Formatting Notes |
| 86 |
| 87 * Handling long package names: |
| 88 |
| 89 ``` |
| 90 // GENERATED_JAVA_ENUM_PACKAGE: ( |
| 91 // org.chromium.chrome.this.package.is.too.long.to.fit.on.a.single.line) |
| 92 ``` |
| 93 |
| 94 * Enum entries |
| 95 * Single line enums should look like this: |
| 96 |
| 97 // GENERATED_JAVA_ENUM_PACKAGE: org.foo |
| 98 enum NotificationActionType { BUTTON, TEXT }; |
| 99 |
| 100 * Multi-line enums should have one enum entry per line, like this: |
| 101 |
| 102 // GENERATED_JAVA_ENUM_PACKAGE: org.foo |
| 103 enum NotificationActionType { |
| 104 BUTTON, |
| 105 TEXT |
| 106 }; |
| 107 |
| 108 * Multi-line enum entries are allowed but should be formatted like this: |
| 109 |
| 110 // GENERATED_JAVA_ENUM_PACKAGE: org.foo |
| 111 enum NotificationActionType { |
| 112 LongKeyNumberOne, |
| 113 LongKeyNumberTwo, |
| 114 ... |
| 115 LongKeyNumberThree = |
| 116 LongKeyNumberOne | LongKeyNumberTwo | ... |
| 117 }; |
| 118 |
| 119 * Preserving comments |
| 120 |
| 121 ```cpp |
| 122 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium |
| 123 enum CommentEnum { |
| 124 // This comment will be preserved. |
| 125 ONE, |
| 126 TWO, // This comment will NOT be preserved. |
| 127 THREE |
| 128 } |
| 129 ``` |
| 130 |
| 131 ```java |
| 132 ... |
| 133 public class CommentEnum { |
| 134 ... |
| 135 /** |
| 136 * This comment will be preserved. |
| 137 */ |
| 138 public static final int ONE = 0; |
| 139 public static final int TWO = 1; |
| 140 public static final int THREE = 2; |
| 141 } |
| 142 ``` |
| 143 |
| 144 ## Code |
| 145 * [Generator |
| 146 code](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_enum.py?dr
=C&sq=package:chromium) |
| 147 and |
| 148 [Tests](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_enum_tes
ts.py?dr=C&q=java_cpp_enum_tests&sq=package:chromium&l=1) |
| 149 * [GN |
| 150 template](https://cs.chromium.org/chromium/src/build/config/android/rules.gni?q=
java_cpp_enum.py&sq=package:chromium&dr=C&l=458) |
OLD | NEW |