Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use a bit more reflection to get it:</p> <pre><code>Method[] methods = stuffClass.getMethods(); for(Method m: methods) { if(m.getName().equals("doNiceStuff")) { Class&lt;?&gt; paramTypes = m.getParameterTypes(); //condition on the desired parameter types comes here if(paramTypes.length==1 &amp;&amp; A.class.isAssignableFrom(paramTypes[0]) { //we found it! } } } </code></pre> <p><strong>Why is isSuperClass() not good in this case?</strong></p> <p>Suppose we'd do it like this:</p> <pre><code>if (paramTypes.length == 1 &amp;&amp; paramTypes[0].getSuperclass().equals(A.class)) { ... </code></pre> <p>This solution would be good for the class <code>B</code>, but what if we have a class <code>C</code>?</p> <pre><code>public class C extends B { public void doNiceStuff(C arg0) { //do nice stuff here } } </code></pre> <p>This condition</p> <pre><code>paramTypes[0].getSuperclass().equals(A.class) </code></pre> <p>would be false! paramTypes[0].getSuperClass() is <code>B.class</code>, and is not equal to <code>A.class</code>...</p> <p><strong>Class.getMethods()</strong></p> <blockquote> <p>Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.</p> </blockquote> <p><strong>Class.isAssignableFrom()</strong></p> <blockquote> <p>Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false. Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.</p> </blockquote> <p>Recommended reading:</p> <ul> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html" rel="nofollow">Class</a></li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html" rel="nofollow">Method</a></li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isAssignableFrom%28java.lang.Class%29" rel="nofollow">isAssignableFrom</a></li> </ul>
    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.
 

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