Movatterモバイル変換


[0]ホーム

URL:


project logoChromium Docs

Accessing C++ Enums In Java

Contents

Introduction

Accessing C++ enums in Java is implemented via a Python script which analyzes the C++ enum and spits out the corresponding Java class. The enum needs 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 prefixed with the MACRO_CASED_ name of the enum those prefixes will be stripped from the Java version.

Features

  • Customize the package name of the generated class using theGENERATED_JAVA_ENUM_PACKAGE directive (required)
  • Customize the class name using theGENERATED_JAVA_CLASS_NAME_OVERRIDE directive (optional)
  • Strip enum entry prefixes to make the generated classes less verbose using theGENERATED_JAVA_PREFIX_TO_STRIP directive (optional)
  • Follows best practices by usingIntDef Instead of Enum
  • Generate theflag attribute using theGENERATED_JAVA_IS_FLAG directive (optional)
  • Copies comments that directly precede enum entries into the generated Java class

Usage

  1. Add directives to your C++ enum. Only theGENERATED_JAVA_ENUM_PACKAGE directive is required:

    // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome// GENERATED_JAVA_CLASS_NAME_OVERRIDE: FooBar// GENERATED_JAVA_PREFIX_TO_STRIP: BAR_// GENERATED_JAVA_IS_FLAG: trueenumSomeEnum{  BAR_A=1<<0,  BAR_B=1<<1,  BAR_C= BAR_B,};
  2. Add a new build target and add it to thesrcjar_deps of anandroid_library target:

    if(is_android){import("//build/config/android/rules.gni")}if(is_android){  java_cpp_enum("java_enum_srcjar"){# External code should depend on ":foo_java" instead.    visibility=[":*"]    sources=[# Include the .h or .cc file(s) which defines the enum(s)."base/android/native_foo_header.h",]}# If there's already an android_library target, you can add# java_enum_srcjar to that target's srcjar_deps. Otherwise, the best# practice is to create a new android_library just for this target.  android_library("foo_java"){    srcjar_deps=[":java_enum_srcjar"]# Important: the generated enum uses the @IntDef annotation provided by# this dependency.    deps=["//third_party/androidx:androidx_annotation_annotation_java"]}}
  3. The generated fileorg/chromium/chrome/FooBar.java would contain:

    package org.chromium.chrome;import androidx.annotation.IntDef;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@IntDef(flag=true, value={FooBar.A,FooBar.B,FooBar.C})@Retention(RetentionPolicy.SOURCE)public@interfaceFooBar{int A=1<<0;int B=1<<1;int C=1<<1;}

Formatting Notes

  • Handling long package names:

    // GENERATED_JAVA_ENUM_PACKAGE: (//   org.chromium.chrome.this.package.is.too.long.to.fit.on.a.single.line)
  • Enum entries

    • Single line enums should look like this:

      // GENERATED_JAVA_ENUM_PACKAGE: org.fooenumNotificationActionType{ BUTTON, TEXT};
    • Multi-line enums should have one enum entry per line, like this:

      // GENERATED_JAVA_ENUM_PACKAGE: org.fooenumNotificationActionType{  BUTTON,  TEXT};
    • Multi-line enum entries are allowed but should be formatted like this:

      // GENERATED_JAVA_ENUM_PACKAGE: org.fooenumNotificationActionType{LongKeyNumberOne,LongKeyNumberTwo,...LongKeyNumberThree=LongKeyNumberOne|LongKeyNumberTwo|...};
  • Preserving comments

    // GENERATED_JAVA_ENUM_PACKAGE: org.chromiumenumCommentEnum{// This comment will be preserved.  ONE,  TWO,// This comment will NOT be preserved.  THREE}
    ...public@interfaceCommentEnum{.../**   * This comment will be preserved.   */int ONE=0;int TWO=1;int THREE=2;}

Troubleshooting

Symbol not found/could not resolve IntDef

You may see an error like this when compiling:

$ autoninja-Cout/Defaultbase/foo_javautil.build_utils.CalledProcessError:Command failed:...org/chromium/chrome/FooBar.java:13: error: symbolnot found androidx.annotation.IntDefHint:Add"//third_party/androidx:androidx_annotation_annotation_java" to deps of//base/foo_javaimport androidx.annotation.IntDef;^org/chromium/chrome/FooBar.java:18: error: couldnot resolveIntDef@IntDef({^

The fix is to add"//third_party/androidx:androidx_annotation_annotation_java" to thedeps of theandroid_library. Note:do not add this to thejava_cpp_enum target by mistake, otherwise you'll see a new error:

$ autoninja-Cout/Defaultbase/foo_java[0/1]Regenerating ninja filesERROR at//base/BUILD.gn:194:12: Assignment had no effect.    deps=["//third_party/androidx:androidx_annotation_annotation_java"]^--------------------------------------------------------------Youset the variable"deps" hereand it was unused before it wentout of scope....

See also

Code


[8]ページ先頭

©2009-2025 Movatter.jp