Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>FYI, you could check </p> <pre><code>if(given == null || given.getClass() != expected) </code></pre> <p>to check if the class of <code>given</code> is <em>exactly</em> the class <code>expected</code>, not just assignment-compatible.<br> I'd call this one assertClass and your original suggestion assertInstance or assertInstanceOf.</p> <p><em>In response to the comment:</em><br> My answer was meant as a general tip, not as an answer to your original question. I added an "FYI" to make that clearer ;) </p> <p>Depending on what exactly you want, both ways may be reasonable.</p> <p>In the following example</p> <pre><code>public class Base {} public interface Ifc {} public class A{} extends Base implements Ifc public class B{} extends Base </code></pre> <p>the behavior of the methods above would be like this</p> <pre><code>A a=new A(); B b=new B(); assertClass(A.class, a); assertClass(Base.class, a); // fails, a.getClass() != Base.class assertClass(Ifc.class, a); // fails, a.getClass() != Ifc.class assertClass(B.class, b); assertClass(Base.class, b); // fails, b.getClass() != Base.class assertClass(Ifc.class, b); // fails, b.getClass() != Ifc.class assertInstance(A.class, a); assertInstance(Base.class, a); // works assertInstance(Ifc.class, a); // works assertInstance(B.class, b); assertInstance(Base.class, b); // works assertInstance(Ifc.class, b); // fails, B does not implement Ifc </code></pre> <p>Most likely you'll want to check with your method.<br> But if you want to validate the <em>exact</em> class, e.g. to make sure that it's <em>not</em> proxied or that it's <em>not</em> an extending class, my suggestion would be the way to do that.</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.
    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