Java .properties files as strongly typed classes

后端 未结 8 1122
小蘑菇
小蘑菇 2021-01-02 14:50

Is there way to get properties files as strongly typed classes? I guess there are code generators but doing it with annotations would be much cooler.

What I mean is;

相关标签:
8条回答
  • 2021-01-02 15:18

    The Annotation Processing Tool (apt) cannot modify classes (though it can create new ones). In order to modify the class at compile time, you'd probably need to edit the AST (as Project Lombok does). The simplest approach would probably be to generate the classes and then use the generated library as a dependency for other code.

    0 讨论(0)
  • 2021-01-02 15:22

    If you want to do it statically, its a code generation problem that may be solved quite easily (for each item in file, produce a new getXXX method).

    But if you want this at runtime, then you have the problem of having your code referencing method that did not exists at compile time; I don't think it can be done.

    (Note that if you are looking for a project idead, the reverse, having an interface with accessor method and annotation, and an implementation generated at runtime, that relies on the annotated methods, can be done.)

    0 讨论(0)
提交回复
热议问题