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.

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 a library like Kara. String doc = html( head( meta(charset -> "utf-8"), link(rel->stylesheet, type->css, href->"/my.css"), script(type->javascript, src -> "/some.js") ),… 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 »

Anonymous Types in Java

Posted by & filed under Java.

Java allows casting to an intersection of types, e.g. (Number & Comparable)5. When combined with default methods on interfaces, it provides a way to combine behaviour from multiple types into a single type, without a named class or interface to combine them. Let’s say we have two interfaces that provide behaviour for Quack and Waddle…. Read more »

Lambda Type References

Posted by & filed under Java.

We’re used to type erasure ruining our day in Java. Want to create a new T() in a generic method? Normally not possible. We can pass a Class<T> around, but that doesn’t work for generic types. We can’t write List<String>.class One exception to this was using super type tokens. We can get the generic type… Read more »

Yield Return in Java

Posted by & filed under c#, Java.

A feature often missed in Java by c# developers is yield return It can be used to create Iterators/Generators easily. For example, we can print the infinite series of positive numbers like so: public static void Main() { foreach (int i in positiveIntegers()) { Console.WriteLine(i); } }   public static IEnumerable<int> positiveIntegers() { int i… Read more »

Adding toList() to Java Streams

Posted by & filed under Java.

The Java Streams API is lovely, but there are a few operations that One repeats over and over again which could be easier. One example of this is Collecting to a List. List<String> input = asList("foo", "bar"); List<String> filtered = input .stream() .filter(s -> s.startsWith("f")) .collect(Collectors.toList());List<String> input = asList("foo", "bar"); List<String> filtered = input .stream()… Read more »

Work around Java “same erasure” errors with Lambdas

Posted by & filed under Java.

A common frustration with Java is the inability to overload methods when the method signatures differ only by type parameters. Here’s an example, we’d like to overload a method to take either a List of Strings or a List of Integers. This will not compile, because both methods have the same erasure. class ErasureExample {… Read more »