Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: docs/android_accessing_cpp_enums_in_java.md

Issue 2507863003: Add public doc for accessing C++ enums in Java. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Accessing C++ Enums In Java
2
3 [TOC]
4
5 ## Introduction
6
7 The traditional method involved extracting the enum values to a separate `_list. h` file so that we could then use gcc -E to generate a Java file (yes, we use th e C preprocessor to codegen Java). The new method involves a Python script which analyzes the C++ enum and spits out the corresponding Java class. The enum need s to be annotated in a particular way. By default, the generated class name will be the same as the name of the enum. If all the names of the enum values are pr efixed with the MACRO\_CASED\_ name of the enum those prefixes will be stripped from the Java version.
agrieve 2016/11/17 15:16:14 let's not mention the previous way :P. Also - ple
estevenson 2016/11/17 15:52:58 Done.
8
9 ## Features
10 * Customize the package name of the generated class using the `GENERATED_JAVA_EN UM_PACKAGE` directive (required)
11 * Customize the class name using the `GENERATED_JAVA_CLASS_NAME_OVERRIDE` direct ive (optional)
12 * Strip enum entry prefixes to make the generated classes less verbose using the `GENERATED_JAVA_PREFIX_TO_STRIP` directive (optional)
13 * Supports [`@IntDef`](https://developer.android.com/reference/android/support/a nnotation/IntDef.html)
14 * Copies comments that directly preceed enum entries into the generated Java cla ss
15
16 ## Usage
17
18 1. Add directives to your C++ enum
19
20 ```cpp
estevenson 2016/11/16 22:51:10 I indented these blocks so that list continuation
21 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome
22 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: FooBar
23 // GENERATED_JAVA_PREFIX_TO_STRIP: BAR_
24 enum SomeEnum {
25 BAR_A,
26 BAR_B,
27 BAR_C = BAR_B,
28 };
29 ```
30
31 2. Add a new build target
32
33 ```
34 import("//build/config/android/rules.gni")
35
36 java_cpp_enum("foo_generated_enum") {
37 sources = [
38 "base/android/native_foo_header.h",
39 ]
40 }
41 ```
42
43 3. Add the new target to the desired android_library targets srcjar_deps:
44
45 ```
46 android_library("base_java") {
47 srcjar_deps = [
48 ":foo_generated_enum",
49 ]
50 }
51 ```
52
53 4. The generated file `org/chromium/chrome/FooBar.java` would contain:
54
55 ```java
56 package org.chromium.chrome;
57
58 import android.support.annotation.IntDef;
59
60 import java.lang.annotation.Retention;
61 import java.lang.annotation.RetentionPolicy;
62
63 public class FooBar {
64 @IntDef({
65 A, B, C
66 })
67 @Retention(RetentionPolicy.SOURCE)
68 public @interface FooBarEnum {}
69 public static final int A = 0;
70 public static final int B = 1;
71 public static final int C = 1;
72 }
73 ```
74
75 ## Formatting Notes
76
77 * Handling long package names:
78
79 ```
80 // GENERATED_JAVA_ENUM_PACKAGE: (
81 // org.chromium.chrome.this.package.is.much.too.long.to.fit.on.a.single.li ne)
82 ```
83
84 * Enum entries
85 * Single line enums should look like this:
86
87 // GENERATED_JAVA_ENUM_PACKAGE: org.foo
88 enum NotificationActionType { BUTTON, TEXT };
89
90 * Multi-line enums should have one enum entry per line, like this:
91
92 // GENERATED_JAVA_ENUM_PACKAGE: org.foo
93 enum NotificationActionType {
94 BUTTON,
95 TEXT
96 };
97
98 * Multi-line enum entries are allowed but should be formatted like this:
99
100 // GENERATED_JAVA_ENUM_PACKAGE: org.foo
101 enum NotificationActionType {
102 LongKeyNumberOne,
103 LongKeyNumberTwo,
104 ...
105 LongKeyNumberThree =
106 LongKeyNumberOne | LongKeyNumberTwo | ...
107 };
108
109 * Preserving comments
110
111 ```cpp
112 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium
113 enum CommentEnum {
114 // This comment will be preserved.
115 ONE,
116 TWO, // This comment will NOT be preserved.
117 THREE
118 }
119 ```
120
121 ```java
122 ...
123 public class CommentEnum {
124 ...
125 /**
126 * This comment will be preserved.
127 */
128 public static final int ONE = 0;
129 public static final int TWO = 1;
130 public static final int THREE = 2;
131 }
132 ```
133
134 ## Code
135 * [Generator code](https://cs.chromium.org/chromium/src/build/android/gyp/java_c pp_enum.py?dr=C&sq=package:chromium) and [Tests](https://cs.chromium.org/chromiu m/src/build/android/gyp/java_cpp_enum_tests.py?dr=C&q=java_cpp_enum_tests&sq=pac kage:chromium&l=1)
136 * [GN template](https://cs.chromium.org/chromium/src/build/config/android/rules. gni?q=java_cpp_enum.py&sq=package:chromium&dr=C&l=458)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698