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:
- Add an
--android-java option to flatc next to --java one to optimize the code for Android.
- 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.
- 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;
}
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:
--android-javaoption toflatcnext to--javaone to optimize the code for Android.flatcin invoked with--android-java, add@Nullableto all non primitive fields and generate@interfaces instead of classes for enum definitions, using@IntDefon them.--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
flatc 1.3
desired output