I have a generic method that returns a boolean value in specific scenarios.
the method signature of my method is e.g.
public <T> T get() { return Boolean.true;}
Till here, it works fine with no compiler error. although, when I try to use the same in an if clause, like
if(get())
then it returns error saying
Type mismatch: cannot convert from Object to boolean
Also I doubt this is happening only with eclipse. this is an existing code, which I am trying to compile in my workspace.
Please help.
Update:
This article explains Target Type Inference well - https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html
and as explained below by @Codebender, TargetType inference works well for chained/nested methods, Although it looks like it will not work for language constructs like if, for, while etc. I tried to write a method below -
public void test(boolean val)
and tried calling it like
test(get())
and it works fine.
Solution:
Finally in this code, I ended up adding explicit typecast like
if((Boolean)get())
and it worked for me.