Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Java 17 features #6691

Merged
merged 10 commits into from
Jun 5, 2024
Merged

Use Java 17 features #6691

merged 10 commits into from
Jun 5, 2024

Conversation

Gold856
Copy link
Contributor

@Gold856 Gold856 commented Jun 3, 2024

Uses enhanced instanceof (and simplify equals methods)
Uses switch expressions and arrow labels
Seal and finalize some Shuffleboard classes

SamCarlberg and others added 5 commits January 21, 2024 00:14
Includes features added from 13 through 21
Replace usages of java.util.Optional with our Option type
Use switch expressions and new switch syntax where possible
Use pattern matching where possible
Use enhanced instanceof where possible
@Gold856 Gold856 requested review from PeterJohnson and a team as code owners June 3, 2024 01:03
@Starlight220
Copy link
Member

apparently none of the linters can handle an exhaustive switch expression with no default

Are you handling the case of null? (Or otherwise proving it's not null)

@Gold856
Copy link
Contributor Author

Gold856 commented Jun 3, 2024

Are you handling the case of null? (Or otherwise proving it's not null)

That's not available until Java 21. Switch expressions/statements have historically thrown NPEs until Java 21. And some of the tools say they won't warn on switch expressions because javac will check switch exhaustiveness, but they still warn anyways.

PeterJohnson
PeterJohnson previously approved these changes Jun 3, 2024
@Gold856
Copy link
Contributor Author

Gold856 commented Jun 3, 2024

Apparently switch expressions are separate from arrow labels, so when the linters were warning about missing switch defaults on switch statements, they were, in fact, warning about missing switch defaults on switch statements. Defaults have been put back in, matching whatever previous logic was present.

@sciencewhiz
Copy link
Contributor

Did you do this by hand, or did you use a tool?

@Gold856
Copy link
Contributor Author

Gold856 commented Jun 3, 2024

I cherry-picked @SamCarlberg's commit that refactored a bunch of code to use Java 21 features, removed the Java 21 parts to make it Java 17 compatible, and refactored any remaining code, mostly by using VS Code's search feature with the regexes case .*: to convert case labels, else if to search for places where a switch would be better, and instanceof to convert instanceof usages.

Comment on lines -23 to +28
int type = 0;
switch (pixelFormat) {
case kYUYV:
case kRGB565:
type = CvType.CV_8UC2;
break;
case kBGR:
type = CvType.CV_8UC3;
break;
case kGray:
case kMJPEG:
default:
type = CvType.CV_8UC1;
break;
}
return type;
return switch (pixelFormat) {
case kYUYV, kRGB565, kY16, kUYVY -> CvType.CV_8UC2;
case kBGR -> CvType.CV_8UC3;
case kBGRA -> CvType.CV_8UC4;
case kGray, kMJPEG, kUnknown -> CvType.CV_8UC1;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noting that this is a change in behavior (which matches CvSink)- I think it's fine, just want to note that down.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I assumed that we want this to match CvSink.

@PeterJohnson PeterJohnson merged commit b99d9c1 into wpilibsuite:main Jun 5, 2024
25 checks passed
@Gold856 Gold856 deleted the java-17-features branch June 5, 2024 04:28
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.

6 participants