Dunno how this slipped by us for so long:
protected <T> T assertInstanceOf(Object o, Class<T> expectedType, boolean allowNull) { if (expectedType == null) fail("Null expectedType"); if (o == null && allowNull) return null; try { return expectedType.cast(o); } catch (ClassCastException e) { fail("Object " + o + " of class " + o.getClass().getName() + " is not an instanceof " + expectedType.getName()); // should not reach this return null; } }