Skip to content

wrap-java: Basic classfile parser to get CLASS retained annotations #589

Draft
ktoso wants to merge 4 commits intoswiftlang:mainfrom
ktoso:annotations
Draft

wrap-java: Basic classfile parser to get CLASS retained annotations #589
ktoso wants to merge 4 commits intoswiftlang:mainfrom
ktoso:annotations

Conversation

@ktoso
Copy link
Collaborator

@ktoso ktoso commented Mar 6, 2026

Implement basic class file parsing, only enough to get annotations

Thankfully the classfile format is well known and documented.

We avoid the usual "Java way" of doing this work which would be to pull
in the asm.jar dependency, but instead choose to do the parsing
ourselfes.

The format is well specified and stable, so we can be pretty confident
in it. I based it on the JDK22 revision of the spec: https://docs.oracle.com/javase/specs/jvms/se22/html/jvms-4.html

We only forus on the "RuntimeInvisibleAannotations" attribute because
runtime visible ones we're able to get from plain reflection calls
(which the Deprecated test cases showcase already in previous PR): https://docs.oracle.com/javase/specs/jvms/se22/html/jvms-4.html#jvms-4.7.17

This is necessary to support Android's RequiredApi annotations which are
CLASS retained (or how kotlin confusingly calls it BINARY which mislead
me and I thouhgt they're retained but yeah makes sense).

This way we're able to emit Android availability annotations for the
whole Android SDK.

@ktoso ktoso force-pushed the annotations branch 2 times, most recently from d41030e to 9eb575c Compare March 6, 2026 04:42
Implement basic class file parsing, only enough to get annotations

Thankfully the classfile format is well known and documented.

We avoid the usual "Java way" of doing this work which would be to pull
in the asm.jar dependency, but instead choose to do the parsing
ourselfes.

The format is well specified and stable, so we can be pretty confident
in it. I based it on the JDK22 revision of the spec: https://docs.oracle.com/javase/specs/jvms/se22/html/jvms-4.html

We only forus on the "RuntimeInvisibleAannotations" attribute because
runtime visible ones we're able to get from plain reflection calls
(which the Deprecated test cases showcase already in previous PR): https://docs.oracle.com/javase/specs/jvms/se22/html/jvms-4.html#jvms-4.7.17

This is necessary to support Android's RequiredApi annotations which are
CLASS retained (or how kotlin confusingly calls it BINARY which mislead
me and I thouhgt they're retained but yeah makes sense).

This way we're able to emit Android availability annotations for the
whole Android SDK.
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