Skip to content

Promote container elements from generator candidates - #1399

Open
seongahjo wants to merge 2 commits into
fix/restore-set-value-handlingfrom
refactor/container-element-candidates
Open

seongahjo wants to merge 2 commits into
fix/restore-set-value-handlingfrom
refactor/container-element-candidates

Conversation

@seongahjo

Copy link
Copy Markdown
Contributor

Summary

Object properties become nodes through candidates and promoters, but container elements were created as nodes directly by the JvmContainerNodeGenerators. A Map.Entry element therefore needed its own step after type resolution to become a Map.Entry node.

Generators now return element candidates, and the transformer promotes and resolves them with the same step as object property candidates. The candidate factory and the promoters alone decide whether a node is a plain, Map element or Map.Entry node.

This is the last of four stacked PRs, based on #1398:

  1. Do not take supertype type arguments for JDK value types and enums #1397 Do not take supertype type arguments for JDK value types and enums
  2. Fix setting JDK internal containers into Object properties #1395 Fix setting JDK internal containers into Object properties
  3. Restore set value type checks and per-element setLazy #1398 Restore set value type checks and per-element setLazy
  4. This PR

(Optional): Description

Generators return element candidates (object-farm-api)

  • JvmContainerNodeGenerator.generateContainerElements returns List<JvmNodeCandidate> instead of List<JvmNode>.
    • The linear, array and single-element generators create candidates with JavaNodeCandidateFactory, so an element of a Map.Entry type gets a Map.Entry candidate.
    • The map generator creates JavaMapNodeCandidates, promoted by JavaMapNodePromoter, which nothing used before.
    • ContainerPropertyGeneratorNodeGenerator, which bridges user-registered ContainerPropertyGenerators, returns candidates too.
  • Users cannot register a JvmContainerNodeGenerator through the fixture-monkey API, only ContainerPropertyGenerators, so this does not change the user-facing API.

One step for object properties and container elements

  • JvmNodeTreeTransformer.promoteAndResolveCandidate selects the candidate to promote from the set value's type, promotes it and resolves its type, for object property and container element candidates alike.
  • The separate element steps (resolveContainerElementTypes, promoteElementToMapEntryIfNeeded, resolveElementSharingContainerPath) are removed.
  • A candidate with neither a name nor an index, such as the content of an Optional, shares its parent's path and is resolved without the parent's path resolvers, as before.
  • Element nodes now have a source candidate in JvmNodeTree, like object property nodes.

Element index

  • JvmNodeCandidate.getIndex() returns the index of a candidate created by a ContainerElementCreationMethod, and null otherwise.
  • The promoters put it on the node they create, the same as the name.
  • KotlinNodePromoter leaves container element candidates to the Java promoters, so element types and nullability stay as the generators decide.

How Has This Been Tested?

  • Added containerElementsArePromotedFromGeneratedCandidates to JvmNodeTreeTransformerTest: the elements of a List<String> have their index, their name and their source candidate

  • Added sampleOptionalOfEntry to ContainerTypeTest: Optional<Map.Entry<String, Integer>> is generated with an entry

  • Existing tests cover list, set, array, map and Optional elements, and Map.Entry elements (ContainerTypeTest.sampleListOfEntries, SetOptionalAndEntryDecompositionTest.setSetOfEntriesToObjectProperty)

  • Ran object-farm-api, fixture-monkey, fixture-monkey-kotlin, java-tests, java-17-tests and kotlin-tests with checkstyle, Checker Framework and ktlint

  • Benchmarks before and after this change, quick mode (-Pjmh.quick), fork 1, run one after another, AverageTime in ms/op:

    Benchmark Before After Ratio
    AdapterBenchmark.warmWithAdapter 56.24 ± 6.05 59.33 ± 7.47 1.055
    AdapterBenchmark.warmWithAdapterWithSet 56.78 ± 2.77 60.14 ± 8.49 1.059
    AdapterBenchmark.warmWithAdapterWithThenApply 278.82 ± 67.51 293.84 ± 55.44 1.054
    ObjectGenerationBenchmark.builderGenerateOrderSheetWithFixtureMonkey 138.98 ± 24.82 142.43 ± 4.35 1.025

    All confidence intervals overlap, but every average is slower (2.5% to 5.9%). Each element now costs a candidate, a promoter lookup and a candidate-to-node mapping, which JvmNodeTree copies. Which of these costs most was not measured.

Is the Document updated?

No documentation change is needed.

- Throw when a set, setLazy or register value is not assignable to the property type
- Call a per-element setLazy supplier for each element
- Decompose, check and rebuild Optional, Map.Entry and registered setLazy values
- Infer the type arguments of Map.Entry and Optional values from their contents
- Build Map.Entry nodes for Map.Entry values set into Object properties and containers
- Return element candidates from JvmContainerNodeGenerator instead of nodes
- Promote and resolve element candidates the same way as object property candidates
- Take a candidate's index from its ContainerElementCreationMethod
- Leave container element candidates to the Java promoters in KotlinNodePromoter
@seongahjo
seongahjo force-pushed the refactor/container-element-candidates branch from aa862aa to 4f4db69 Compare November 19, 2026 08:45
@seongahjo seongahjo added this to the 1.2.5 milestone Nov 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant