Groovy null and isEmpty checks on child collections
<p>I have the following user class:</p>

<pre><code>class User {
    String username
    Set&lt;Role&gt; roles
}
</code></pre>

<p>In the application, I call <code>SecurityUtils.getUser()</code> to get the current logged-in user. This method returns a valid <code>User</code> instance if the user is logged in, or <code>null</code> if they are unauthenticated/anonymous:</p>

<pre><code>User user = SecurityUtils.getUser()
</code></pre>

<p>I am trying to write a method where I need to determine if the user is logged in or not, and if they are, I need to do something special if their set of <code>Roles</code> are either empty or <code>null</code>:</p>

<pre><code>if(user &amp;&amp; !user.roles) {
    // Do something special
}
</code></pre>

<p>I would like to make this Groovier, so I tried:</p>

<pre><code>if(!user?.roles) {
    // Do something special  
}
</code></pre>

<p>And this is not working. Again, the <code>// Do something special</code> should <em>only</em> execute if:</p>

<ol>
<li>The <code>user</code> is non-null (authenticated); and (</li>
<li>Has a set of <code>Roles</code> that is null; or</li>
<li>Has a set of <code>Roles</code> that is empty )</li>
</ol>

<p>Any ideas?</p>