hack API

Hacking third-party APIs on the JVM

The JVM ecosystem is mature and offers plenty of libraries, so you don’t need to reinvent the wheel. Basic - and not so basic - functionalities are just a dependency away. Sometimes, however, the dependency and your use-case are slightly misaligned. The correct way to fix this would be to create a Pull Request. But your deadline is tomorrow: you need to make it work now! It’s time to hack the provided API. In this post, we are going through some alternatives that allow you to make

software engineering

Timely computation of derived values

In my penultimate post, I described the use-case of an e-commerce shop. In general, interactions in such a shop are the following: You can browse a catalog of itemsEach item has a price tagYou can put x items in your cart It’s when the exciting part starts. On most pages, you want to give users a condensed view of their cart’s state. Here are a couple of options for the state’s view: Empty/non-emptyNumber of cart linesNumber of itemsTotal cost. In demos, it’s as sim

Stream Collector API

Teeing, a hidden gem in the Java API

Last week, I described a use-case for a custom Stream Collector. I received a intriguing comment on Twitter: Interesting article. For completness' sake this specific problem could also be solved using the standard teeing collector— Miguel Martins (@Miguucm) May 3, 2021 Hats off to you, Miguel! Your comment revealed a method I didn’t know! So I decided to investigate what the teeing() method is about. Returns a Collector that is a composite of two downstream collectors.

Stream Collector API

A real-world example of a Stream Collector

Java Stream’s Collectors methods fit most use-cases. They allow returning either a Collection or a scalar. For the former, you use one of the toXXX() method, for the latter, one of the reducing() one. Let’s imagine an e-commerce platform that implements a shopping cart. The cart is modeled as the following: This diagram might translate into the following (abridged) code: Product.java public class Product { private final Long id; (1) priva

Good practice overengineering

An example of overengineering - keep it WET

This week’s post is pretty short. I’ve already written about overengineering, but this adds a personal touch. I had to rewrite my Jet Train demo to use another data provider, switching from a Swiss one to a Bay Area one. One of the main components of the demo is a streaming pipeline. The pipeline: Reads data from a web endpointTransforms data through several stepsWrites the final data into an in-memory data grid Most of the transform steps in #2 enrich the data. Each of them requi

delegate

A look at Kotlin's delegation

Kotlin offers many exciting features. In general, developers tend to cite null safety as their favorite. For me, it’s function extensions. But delegation comes a close second. The delegation pattern The delegation pattern is described in the GoF book: Delegation is a way to make composition as powerful for reuse as inheritance [Lie86, JZ91]. In delegation, two objects are involved in handling a request: a receiving object delegates operations to its delegate. This is analogous to su

Spring Data Spring Boot customization

Your own custom Spring Data repository

Frameworks promise to speed up one’s development pace provided one follows the mainstream path. The path may be more or less narrow. I’m a big fan of the Spring ecosystem because its design is extensible and customizable at different abstraction levels: thus, the path is as large as you need it to be. Functional Programming is becoming more and more popular. Spring provides a couple of DSLs for the Kotlin language. For example, the Beans DSL and the Routes DSL allow for a more funct

security JVM hack

Changing a field's type in recent JDKs

A couple of years ago, I attended a talk of my former colleague (but still friend) Volker Simonis. It gave me the idea to dig a bit into the subject of how to secure the JVM. From the material, I created a series of blog posts as well as a talk. From that point on, I submitted the talk at meetups and conferences, where it was well-received. Because I like to explore different areas, I stopped to submit other proposals. Still, the talk is in my portfolio, and it was requested again in 2021. I ha

Hibernate JPA persistence

A (definitive?) guide on LazyInitializationException

Posts that have been written about Hibernate’s LazyInitializationException could probably fill whole books. Yet, I believe each of them focuses on a particular aspect of it: some on a specific solution, some on how to solve it with Spring Boot, etc. I’d like this post to be the definitive guide on the subject, even though I’m pretty sure it won’t. At least, I’ll be able to point others to it. The root cause Whether you love or hate ORM frameworks in general, they

GraalVM Spring Native Image AOT

Kicking Spring Native's tires

I’ve been playing with GraalVM AOT compilation capability since I became aware of it. As a long-time Spring aficionado, I carefully monitored the efforts that the engineers at Tanzu have put into making Spring AOT-compatible. Recently, they announced the beta version of the integration. In this post, I want to check how easy it is to produce a (working!) Docker image from an existing Spring Boot application. Introduction GraalVM provides many different features. Among them, the componen