Upgrade process from java 8 to java 21

I tried to do this in parallel of some other things this morning but then i had to focus on another task :downcast_face_with_sweat:

You are perfectly right, this is a side effect of the automatic refactoring performed by openrewrite. I fixed these cases locally

Just a note, I had trouble compiling Orekit in java 21 with intelliJ. It’s a known problem which requires some manual action. See here

Cheers,
Romain.

Pipeline is finally green with an exact 95.0% coverage, ready for merge :melting_face:

Merge request set to auto merge following latest update on develop branch :champagne:.

As a follow up, would you agree with using var as well @orekit_dev_team ? I would do another merge request for this. It may be very interesting for field related classes.

Cheers,
Vincent

Thanks vincent.

I do like var personnally, but I would restrict its use only when the type is obvious (call to constructor, cast)

Cheers,
Romain.

Indeed that is where it is the most redundant. In case we at least agree on that, i’ll take a look and see how to do this for obvious cases only

First of all, thank you @Vincent for the upgrade.

I’m not a Java expert and have no personal opinion on the use of var. So I innocently googled “Is it better to use var in java” to have some opinions. The three first results [1], [2] and [3] converge on the same remark: var parameter should only be used where the code is already perfectly understandable (e.g. variable name perfectly reflecting what the object is) and clear. If the code is not perfectly understandable, so the var should be avoided.

I was surprised to not see Baeldung website result since I consider it as a good reference for Java tips and good practices. So I search on the website and found an interesting article [4]. This article also highlights that var shall be used carefully and provide some guildelines (Chapter 4).

Finally, I asked ChatGPT (I’m sorry for people that don’t like AI). I asked the question: “Is it recommended to use “var” type in Java?” Please find the answer below:

In modern Java, var is generally recommended when it improves readability, but not as a blanket replacement for explicit types.

Java introduced local variable type inference with var in Java 10. It lets the compiler infer the variable type from the initializer.



Example:

var message = "Hello";   // inferred as String

var count = 42;          // inferred as int



When var IS a good idea

1. The type is obvious from the right side

var users = new ArrayList<User>();

This avoids repetition without hurting clarity.



2. Long generic types

var map = new HashMap<String, List<Order>>();

This is cleaner and easier to read.



3. Streams and iterators

var entry = map.entrySet().iterator().next();

The explicit type would add noise.



When var is NOT recommended

1. The type is unclear

var result = process(data);

What is result? A String? List<User>? Optional<Order>?

Explicit types are better here:

OrderResult result = process(data);



2. Primitive confusion

var number = 1;   // int, not long

Inference may not match your intent.



3. API/public code readability suffers

If another developer must mentally infer the type, readability decreases.


So, based on my Google researches, my opinion is that we have to be careful on the use of var and limit it to the cases where it is useful, applicable, and with no decrease on the code understability. In addition, since Orekit is an open source library for which the code can be read by users, we shall be strongly careful on the use of var.

[1]: https://www.reddit.com/r/java/s/tHvh6aAqIa

[2]: https://medium.com/@spextreme/java-var-should-be-a-code-smell-17af0be79646

[3]: https://groups.google.com/g/project-lombok/c/JiAEGIcgFO4

[4]: https://www.baeldung.com/java-10-local-variable-type-inference

Thanks Bryan for the analysis. I agree with careful use of var.

I’m not familiar with the use of var in Java, but C++ has a similar convention (auto) and in general I found that auto/var is very handy in cases where the variable use scope and general type are clear, but writing out the exact type is cumbersome for the user (for example, the iterator value for a loop that is moving through a vector containing complex template values). Otherwise I prefer to type variables explicitly.