Note that there are some explanatory texts on larger screens.

plurals
  1. POCompiler warning when casting to generic method type variable in Java
    primarykey
    data
    text
    <p><strong>Edit:</strong> I initially accepted thejh's answer, but I wasn't really satisfied with it since I wanted to make proper use of generics. So, I kept doing research and found a solution. Read about it in <a href="https://stackoverflow.com/questions/4117500/compiler-warning-when-casting-to-generic-method-type-variable-in-java/4153524#4153524">my answer</a> below.</p> <hr> <p>Here's a little self-contained piece of Java code which shows what I'm trying to do. It compiles, runs, and behaves correctly.</p> <pre><code> 1 import java.lang.reflect.Method; 2 import java.lang.reflect.InvocationTargetException; 3 4 public class Example 5 { 6 public static &lt;T&gt; void foo(Method method, String target, Object argument, T expectedReturn) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException 7 { 8 T actualReturn = (T) method.invoke(target, argument); 9 System.out.print(actualReturn.equals(expectedReturn)); 10 } 11 12 public static void main(String[ ] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException 13 { 14 foo(String.class.getMethod("charAt", int.class), "test", 1, 'e'); 15 } 16 } </code></pre> <p>Running this prints <code>true</code> to the console, which is what I expected. What's bothering me is that, due to the cast on line 8, I'm getting a warning when I compile it, as follows (jGRASP is my IDE, by the way).</p> <blockquote> <p>----jGRASP exec: javac -g -Xlint:unchecked Sandbox.java<br> Sandbox.java:8: warning: [unchecked] unchecked cast<br> found : java.lang.Object<br> required: T<br> 1 warning </p> <p>----jGRASP: operation complete.</p> </blockquote> <p>Originally, I tried line 8 without the cast, but that failed to compile with an error complaining about finding an <code>Object</code> when it required <code>T</code> (<code>invoke</code> returns an <code>Object</code>). Later on, I rewrote it like this, blindly hoping to get rid of the warning.</p> <pre><code>T actualReturn = method.getReturnType( ).cast(method.invoke(target, argument)); </code></pre> <p>But that gives a compile error that I can't make head nor tail of.</p> <blockquote> <p>----jGRASP exec: javac -g -Xlint:unchecked Sandbox.java<br> Sandbox.java:8: incompatible types<br> found : capture#898 of ?<br> required: T<br> 1 error </p> <p>----jGRASP wedge: exit code for process is 1.<br> ----jGRASP: operation complete.</p> </blockquote> <p>And that number next to <code>capture#</code> is different each time I try to compile with that same line of code.</p> <p>So, what exactly is the problem? Why am I getting the warning when I cast the object returned by invoke to the type variable? Does that indicate that I'm doing something wrong? How can I write this so that the warning goes away? And I'd prefer not to suppress it with an annotation, as that doesn't seem like much of a solution to me.</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