[ 
https://issues.apache.org/jira/browse/SLING-3714?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14048040#comment-14048040
 ] 

Stefan Seifert commented on SLING-3714:
---------------------------------------

i think this can be a good solution for SLING-3709 - especially because it does 
not required changes to the existing adaptTo method.

for my personal wish - having a more compact and null-safe possibility to adapt 
objects - it does not help, because it replaces a necessary null-check with a 
necessary isSuccess check. an example for illustration:

a caller wants to adapt an object to another object, and he knows that it 
cannot be null unless something serious got wrong, e.g. the bundle with the 
adapterfactory is missing or something is misconfigured.

currently the only way to do this is with an explicit null check:
{code:java}
MyClass myClass = otherObject.adaptTo(MyClass.class);
if (myClass==null) {
  throw new RuntimeException("Unable to adapt OtherClass to MyClass.");
}
// further processing
{code}
without this extra boilerplate code and NPE would be thrown without any context 
information which class was adapted to which class.

having a method like adpatToNotNull leads to much cleaner code but still a 
meaningful error message in case of a serious error:
{code:java}
MyClass myClass = otherObject.adaptToNotNull(MyClass.class);
// further processing
{code}

"adaptToNotNull" is not the best name, perhaps a better can be found. 

we have hundreds of adaptTo calls in our codebase, and really want to avoid 
such repetitive boilerplate code. the root case of the exception can contain 
more detailed error information.

but this wish is not directly related to SLING-3709. still i think your 
proposal is a bit crude from an API point of view because it deals with 
exception handling in a uncommon way compared to java standards. bit the big 
plus is of course that it does not change the existing API.

> Allow for a caller to request a non-null response from adaptTo()
> ----------------------------------------------------------------
>
>                 Key: SLING-3714
>                 URL: https://issues.apache.org/jira/browse/SLING-3714
>             Project: Sling
>          Issue Type: Improvement
>            Reporter: Justin Edelson
>
> See SLING-3709 for a Sling Models-specific request. As I commented there, I 
> think this makes more sense as a core change to the Adaptable interface.
> One option:
> resource.adaptTo(Result<Node>.class)
> Which returns a Result object, which has an API like:
> interface Result<T> {
> boolean isSuccess();
> T getObject();
> List<Error> getErrors();
> }



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to