Null safe @Delegate

116 views
Skip to first unread message

Tomasz Forys

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

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

enum NullHandling {
THROW_EXCEPTION,
RETURN_NULL
}


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

unread,
Jan 15, 2022, 1:07:12 AM1/15/22
to project...@googlegroups.com
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 project-lombo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/9b6a3235-142a-48fd-b492-e52b320d214cn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages