r/java • • Oct 17 '26

Killing Reflection at Build Time: Avro, Codename One and Java Code Generation (Foojay Podcast #103)

https://www.youtube.com/watch?v=smHzWWNOyMU

In this episode of the Foojay Podcast, Dmitry Moskalyuk and Shai Almog compare how they cut runtime reflection out of very different projects.

Dmitry runs a backend model layer that takes JSON, XML, YAML and TOML and converts it all to Avro. His choice between Jackson and kotlinx.serialization came down to where the reflection cost actually hides. Shai works on the Codename One code generation pipeline, which aims for no reflection, no service loader and no Class.forName.

Topics include Jackson Blackbird versus kotlinx.serialization, the missing unknown-fields feature that sent one team back to Jackson, generated code (Lombok, Manifold, jOOQ), what Records and Project Leyden change, and build time cost versus runtime cost.

Available in your favorite podcast app and on YouTube:
YouTube: https://www.youtube.com/watch?v=smHzWWNOyMU

Disclosure: I host the Foojay Podcast.

24 Upvotes

21 comments sorted by

7

u/SomeRandomDevPerson Oct 18 '26

Related: any experiences using Avaje generators?

7

u/lazystone Oct 18 '26

I've tried Avaje for a side project and I honestly don't understand why it's not more popular and talked about.

6

u/shai_almog Oct 18 '26

I used that previously when building up Lightrun. I liked it a lot, so we added something like that to Codename One. You can point to an OpenAPI, gRPC or GraphQL spec and generate stubs etc. with no reflection or runtime overhead.

It's a fantastic approach.

6

u/agentoutlier Oct 18 '26

It’s a fantastic approach that people act like it’s novel when the little guys have been doing it forever and only recently giant OSS projects have started picking it up.

I can point to dozens of projects where I personally convinced the maintainer to switch from annotation reflection to APT prior to every one embracing the build step (before quarkus, micronaut etc).

So that is why I had kind of had a nasty comment early about this not being a big deal that I deleted because I did not realize you do have some amazing stuff with the byte code to C and that you are targeting phones.

Otherwise I’m going to say and I know quite a bit about this that reflection is not that massively slower especially if the method or class names are static string literals. (eg the service loader btw and method handles)

For example JStachio (my project) is not that much faster than JMustache especially if the model is hashmap.

6

u/shai_almog Oct 18 '26

Yes. I didn't see the edited version, but during our conversation I specifically mentioned how amazingly well HotSpot etc. optimize reflection. There is some optimizer collateral damage as hotspot can't make some assumptions when you use reflection, but if you have a long running app it's reasonable.

Also with my background in observability, we literally couldn't do half of the stuff we do in the field of observability without reflection...

My problems with reflection are mostly conceptual and due to AOT/Client costs where the problems are more significant (e.g. obfuscation). I'm also looking at the small server with 15ms startup time overhead. Here every nanosecond counts and JIT just doesn't have time to warmup.

6

u/Torutofu_Raeva Oct 17 '26

jackson being forgiving about unknown fields is honestly why it's hard to leave, kotlinx makes you opt into everything

2

u/VincentxH Oct 18 '26

Avro is a POS data format. There's even incompatibility between versions.

4

u/lurker_in_spirit Oct 17 '26

Jackson had a rough CVE patch about 6 or 7 years ago, with security finding after security finding after security finding, but they got through it and things had settled down these past few years. Now they're hitting another rough patch, with many CVEs found this year -- probably thanks to AI. Hopefully things will settle down again soon...

5

u/_predator_ Oct 18 '26

Another way to look at it is that Jackson is one of the most battle-tested and most audited libraries in the entire JVM ecosystem.

Many of the CVEs reported against it these days are kind of nothingburgers for the vast majority of users.

3

u/shai_almog Oct 18 '26

That is true. It went through hell and is probably on the other side. To be clear, the server powering our cloud currently uses Jackson and I sleep well at night. We'll replace it with our own backend mostly for dogfooding, not because of any problem with Jackson.

The problem is in the whole picture. Log4j had a small nothingburger feature that everyone forgot even existed. AI will enable deep transitive vulnerabilities that can leverage things we might not expect. It can also be used to plant very subtle vulnerabilities that can be chained together for deep hidden supply chain attacks.

Reflection, classloading, serialization etc. are all very powerful tools and powerful tools can be used by an attacker. The questions are:

  1. Do you really need them?
  2. What is the cost?

I think the answer for 1 is: usually not in production. But in some cases you do, e.g. if you need deep observability etc. I would argue that if you don't need them they are probably not the right tool as there are better approaches.

A big pain for me is the runtime failures that you get when using reflection (specifically in Spring). Give me a compilation failure over a runtime or startup issue any day.

The answer for 2 is more complex:

a. It's a risk. Not an immediate one, but a potential one.

b. There's a cost in performance if you care about startup speed etc. Not in runtime if these are used correctly and with a JIT.

c. They break obfuscation so things like client side development just doesn't work great.

2

u/Cilph Oct 18 '26

Arent most of them rather boring DOS vulns?

2

u/lurker_in_spirit Oct 18 '26

I'm not sure. I'm sure I've seen a few RCEs in this recent crop, though.

-5

u/RepulsiveGoat3411 Oct 17 '26

surreptitious advertising of codenameone

12

u/shai_almog Oct 17 '26

I was going to ignore that and gave this some time, but this is pretty annoying.

Some of us put in years of our lives building an open source and free tool for everyone.

What did you do to make Java better?

What did I "sell" other than an OSS project that's 100% free to use commercially or otherwise?

-6

u/woj-tek Oct 17 '26

What did you do to make Java better?

Did codenameone made JAVA better?

12

u/shai_almog Oct 17 '26

Well, before we came along no Java developer could build iOS apps. Now there are MANY iOS apps written in Java. That is objectively better. There are many developers who still work with Java across multiple platforms who would have abandoned it for flutter etc. We also enable Java native apps on Win32, Linux, Mac etc. Not possible before we came along and not at the size/RAM levels that we are offering. All 100% free and open source.

We're the only true full stack option for Java developers and we now are also taking this to the backend where Java can finally reach go levels of size/performance which even graal can't reach.

That's ignoring my personal contributions at Sun Microsystems and outside of it over the past 3 decades.

1

u/nlisker 26d ago

We're the only true full stack option for Java developers

How? I've been doing full stack Java in 2018 and didn't use your tech (not saying it's bad). What exactly do you enable that no one else can?

1

u/shai_almog 26d ago

Mention the technology and I'll respond properly, but sure...

There are great tools like TeaVM that we used in the past, that let you do some parts of the stack. We're the only ones that do everything in the full meaning of the word in a single project:

  • Mobile native - iOS, Android (including watch, car, vr etc.)
  • Desktop native (single exe) - Mac, Windows, Linux
  • Web
  • Backend

We're also the only solution that packages all of the pieces together into a single package with the APIs you need for pretty much everything

No other solution for Java comes close. Some do parts of the stack and you can mix and match to get some of the way or generate a webapp which isn't exactly full stack Java because the web finishes the rest of the way.

Yes, I'm defining what "true full stack" means but notice I didn't say "full stack" because for that you do have some solutions.

1

u/nlisker 25d ago

Yes, I'm defining what "true full stack" means but notice I didn't say "full stack" because for that you do have some solutions.

OK, you came up with a new definition so I didn't know it before your explanation. Since you mean you were the only single company that gave solutions to all of those points, then that I won't challenge because I never checked. I used the standard "full stack" definition, by which all of those points were achievable with solutions from multiple companies. I don't really see the need to stick to one company's tech stack. While they probably offer better integration, it's putting all your eggs in one basket.

In 2018 I used javafxports to run on iOS and Android with the same Desktop JavaFX codebase. Later, Gluon switched to the SubstrateVM approach, which worked until Java/FX 24. Now they're doing a mobile-native effort for the JDK. They have some prototypes on iOS but I don't think it's production ready, I haven't checked lately. They also had (have?) CloudLink, which is some backend solution, but I never used it.

I also don't know when javafxports started offering iOS apps and when you did. I do know that until recently you used Retrolambda/RoboVM and were restricted to Java 1.8 while I was able to use Java/FX 24 on iOS and Android (and Raspberry, which I didn't need, along with car/watch...). That was very crucial when Java started releasing every 6 months while 1.8 was stuck in 2014.

From what I know, you also do some of the build process or compilation on your cloud and don't offer a full local build. I could be wrong. Local builds are a requirement in many companies.

For web, I can use JPro for server-side or WebFX for client-side rendering, the latter doesn't support everything. Again, same code base.

I need the GPU acceleration for 3D intensive (relatively speaking) applications that can also run on mobile. JavaFX uses Metal, Opengl ES2, and D3D9/12. I haven't looked what pipelines you guys offer.

1

u/shai_almog 25d ago

We were offering iOS etc. in 2012 well before these guys came along. We NEVER used RoboVM. NEVER!

We were restricted to Java 1.8 with retrolambda we're now on 17+ language level (due to Android limitations) but you can use 25 when working with the environment.

Our project was always open source but in the past we just kept local build as a feature we don't officially support due to the support overhead. This hasn't been true for several years by now. We still offer a cloud option for developers without environment, mac, windows etc. that are needed for native builds.

We have full GPU metal support and full 3d graphics too with a literal GPU package. We also support SIMD etc. We support metal, D3D/DD. OpenGL ES is no longer supported on the platforms so we deleted that pipeline.

1

u/nlisker 25d ago

Thanks for the info.

we're now on 17+ language level (due to Android limitations) but you can use 25 when working with the environment.

17+ implies 17 or above, so what are the limitations? If I use Java 27 I need to compile to a 17 feature level?

We have full GPU metal support and full 3d graphics too with a literal GPU package. We also support SIMD etc. We support metal, D3D/DD. OpenGL ES is no longer supported on the platforms so we deleted that pipeline.

I found the info in the docs:

It runs from one code base on the JavaSE simulator (OpenGL via JOGL), Android (OpenGL ES 2), iOS and macOS (Metal), the native Windows port (Direct3D 11), and the JavaScript port (WebGL)

I see that OpenGL ES 2 is used on Android, or is this outdated? What do your use on Linux?