Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add @SafeVarargs to findAncestor()#4632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
bannmann wants to merge1 commit intojavaparser:master
base:master
Choose a base branch
Loading
frombannmann:findancestor-safevarargs

Conversation

bannmann
Copy link
Contributor

@jlerbsc
Copy link
Collaborator

For information, JP is currently compiled in Java 8.

@bannmann
Copy link
ContributorAuthor

Hm, it turns out@SafeVarargs can only be used onfinal methods. Anddefault methods on interfaces cannot be final. So I guess users have to live withUnchecked generics array creation for varargs parameter warnings whenever callingfindAncestor() (or add@SuppressWarnings("unchecked") to their call site).

Alternatively, one might add an overload that accepts a single ancestor class. That overload would not suffer from this problem.

Come to think of it, having that overload might be a good idea anyway - the currentfindAncestor(Class<N>... types) signature can in fact be called without any arguments (someNode.findAncestor()), which would always return an emptyOptional. It would be better if that would trigger a compilation error.

Other APIs tackle this by having non-vararg overloads that combine a fixed argument with varargs. So instead offindAncestor(Class<N>... types) we would have:

  1. findAncestor(Class<N> type)
  2. findAncestor(Class<N> type, Class<N>... types)

When passing a single argument, the Java compiler would then prefer the first version, and use the second if there are more types. Most importantly,someNode.findAncestor() would no longer compile. (Of course that would constitute a breaking change, so it might require deprecation of the old version and adding the new ones with different names.)

@bannmann
Copy link
ContributorAuthor

For information, JP is currently compiled in Java 8.

Oh, I didn't know that. Anyway,@SafeVarargs is the same in Java 8.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@bannmann@jlerbsc

[8]ページ先頭

©2009-2025 Movatter.jp