I've been thinking about tree-shaking in Java and whether it could actually work if we handle dependency injection at compile-time instead of at runtime and avoid using reflection altogether. Previously, I remember discussions suggesting that due to Java's dynamic features, tree-shaking might not be feasible. But I wonder if these conditions change things?
5 Answers
.NET has tackled this problem with a workable solution by using annotations to describe dependencies combined with some manual adjustments. It could be a model to look at for Java as well.
In my last job, we developed a proprietary tree-shaker for packaging our client application to minimize size. We specified one or more root classes, and the tool would backtrack to figure out the necessities. There were also controls to opt into packages as needed. It functioned surprisingly well, and the implementation was less complex than you'd think!
It's definitely possible to achieve even with some runtime dependency injection and reflection, as long as you define how DI and reflection are used at compile-time. Both GraalVM and Android apps implement this concept, so those are great resources to dive deeper into.
Isn’t this the exact goal of JPMS and jlink? They aim to help with modularization which could theoretically allow for more efficient tree-shaking.
Just a heads up: while you might gain some advantages like reduced startup time or smaller artifacts, the trade-off could increase compile-time complexities and affect how predictable builds are. It sounds tempting, but be careful as it might lead you into a noob trap.
Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically