kaitai-io / kaitai_struct_java_runtime

Kaitai Struct: runtime for Java
MIT License
42 stars 18 forks source link

Add annotations to field getters, param getters and instance getters #22

Open Mingun opened 4 years ago

Mingun commented 4 years ago

If you want to build visualizer, you must rely on that fact, that internal fields begins with underscore. You can use the static field _seqFields to distinguish between parameters and structure fields. ~To distinguish between parameters and instances of the structure, you can see that there is a field with the same name, as getter method. If there is no field, it is an instance.~ No, this not work, instances also has backing field in class

But all these methods are fragile. I suggest introduce 3 annotations instead:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Parameter {
  String doc();// documentation
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Field {
  String doc();// documentation
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Instance {
  String doc();// documentation
}

List of all related issues/PRs:

GreyCat commented 4 years ago

From what I understand, adding these annotations would allow to use reflection in runtime to determine if given member is:

Which problems does that solve and how does that help with the questions that you've outlined above, i.e.:

?

Mingun commented 4 years ago

a "field" (what is a "field", actually? an attribute in a seq?)

Yes

an "instance" (probably you'll want to distinguish between "parse instances" and "value instances"?).

No, I think not. I just want to mark fields/getters/[setters], that generated from instances map. If I understand correctly, you mean

determining whether a member is internal field or not

Right now you are forced to use dirty haks to exclude fields, that is not fields, like checking name of field for leading underscore. With annotation you just go through all fields with these annotations

determining sequence of seq attributes

Initially, I hoped, that field sequence in the class will reflect order in seq. But, unfortunally, Java does not guaranties, that method or field will in some order (for example, in Java 10, this is the reverse order of defining fields in a class), so in actual proposal I included index parameter