blob: fb3382e6a3394ab1c21343d6bda823608f7121ee [file] [log] [blame] [view]
estevensonc0b135ff2016-11-17 16:03:511# Accessing C++ Enums In Java
2
3[TOC]
4
5## Introduction
6
7Accessing C++ enums in Java is implemented via a Python script which analyzes
8the C++ enum and spits out the corresponding Java class. The enum needs to be
9annotated in a particular way. By default, the generated class name will be the
10same as the name of the enum. If all the names of the enum values are prefixed
11with the MACRO\_CASED\_ name of the enum those prefixes will be stripped from
12the 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`
18directive (optional)
19* Strip enum entry prefixes to make the generated classes less verbose using
20the `GENERATED_JAVA_PREFIX_TO_STRIP` directive (optional)
21* Supports
22[`@IntDef`](https://developer.android.com/reference/android/support/annotation/IntDef.html)
qyearsleyc0dc6f42016-12-02 22:13:3923* Copies comments that directly precede enum entries into the generated Java
estevensonc0b135ff2016-11-17 16:03:5124class
25
26## Usage
27
281. 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
412. 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
533. 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
634. 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
dgn2b5c5e822017-05-12 20:33:3973 @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;
estevensonc0b135ff2016-11-17 16:03:5181 }
82 ```
83
84## Formatting Notes
85
86* Handling long package names:
87
88 ```
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
96 // GENERATED_JAVA_ENUM_PACKAGE: org.foo
97 enum NotificationActionType { BUTTON, TEXT };
98
99 * Multi-line enums should have one enum entry per line, like this:
100
101 // GENERATED_JAVA_ENUM_PACKAGE: org.foo
102 enum NotificationActionType {
103 BUTTON,
104 TEXT
105 };
106
107 * Multi-line enum entries are allowed but should be formatted like this:
108
109 // GENERATED_JAVA_ENUM_PACKAGE: org.foo
110 enum NotificationActionType {
111 LongKeyNumberOne,
112 LongKeyNumberTwo,
113 ...
114 LongKeyNumberThree =
115 LongKeyNumberOne | LongKeyNumberTwo | ...
116 };
117
118* Preserving comments
119
120 ```cpp
121 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium
122 enum CommentEnum {
123 // This comment will be preserved.
124 ONE,
125 TWO, // This comment will NOT be preserved.
126 THREE
127 }
128 ```
129
130 ```java
131 ...
dgn2b5c5e822017-05-12 20:33:39132 public @interface CommentEnum {
estevensonc0b135ff2016-11-17 16:03:51133 ...
134 /**
135 * This comment will be preserved.
136 */
dgn2b5c5e822017-05-12 20:33:39137 int ONE = 0;
138 int TWO = 1;
139 int THREE = 2;
estevensonc0b135ff2016-11-17 16:03:51140 }
141 ```
142
143## Code
144* [Generator
145code](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_enum.py?dr=C&sq=package:chromium)
146and
147[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)
148* [GN
149template](https://cs.chromium.org/chromium/src/build/config/android/rules.gni?q=java_cpp_enum.py&sq=package:chromium&dr=C&l=458)