Skip to content

Support IntDef, Nullable and NotNull annotations for Android java #3933

Description

@LouisCAD

Hi!

I started using flatbuffers to sync Watch Face themes across Android handset and paired Android Wear devices, and I find there's room for improvement for java implementation:

Flatbuffers enums don't use java enums, which is great according to this guy, but it would be much better if it generated IntDefs for us to get safety lint checks on Android. It would also be nice to get nullable/not nullable annotations on Android.

My suggestion is the following:

  1. Add an --android-java option to flatc next to --java one to optimize the code for Android.
  2. When flatc in invoked with --android-java, add @Nullable to all non primitive fields and generate @interfaces instead of classes for enum definitions, using @IntDef on them.
  3. Generate string constants for enum names only on demand, when an option is added (e.g. --gen-enum-names)

Here's a comparison with the code currently being generated by flatc 1.3 and how I'd like it to write it:

my_schema_snippet.fbs

namespace xyz.louiscad.common.model;

enum ColorType : int {
    ARGB, MD_COLOR_KEY
}

flatc 1.3

// automatically generated, do not modify

package xyz.louiscad.common.model;

public final class ColorType {
  private ColorType() { }
  public static final int ARGB = 0;
  public static final int MD_COLOR_KEY = 1;

  private static final String[] names = { "ARGB", "MD_COLOR_KEY", };

  public static String name(int e) { return names[e]; }
};

desired output

package xyz.louiscad.common.model;

import android.support.annotation.IntDef;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;

import static java.lang.annotation.RetentionPolicy.SOURCE;

@Documented
@Retention(SOURCE)
@IntDef({ColorType.ARGB, ColorType.MATERIAL_COLOR_KEY})
public @interface ColorType {
    int ARGB = 0;
    int MATERIAL_COLOR_KEY = 1;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions