Skip to content

[jnigen] Getters and setters - #3231

Merged
liamappelbe merged 15 commits into
mainfrom
jnigen_get_set
Mar 20, 2026
Merged

liamappelbe merged 15 commits into
mainfrom
jnigen_get_set

Conversation

@liamappelbe

@liamappelbe liamappelbe commented Mar 13, 2026 •

Copy link
Copy Markdown
Contributor
  • AST: Added a propertyKind field to Method, which can be getter, setter, or none.
  • Transform: _MethodRenamer (best viewed with whitespace changes hidden)
    • _isGetter and _isSetter show the rules for the when the transformation happens.
    • The main complication is dealing with the fact that, in Dart, getters and setters can have the same name, as long as they also have the same type. So we need a registry of getters and setters keyed by name and type, so we can dedupe them where possible. That's what propertyNames is for. propertySig is the deduping key.
    • The getter/setter transformation is disabled for interfaces. It would cause all sorts of name conflicts, eg in the lambdas that store the user's method impl.
  • Code gen:
    • Generated method header changes based on the propertyKind.
    • Only non-obvious change is idName. The old logic for naming the private _id_ variables for each method caused naming collisions since getters and setters can have the same name. So the id name needs to incorperate the property kind.

Fixes #612

@github-actions

github-actions Bot commented Mar 13, 2026 •

Copy link
Copy Markdown

PR Health

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?
jni Breaking 0.15.2 0.16.0-wip 0.16.0-wip ✔️

This check can be disabled by tagging the PR with skip-breaking-check.

Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

This check can be disabled by tagging the PR with skip-changelog-check.

API leaks ⚠️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbol Leaking sources
jni $JIterator core_bindings.dart::JIterator::implementIn::$impl
core_bindings.dart::JIterator::implement::$impl
jni $JCollection core_bindings.dart::JCollection::implementIn::$impl
core_bindings.dart::JCollection::implement::$impl
jni $JList core_bindings.dart::JList::implementIn::$impl
core_bindings.dart::JList::implement::$impl
jni $JMap$JEntry core_bindings.dart::JMap$JEntry::implementIn::$impl
core_bindings.dart::JMap$JEntry::implement::$impl
jni $JMap core_bindings.dart::JMap::implementIn::$impl
core_bindings.dart::JMap::implement::$impl
jni $JSet core_bindings.dart::JSet::implementIn::$impl
core_bindings.dart::JSet::implement::$impl

This check can be disabled by tagging the PR with skip-leaking-check.

@liamappelbe liamappelbe changed the title WIP [jnigen] Getters and setters [jnigen] Getters and setters Mar 19, 2026
@liamappelbe
liamappelbe requested a review from dcharkes March 19, 2026 00:29
@liamappelbe
liamappelbe marked this pull request as ready for review March 19, 2026 00:30
.constructorId("(Ljava/lang/String;)V")
.call(fileInputStreamClass, [file.toJString()]);
final pdDoc = PDDocument.load$6(inputFile)!;
int pages = pdDoc.getNumberOfPages();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh gosh, I forgot how verbose Java is 😆


bool _isVoid(ReferredType t) => t is PrimitiveType && t.name == 'void';

final _getterRegExp = RegExp(r'^get[A-Z]');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any other patterns that we should match?

Is it common in Java to have isEven() that we want do do get isEven?

LGTM to the patterns we have.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. This is a super common pattern in Java, and I think it makes sense to transform these ones too.

}

enum MethodPropertyKind {
none,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a normal method instead of none?

(And I guess we don't do operator which is the 4th type of member.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

expect(e.number, equals(0));
expect(e.isUp, true);
expect(e.codename!.toDartString(), equals('achilles'));
e.number = 1;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Immediately feels like Dart 😄

test('Misc. instance methods', () {
final e = Example();
final rand = e.getRandom();
final rand = e.random;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is weird, because I suspect this is not a getter but an actual generator.

But you could argue it's misnamed in Java. it should have been named nextRandom().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think eventually we should make this configurable. In the same way users can rename things, they should also be able to convert methods to be getters/setters, or not.

@liamappelbe
liamappelbe merged commit 2ebcf35 into main Mar 20, 2026
32 checks passed
@liamappelbe
liamappelbe deleted the jnigen_get_set branch March 20, 2026 01:13
Hassnaa9 pushed a commit to Hassnaa9/native that referenced this pull request Apr 6, 2026
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.

☂️ Idiomatic conversions

2 participants