Skip to content

atdj: generate wrapper classes for top-level list aliases#500

Merged
mjambon merged 3 commits intoahrefs:masterfrom
mjambon:atdj-arrays
Apr 17, 2026
Merged

atdj: generate wrapper classes for top-level list aliases#500
mjambon merged 3 commits intoahrefs:masterfrom
mjambon:atdj-arrays

Conversation

@mjambon
Copy link
Copy Markdown
Collaborator

@mjambon mjambon commented Apr 17, 2026

Summary

  • Java has no type aliases, so type items = item list previously caused an error in atdj. This PR generates a proper Java class instead.
  • A new trans_list_alias function in atdj_trans.ml generates a class (e.g. Items) implementing Atdj with the same interface as record classes: no-arg constructor, ArrayList<T> constructor, String (JSON) constructor, package-private JSONArray constructor, toJsonBuffer, toJson, and a public value field.
  • trans_outer is extended to recognise both direct List nodes and Name aliases that resolve to a List via norm_ty.
  • assign is updated so that when a list alias appears as a record field or sum-variant payload, deserialization uses new Items(jsonArray) rather than the inline ArrayList-building loop.
  • All element types are handled correctly: records, sums, booleans, integers, floats, and strings (the last using Util.writeJsonString as expected).
  • A test case type items = simple_record list is added along with the expected Items.java output and the necessary dune rules.

Test plan

  • dune build atdj compiles cleanly
  • All expected-vs-generated Java file diffs pass
  • Manually verify generated output for record, sum, bool, string, and int element types looks correct

🤖 Generated with Claude Code

mjambon and others added 2 commits April 17, 2026 00:46
Java has no type aliases, so 'type items = item list' previously caused
an error. We now generate a class Items implementing Atdj with:
- a no-arg constructor (empty list)
- a constructor from ArrayList<Item>
- a constructor from a JSON string
- a package-private constructor from JSONArray (analogous to the JSONObject
  constructor generated for records)
- toJsonBuffer / toJson
- a public 'value' field of type ArrayList<Item>

The assign function is also updated so that list aliases used as record
fields or sum-variant payloads are deserialized via 'new Items(jsonArray)'
rather than triggering the inline ArrayList-building loop.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
JSONObject.keys() returns Iterator<Object> in newer versions of
org.json, causing a compile error. The returned key is always a String
in practice, so a cast is safe.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@mjambon mjambon merged commit 7008fea into ahrefs:master Apr 17, 2026
2 of 3 checks passed
pedrobslisboa pushed a commit that referenced this pull request Apr 22, 2026
* atdj: generate wrapper classes for top-level list aliases

Java has no type aliases, so 'type items = item list' previously caused
an error. We now generate a class Items implementing Atdj with:
- a no-arg constructor (empty list)
- a constructor from ArrayList<Item>
- a constructor from a JSON string
- a package-private constructor from JSONArray (analogous to the JSONObject
  constructor generated for records)
- toJsonBuffer / toJson
- a public 'value' field of type ArrayList<Item>

The assign function is also updated so that list aliases used as record
fields or sum-variant payloads are deserialized via 'new Items(jsonArray)'
rather than triggering the inline ArrayList-building loop.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Update CHANGES.md for atdj list alias support

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* atdj: cast keys().next() to String for newer org.json

JSONObject.keys() returns Iterator<Object> in newer versions of
org.json, causing a compile error. The returned key is always a String
in practice, so a cast is safe.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
pedrobslisboa pushed a commit that referenced this pull request Apr 22, 2026
* atdj: generate wrapper classes for top-level list aliases

Java has no type aliases, so 'type items = item list' previously caused
an error. We now generate a class Items implementing Atdj with:
- a no-arg constructor (empty list)
- a constructor from ArrayList<Item>
- a constructor from a JSON string
- a package-private constructor from JSONArray (analogous to the JSONObject
  constructor generated for records)
- toJsonBuffer / toJson
- a public 'value' field of type ArrayList<Item>

The assign function is also updated so that list aliases used as record
fields or sum-variant payloads are deserialized via 'new Items(jsonArray)'
rather than triggering the inline ArrayList-building loop.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Update CHANGES.md for atdj list alias support

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* atdj: cast keys().next() to String for newer org.json

JSONObject.keys() returns Iterator<Object> in newer versions of
org.json, causing a compile error. The returned key is always a String
in practice, so a cast is safe.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
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