TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

JEP draft: Prepare to make final mean final

207 pointsby mfiguiereabout 2 months ago

22 comments

GauntletWizardabout 2 months ago
When I had the brief displeasure of working on HDFS at Facebook, we took a series of customer meetings to figure out how to get our oldest customers to upgrade their clusters. I was in a meeting with the photos team about what their requirements were and what was blocking them from upgrading, and they were very frank - they asked if the upgrade preserved the internal struct types associated with blocks on the disc servers. They didn&#x27;t actually use hdfs as a file system, they allocated 1 GB files with zero replication, then used deep reflection to find the extent that comprised them on the discful storage servers, then built their own archival backup file system on top of that. I was horrified. The some of the older hats on the team were less surprised, having had some inkling of what was going on, even though they clearly didn&#x27;t understand the details. Others considered it tantamount to sacrilege.<p>I think about this a lot. What they had built was probably actually the best distributed file system within Facebook. It was similarly structured to unraid, and had good availability, durability, and space saving properties, but the approach to engineering was just so wrong headed in my opinion that I couldn&#x27;t stomach it. Talking about it with other Java programmers within facebook, nobody seemed to mind. Final was just a hint after all.
评论 #43540831 未加载
评论 #43541532 未加载
cogman10about 2 months ago
Hmm, not a bad approach.<p>I think the one thing that&#x27;d be nice is if I could somehow tell the JVM from a class that this class is open for final mutation rather than needing special flags passed into the JVM or special manifests in the Jar. It&#x27;s often pretty clear to me, as a dev, what I when I need something to have final mutation (generally only with serialization objects).<p>For example,<p><pre><code> @FinalMutatableByReflection class Foo { final String bar; } </code></pre> That&#x27;d allow me to transition code by just adding an annotation where it needs to be while also getting the benefit that final is really final everywhere else in code that isn&#x27;t working with serialization.
评论 #43540145 未加载
评论 #43541506 未加载
评论 #43542330 未加载
评论 #43540109 未加载
elricabout 2 months ago
I&#x27;ve written my fair share of nasty reflexive code for testing or for abusing libraries, but I don&#x27;t think I&#x27;ve ever overwritten final fields in this way. Private fields, sure. But not final.<p>Sounds like a good evolution to me.
TOGoSabout 2 months ago
&gt; Application developers can avoid both current warnings and future restrictions by selectively enabling the ability to mutate final fields where essential.<p>&#x2F;me raises hand<p>Maybe if you want to mutate a field, don&#x27;t mark it `final`?<p>I know, I know, people like to pretend things are one way and then hand their objects over to some horrid framework that breaks all the rules, because apparently giant web of mutable spaghetti is <i>just fine, not an anti-pattern at all</i> if you let some third-party bull$#!7 ORM&#x2F;dependency-injection-framework-for-people-who-don&#x27;t-like-constructors do it.
exabrialabout 2 months ago
I&#x27;m 100% onboard with this. My thought was how are they going to make Serialization work, but looks like they thought of that.<p>I was trying to think of an edge case with JsonB or JAXB that would be affected by this... but generally those frameworks have told you for quite awhile not to do stupid stuff like:<p>``` @Getter public class HelloMessage { @JsonbProperty private final String helloMessage; } ```<p>I can&#x27;t think of any frameworks offhand that do this.
评论 #43540656 未加载
magicalhippoabout 2 months ago
So this is about warning about using deep reflection or such to modify fields marked as final.<p>Not a Java dev, so I thought it might be related to classes marked final somehow. But this seems like a reasonable proposal, at least in spirit.
theanonymousoneabout 2 months ago
If there was a r&#x2F;nottheonion for tech :D<p>Jokes aside, I thought the ability to mutate final fields was already removed&#x2F;restricted after Java 17 :&#x2F;
bironranabout 2 months ago
A cursory glance at &quot;setAccessible&quot; usage reveals popular libraries such as serializers like gson and jaxb, class manipulation and generation like cglib, aspectj and even jdk.internal.reflect, testing frameworks and libraries including junit, mockito and other mocking libraries, lombok, groovy, spring, and the list goes on and on.<p>My bet is that this will be yet another &quot;checked exception&quot; or &quot;module system&quot;, where many applications now need to add &quot;--add-opens&quot;. If you&#x27;ll use ANY of many of the more popular frameworks or libraries you&#x27;ll end up giving this assurance away, which will make library developers not able to rely on it and we&#x27;re back to square one.
评论 #43540421 未加载
评论 #43539649 未加载
评论 #43539702 未加载
评论 #43540453 未加载
nightpoolabout 2 months ago
<p><pre><code> [Speculative optimizations] may not suffice in this case as future planned optimizations may wish to rely not only on immutability within the lifetime of the process, but also on the immutability of fields from one run of the application to the next. </code></pre> Can someone elaborate a little more on what this means? I&#x27;m very surprised to hear that this was considered a blocker important enough to add all of this complicated machinery (and breaking several deserialization libraries...), when I&#x27;ve never even heard of such an optimization and can&#x27;t imagine what sort of form it would take
评论 #43540492 未加载
评论 #43540608 未加载
评论 #43540170 未加载
neonsunsetabout 2 months ago
.NET went through a similar change, blocking `static readonly` fields from being accessible via private reflection. Unfortunately, a lot of serializers and all sorts of meta-programming libraries depend on (mutable) private reflection of instance fields still so for now they are not blocked and JIT cannot treat them as truly immutable, turning into JIT constants the way it does so for static readonly fields. Although I guess you can always make a struct and place it into a static readonly, where each field could be such JIT constant (within certain limits).
mberningabout 2 months ago
If they implement this in a way similar to the package visibility changes your list of JVM args is about to explode in order to support legacy apps.
jjmarrabout 2 months ago
From my perspective as a C++ developer, every attempt to use `const` for compiler optimization appears to be stymied by the existence of `const_cast`, because modifying a `const` value is only undefined behaviour if the underlying object is `const`. Glad to see that Java is willing to break the language to improve it.
评论 #43540159 未加载
评论 #43539753 未加载
immibisabout 2 months ago
I don&#x27;t see the point in putting another guard rail behind the override that&#x27;s supposed to remove all guard rails. Java doesn&#x27;t even have a security model any more, so what are you protecting?<p>Programmers from their own mistakes? That&#x27;s fine, but this is about cases where they set the &quot;I really mean it and this isn&#x27;t a mistake&quot; flag.<p>The JVM&#x27;s optimized code from programmers? Plausible, but aren&#x27;t there already many cases where things get deoptimized based on run-time state changes?<p>It feels like someone just said &quot;final means final!&quot; without really thinking about the purpose of a JVM. Will there be a proposal to enforce checked exceptions, too?
评论 #43555151 未加载
评论 #43555584 未加载
Traubenfuchsabout 2 months ago
I would like to see some realistic example scenarios where this could actually lead to a speedup and how much it actually speeds up.
评论 #43539727 未加载
aardvark179about 2 months ago
So happy to see this, thank you Ron and Alan.
arsabout 2 months ago
Does this mean I should start marking my variables (and function parameters) with Final?<p>Up till now I always assumed the compiler would figure out on its own which variables were final, and optimize as needed. But this JEP makes it seem like there are optimizations that only happen if you manually mark the variable.
评论 #43544753 未加载
satyanashabout 2 months ago
&gt; <i>Prepare to make final mean final</i><p>missed opportunity to call it &quot;final final&quot;
keyboredabout 2 months ago
I don’t see a way to opt-in to a hard error if this happens somewhere in the guts of the code (third-party probably).
评论 #43540057 未加载
stickfigureabout 2 months ago
Great! Now can we make `final` the default for all fields, variables, and parameters?<p>(yes yes, I know, that would break syntax... but please come up with something to discourage mutability)
评论 #43539333 未加载
评论 #43543176 未加载
评论 #43540550 未加载
评论 #43539950 未加载
评论 #43541493 未加载
xxsabout 2 months ago
Around 19 year late, still better than never.
Almondsetatabout 2 months ago
final ly
userabout 2 months ago
This comment is non-sensical.