Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I tried this code and it returns the actual generic type class so it seems the type info can be retrieved. However this only works for method 1 and 2. Method 3 does not seem to return a list typed String as the poster assumes and therefore fails.</p> <pre><code>public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { try{ Method m = Main.class.getDeclaredMethod("method1", new Class[]{}); instanceOf(m, List.class, String.class); m = Main.class.getDeclaredMethod("method2", new Class[]{}); instanceOf(m, List.class, String.class); m = Main.class.getDeclaredMethod("method3", new Class[]{}); instanceOf(m, List.class, String.class); m = Main.class.getDeclaredMethod("method4", new Class[]{}); instanceOf(m, StringList.class); }catch(Exception e){ System.err.println(e.toString()); } } public static boolean instanceOf ( Method m, Class&lt;?&gt; returnedBaseClass, Class&lt;?&gt; ... genericParameters) { System.out.println("Testing method: " + m.getDeclaringClass().getName()+"."+ m.getName()); boolean instanceOf = false; instanceOf = returnedBaseClass.isAssignableFrom(m.getReturnType()); System.out.println("\tReturn type test succesfull: " + instanceOf + " (expected '"+returnedBaseClass.getName()+"' found '"+m.getReturnType().getName()+"')"); System.out.print("\tNumber of generic parameters matches: "); Type t = m.getGenericReturnType(); if(t instanceof ParameterizedType){ ParameterizedType pt = (ParameterizedType)t; Type[] actualGenericParameters = pt.getActualTypeArguments(); instanceOf = instanceOf &amp;&amp; actualGenericParameters.length == genericParameters.length; System.out.println("" + instanceOf + " (expected "+ genericParameters.length +", found " + actualGenericParameters.length+")"); for (int i = 0; instanceOf &amp;&amp; i &lt; genericParameters.length; i++) { if (actualGenericParameters[i] instanceof Class) { instanceOf = instanceOf &amp;&amp; genericParameters[i].isAssignableFrom( (Class) actualGenericParameters[i]); System.out.println("\tGeneric parameter no. " + (i+1) + " matches: " + instanceOf + " (expected '"+genericParameters[i].getName()+"' found '"+((Class) actualGenericParameters[i]).getName()+"')"); } else { instanceOf = false; System.out.println("\tFailure generic parameter is not a class"); } } } else { System.out.println("" + true + " 0 parameters"); } return instanceOf; } public List&lt;String&gt; method1() { return null; } public ArrayList&lt;String&gt; method2() { return new ArrayList&lt;String&gt;(); } public StringList method3() { return null; } public &lt;T extends StringList&gt; T method4() { return null; } </code></pre> <p>This outputs:</p> <pre> Testing method: javaapplication2.Main.method1 Return type test succesfull: true (expected 'java.util.List' found 'java.util.List') Number of generic parameters matches: true (expected 1, found 1) Generic parameter no. 1 matches: true (expected 'java.lang.String' found 'java.lang.String') Testing method: javaapplication2.Main.method2 Return type test succesfull: true (expected 'java.util.List' found 'java.util.ArrayList') Number of generic parameters matches: true (expected 1, found 1) Generic parameter no. 1 matches: true (expected 'java.lang.String' found 'java.lang.String') Testing method: javaapplication2.Main.method3 Return type test succesfull: false (expected 'java.util.List' found 'com.sun.org.apache.xerces.internal.xs.StringList') Number of generic parameters matches: true 0 parameters Testing method: javaapplication2.Main.method4 Return type test succesfull: true (expected 'com.sun.org.apache.xerces.internal.xs.StringList' found 'com.sun.org.apache.xerces.internal.xs.StringList') Number of generic parameters matches: true 0 parameters </pre>
 

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