Java 16 Pattern Matching Fun

Posted by & filed under Java.

Java 16 brings Pattern Matching for instanceof. It’s a feature with exciting possibilities, though quite limited in its initial incarnation.  Basic Pattern Matching We can now do things like  Object o = "hello"; if (o instanceof String s) { System.out.println(s.toUpperCase()); }Object o = "hello"; if (o instanceof String s) { System.out.println(s.toUpperCase()); } Note the variable… Read more »

Revisiting Html in Java

Posted by & filed under Java.

Some time ago I wrote a post about creating an embedded dsl for Html in Java. Sadly, it was based on an abuse of lambda name reflection that was later removed from Java. I thought I should do a followup because a lot of people still visit the old article. While it’s no longer possible… Read more »

Sealed Java State Machines

Posted by & filed under Java.

A few years back I posted about how to implement state machines that only permit valid transitions at compile time in Java. This used interfaces instead of enums, which had a big drawback—you can’t guarantee that you know all the states involved. Java 15 brings a preview feature of sealed classes, solving this downside.

Fun with Java Records

Posted by & filed under Java.

A while back I promised to follow up from this tweet to elaborate on the fun I was having with Java’s new Records (currently preview) feature. Records, like lambdas and default methods on interfaces are tremendously useful language features because they enable many different patterns and uses beyond the obvious. Java 8 brought lambdas, with… Read more »

Learning from Pain

Posted by & filed under ContinuousDelivery, Java, XP.

Pain is something we generally try to avoid; pain is unpleasant, but it also serves an important purpose. Acute pain can be feedback that we need to avoid doing something harmful to our body, or protect something while it heals. Pain helps us remember the cause of injuries and adapt our behaviour to avoid a… Read more »

End to End Tests

Posted by & filed under Java, Testing, XP.

End to end automated tests written with Webdriver have a reputation for being slow, unreliable (failing for spurious reasons), and brittle (breaking with any change). So much so that many recommend not using them. They can become a maintenance burden, making it harder, rather than easier, to make changes to the user interface. However, these… Read more »

Representing the Impractical and Impossible with JDK 10 “var”

Posted by & filed under Java.

Having benefited from “var” for many years when writing c#, I’m delighted that Java is at last getting support for local variable type inference in JDK 10. From JDK 10 instead of saying ArrayList<String> foo = new ArrayList<String>();ArrayList<String> foo = new ArrayList<String>(); we can say var foo = new ArrayList<String>();var foo = new ArrayList<String>(); and… Read more »

Optionally typechecked StateMachines

Posted by & filed under Java.

Many things can be modelled as finite state machines. Particularly things where you’d naturally use “state” in the name e.g. the current state of an order, or delivery status. We often model these as enums. enum OrderStatus { Pending, CheckingOut, Purchased, Shipped, Cancelled, Delivered, Failed, Refunded }enum OrderStatus { Pending, CheckingOut, Purchased, Shipped, Cancelled, Delivered,… Read more »

HTML in Java

Posted by & filed under Java.

Update: this technique no longer works since name reflection was removed in later versions of Java. Here is another approach. Another use of lambda parameter reflection could be to write html inline in Java. It allows us to create builders like this, in Java, where we’d previously have to use a language like Kotlin and… Read more »

Lambda parameter names with reflection

Posted by & filed under Java.

Java 8 introduced a compiler flag -parameters, which makes method parameter names available at runtime with reflection. Up to now, this has not worked with lambda parameter names. However, Java 8u60 now has a fix for this bug back-ported which makes it possible. Some uses that spring to mind (and work as of recent 8u60ea… Read more »