Repository navigation
Done
Description
Problem
AnnotationDto.members is a Map<String, String>, and each value is a Java source expression. The Roaster backend writes it out as-is: setLiteralValue in RoasterCodeGenerator and RoasterMapper.mapAnnotationMember for type-use annotations. Callers therefore have to do the quoting themselves:
annotation
.annotationType(SuppressWarnings.class)
.members(Map.of("value", "\"unused\"")); // String member
annotation.addMember("since", "\"1\""); // String member
annotation.addMember("value", "Foo.class"); // Class member
annotation.addMember("mode", "Mode.STRICT"); // enum member
annotation.addMember("value", "{\"a\", \"b\"}"); // array memberThe builders added in #415 / #429 make this visible in the public API, including the usage example ShowcaseGenerationTest.
Drawbacks:
- Escaping is left to the caller: a missing quote or an unescaped
"/\in the value produces invalid generated source, and it only shows up when the generated code is compiled. - Types inside a value are not modelled: a class or enum referenced in a value (
Foo.class,Mode.STRICT) is just text. It is not aTypeName, so the backend can't collect an import for it or qualify a colliding simple name (Fields of different types sharing a simple name (e.g. java.util.Date / java.sql.Date) generate uncompilable builder #417). - Backend-specific contract: the value format is Java source syntax rather than structured data, which a non-Roaster backend (Shared generator conformance suite for codegen backends #420) would also have to parse or pass through.
Proposal
Add typed member helpers that do the quoting and keep type information, and keep the raw form for advanced expressions:
annotation
.annotationType(SuppressWarnings.class)
.add2StringMember("value", "unused") // -> value = "unused" (escaped)
.add2ClassMember("value", Foo.class) // -> value = Foo.class (import collected)
.add2EnumMember("mode", Mode.STRICT) // -> mode = Mode.STRICT (import collected)
.add2RawMember("value", "{1, 2}"); // -> as todayTo decide:
- Model: a small
AnnotationMemberValuetype (string / class / enum / primitive / array / raw) instead ofStringin the map, or only helpers on top ofMap<String, String>. Only the first lets backends collect imports. - Compatibility: keep
addMember(String, String)/setMembers(Map<String, String>)as the raw form. - Backends: update the Roaster backend and the conformance suite (Shared generator conformance suite for codegen backends #420).
Notes
- This is not caused by feat: generate fluent builders for codegen model DTOs (#415) #429. The model existed before it; the new builders only make it more visible.
- Related: feat: generate fluent builders for codegen model DTOs (#415) #429 (DTO builders), Shared generator conformance suite for codegen backends #420 (backend-neutral conformance suite), Fields of different types sharing a simple name (e.g. java.util.Date / java.sql.Date) generate uncompilable builder #417 (simple name collisions).
Metadata
Metadata
Assignees
Labels
No labels
Activity