Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I remember reading something interesting about <code>java.lang.Class</code> equality today that completely skipped my mind when I went back to trying to debug this thing. <a href="https://stackoverflow.com/questions/8850951/checking-a-class-type-class-is-equal-to-some-other-class-type">Jon Skeet mentioned in the answer to another question</a>:</p> <blockquote> <p>Yes, that code is valid - if the two classes have been loaded by the same classloader. If you want the two classes to be treated as equal even if they've been loaded by different classloaders, possibly from different locations, based on the fully-qualified name, then just compare fully-qualified names instead.</p> <p>Note that your code only considers an exact match, however - it won't provide the sort of "assignment compatibility" that (say) instanceof does when seeing whether a value refers to an object which is an instance of a given class. For that, you'd want to look at Class.isAssignableFrom.</p> </blockquote> <p>As <code>java.lang.Class</code> does not override <code>java.lang.Object.equals()</code>, and each <code>Class</code> object keeps a reference to its <code>ClassLoader</code>, even if two <code>Class</code>es have the same class, data and name, they won't be equal if they've come from two different <code>ClassLoaders</code>. So how does that apply here?</p> <p>The problematic line of code is</p> <pre><code>if( clazz.getAnnotation( MyAnnotationOne.class ) != null ) </code></pre> <p>Calling <code>clazz.getAnnotations()</code>, as above in the final update to the question will list an annotation with a fully-qualified class name that is equal to the fully-qualified class name of the class referenced by the <code>MyAnnotationOne.class</code> literal in the above if statement. However, I'm guessing that <code>clazz.getAnnotation()</code> uses some internal <code>equals()</code> call to check if the annotation classes are the same.</p> <p>The class referenced by <code>MyAnnotationOne.class</code> is loaded by the custom <code>ClassLoader</code>'s classLoader (which in most cases will be its parent). However, the <code>MyAnnotationOne</code> class that is attached to <code>clazz</code> is loaded by the custom <code>ClassLoader</code> itself. As a result, the <code>equals()</code> method returns <code>false</code>, and we get <code>null</code> back.</p> <p>Thank you very much to Holger for pushing me back in the right direction with this and getting me to the answer in the end.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload