Skip to content

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

Merged
ktoso merged 3 commits intoswiftlang:mainfrom
ktoso:annotations
Mar 6, 2026
Merged

wrap-java: Basic classfile parser to get CLASS retained annotations #589
ktoso merged 3 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 4 times, most recently from cc75be4 to fd58e91 Compare March 6, 2026 11:19
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 marked this pull request as ready for review March 6, 2026 13:58
@ktoso ktoso merged commit 763ff47 into swiftlang:main Mar 6, 2026
61 checks passed
@ktoso ktoso deleted the annotations branch March 6, 2026 14:02
@ktoso
Copy link
Collaborator Author

ktoso commented Mar 6, 2026

resolves #586

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant