Null safe @Delegate

Skip to first unread message

Tomasz Forys

Jan 13, 2022, 12:42:46 PMJan 13
to Project Lombok
Is it possible to add null checks on for fields annotated with @Delegate?
Something like

@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Delegate {
Class<?>[] types() default {};
NullHandling onNull() default NullHandling.THROW_EXCEPTION;
Class<?>[] excludes() default {};

enum NullHandling {

And then if field == null and @Delegate(onNull=RETURN_NULL) code will not throw NPE but just return null (for primitives return "default" value or still throw NPE).
With default value =  THROW_EXCEPTION  - it would be backward compatible.

Reinier Zwitserloot

Jan 15, 2022, 1:07:12 AMJan 15
Hi Tomasz,

Interesting idea but I’m not sure it’s worthwhile. A setting might seem simple and innocuous, but I don’t think lombok is actually improved if we have 50 parameters on every exception, most of which are exotic and therefore it would not be immediately obvious as to why most of them exist.

It’s also a tad hairy. What do we do for a method that returns a primitive? Return 0  / false  / 0.0 / '\0' ? I think   that’s reaching - but it shows that RETURN_NULL  simply cannot be the name of that enum; it’d have to be RETURN_DEFAULT  or something, but how would you describe the behaviour of java for uninitialized non-final fields (0 / null / false / 0.0 / etc), also in light of valhalla types in future java releases? I don’t think a pithy term even exists.

Thus, clarity kills this proposal. I don’t see any route to make clear at a glance what this does, and this feature is by no means useful enough to warrant adding something that requires reading a manual to figure out.

You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
Reply all
Reply to author
0 new messages