kaitai_struct_java_runtime icon indicating copy to clipboard operation
kaitai_struct_java_runtime copied to clipboard

Add annotations to field getters, param getters and instance getters

Open Mingun opened this issue 5 years ago • 2 comments

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:

  • [ ] this one for issue
  • [ ] https://github.com/kaitai-io/kaitai_struct_compiler/pull/191 -- PR for compiler
  • [x] https://github.com/kaitai-io/kaitai_struct_java_runtime/pull/23 -- PR for runtime

Mingun avatar Jan 09 '20 17:01 Mingun

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

  • a "parameter" (understandable)
  • a "field" (what is a "field", actually? an attribute in a seq?)
  • an "instance" (probably you'll want to distinguish between "parse instances" and "value instances"?).

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

  • determining whether a member is internal field or not
  • determining sequence of seq attributes

?

GreyCat avatar Jan 11 '20 23:01 GreyCat

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

  • value instance -- instances with value key (http://doc.kaitai.io/user_guide.html#_calculated_value_instances)
  • parse instance -- instances without value key

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

Mingun avatar Jan 12 '20 14:01 Mingun