OLD | NEW |
1 # Accessing C++ Enums In Java | 1 # Accessing C++ Enums In Java |
2 | 2 |
3 [TOC] | 3 [TOC] |
4 | 4 |
5 ## Introduction | 5 ## Introduction |
6 | 6 |
7 Accessing C++ enums in Java is implemented via a Python script which analyzes | 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 | 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 | 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 | 10 same as the name of the enum. If all the names of the enum values are prefixed |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 4. The generated file `org/chromium/chrome/FooBar.java` would contain: | 63 4. The generated file `org/chromium/chrome/FooBar.java` would contain: |
64 | 64 |
65 ```java | 65 ```java |
66 package org.chromium.chrome; | 66 package org.chromium.chrome; |
67 | 67 |
68 import android.support.annotation.IntDef; | 68 import android.support.annotation.IntDef; |
69 | 69 |
70 import java.lang.annotation.Retention; | 70 import java.lang.annotation.Retention; |
71 import java.lang.annotation.RetentionPolicy; | 71 import java.lang.annotation.RetentionPolicy; |
72 | 72 |
73 public class FooBar { | 73 @IntDef({ |
74 @IntDef({ | 74 FooBar.A, FooBar.B, FooBar.C |
75 A, B, C | 75 }) |
76 }) | 76 @Retention(RetentionPolicy.SOURCE) |
77 @Retention(RetentionPolicy.SOURCE) | 77 public @interface FooBar { |
78 public @interface FooBarEnum {} | 78 int A = 0; |
79 public static final int A = 0; | 79 int B = 1; |
80 public static final int B = 1; | 80 int C = 1; |
81 public static final int C = 1; | |
82 } | 81 } |
83 ``` | 82 ``` |
84 | 83 |
85 ## Formatting Notes | 84 ## Formatting Notes |
86 | 85 |
87 * Handling long package names: | 86 * Handling long package names: |
88 | 87 |
89 ``` | 88 ``` |
90 // GENERATED_JAVA_ENUM_PACKAGE: ( | 89 // GENERATED_JAVA_ENUM_PACKAGE: ( |
91 // org.chromium.chrome.this.package.is.too.long.to.fit.on.a.single.line) | 90 // org.chromium.chrome.this.package.is.too.long.to.fit.on.a.single.line) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 enum CommentEnum { | 122 enum CommentEnum { |
124 // This comment will be preserved. | 123 // This comment will be preserved. |
125 ONE, | 124 ONE, |
126 TWO, // This comment will NOT be preserved. | 125 TWO, // This comment will NOT be preserved. |
127 THREE | 126 THREE |
128 } | 127 } |
129 ``` | 128 ``` |
130 | 129 |
131 ```java | 130 ```java |
132 ... | 131 ... |
133 public class CommentEnum { | 132 public @interface CommentEnum { |
134 ... | 133 ... |
135 /** | 134 /** |
136 * This comment will be preserved. | 135 * This comment will be preserved. |
137 */ | 136 */ |
138 public static final int ONE = 0; | 137 int ONE = 0; |
139 public static final int TWO = 1; | 138 int TWO = 1; |
140 public static final int THREE = 2; | 139 int THREE = 2; |
141 } | 140 } |
142 ``` | 141 ``` |
143 | 142 |
144 ## Code | 143 ## Code |
145 * [Generator | 144 * [Generator |
146 code](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_enum.py?dr
=C&sq=package:chromium) | 145 code](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_enum.py?dr
=C&sq=package:chromium) |
147 and | 146 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) | 147 [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 | 148 * [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) | 149 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 |