velocity-engine icon indicating copy to clipboard operation
velocity-engine copied to clipboard

Support Java 16 records in property lookup

Open jtotht opened this issue 1 year ago • 2 comments

Java 16 records (JEP 395) are a perfect fit for Velocity variables: they are lightweight and desgined to contain immutable data. However, records don’t follow the JavaBean convention: the getter of the field foo is foo(), not getFoo(). Add support for this naming convention.

While this naming convention is mostly useful in projects using Java 16 records, the patch itself doesn’t rely on records, so it doesn’t break Java 1.8 compatibility.

jtotht avatar Apr 14 '24 13:04 jtotht

Stupid question: is this limited to records only or will it work with any method without get prefix?

michael-o avatar Apr 14 '24 13:04 michael-o

Actually, I could only test it with regular classes because of the project’s Java 1.8 support. So other methods work definitely, records work hopefully. :)

jtotht avatar Apr 14 '24 13:04 jtotht

I'm inclined to accept this PR. There is a tiny corner case compatibility issue for templates relying on a property not to be found in a object having such a method for the property, I consider it sufficiently improbable and unusual to move forward without introducing one more compatibility flag.

arkanovicz avatar Sep 07 '24 11:09 arkanovicz

Thanks for merging it! I’ve tried out Velocity 2.4 in my Java 17 project, and it works like a charm!

jtotht avatar Oct 01 '24 08:10 jtotht

I could be wrong, but I believe this change introduced a breaking change. I created ticket https://issues.apache.org/jira/browse/VELOCITY-986 to demonstrate problem.

patrickjamesbarry avatar Jan 07 '25 19:01 patrickjamesbarry

This change was introduced a breaking change, when we have a map with list. The details in this mail https://lists.apache.org/thread/grd2bhgjjjmbo5rmyn7540btj924lq69 cc @arkanovicz @jtotht @michael-o

ravimergu1 avatar Jul 23 '25 13:07 ravimergu1