blob: 8743ca6811dd16faac07030fd2f676b870a8dbb8 [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
Nate Fischer93c59152019-02-05 01:25:0843 ```gn
estevensonc0b135ff2016-11-17 16:03:5144 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
Nate Fischer93c59152019-02-05 01:25:0855 ```gn
estevensonc0b135ff2016-11-17 16:03:5156 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
Nate Fischer93c59152019-02-05 01:25:0888 ```cpp
estevensonc0b135ff2016-11-17 16:03:5189 // 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 Fischer93c59152019-02-05 01:25:0896 ```cpp
97 // GENERATED_JAVA_ENUM_PACKAGE: org.foo
98 enum NotificationActionType { BUTTON, TEXT };
99 ```
estevensonc0b135ff2016-11-17 16:03:51100
101 * Multi-line enums should have one enum entry per line, like this:
102
Nate Fischer93c59152019-02-05 01:25:08103 ```cpp
104 // GENERATED_JAVA_ENUM_PACKAGE: org.foo
105 enum NotificationActionType {
106 BUTTON,
107 TEXT
108 };
109 ```
estevensonc0b135ff2016-11-17 16:03:51110
111 * Multi-line enum entries are allowed but should be formatted like this:
112
Nate Fischer93c59152019-02-05 01:25:08113 ```cpp
114 // GENERATED_JAVA_ENUM_PACKAGE: org.foo
115 enum NotificationActionType {
116 LongKeyNumberOne,
117 LongKeyNumberTwo,
118 ...
119 LongKeyNumberThree =
120 LongKeyNumberOne | LongKeyNumberTwo | ...
121 };
122 ```
estevensonc0b135ff2016-11-17 16:03:51123
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 ...
dgn2b5c5e822017-05-12 20:33:39138 public @interface CommentEnum {
estevensonc0b135ff2016-11-17 16:03:51139 ...
140 /**
141 * This comment will be preserved.
142 */
dgn2b5c5e822017-05-12 20:33:39143 int ONE = 0;
144 int TWO = 1;
145 int THREE = 2;
estevensonc0b135ff2016-11-17 16:03:51146 }
147 ```
148
149## Code
150* [Generator
151code](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_enum.py?dr=C&sq=package:chromium)
152and
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
155template](https://cs.chromium.org/chromium/src/build/config/android/rules.gni?q=java_cpp_enum.py&sq=package:chromium&dr=C&l=458)