views:

569

answers:

4

I am trying to determine the class type of a class using reflection and then do something specific. For example, if the class is a double, use a double specific method.

I am attempting to use


  if(f.getClass() == Double.class)

However, I am getting a compiler error:

"Incompatible operand types Class <capture#1-of ? extends Field> and Class<Double>"

What is the proper way to do this?

Edit: to be more clear

f is of type Field. obtained by reflection in a loop


  (Field f : instance.getDeclaredFields())
+1  A: 

The simplest way would be

if (f.getClass().equals(Double.class))

Edit: Ah, now I see the problem. The error you were getting was because (as a result of generics) the compiler could tell that the classes would never be equal (Field.class cannot equal Double.class). Using .equals() turns it into a warning instead of an error, but it's still wrong code.

Field.getType() gives you the type of the field. Since this cannot be known at compile time, you will not get an error if you use the == operator as you did originally.

Michael Myers
This does not work the way I would like. Thanks for the help, I stumbled across the answer and have posted it.
kgrad
A: 

If you have objects then use

if (f instanceof Double) { }

Another interesting thing is method isAssignableFrom:

if (f.getClass().isAssignableFrom (Double.class)) { }

But in general it's a bad style. Use polymorphism to implement logic which depends on class types.


Answer for comment: f instanceof Double works fine.

You probably wrote something like this:

float f = 1.1f;
if (f instanceof Double) { ..}

And smart java compiler says that you've got CE. BUT:

public static boolean isInstanceOfDouble (Object obj) {
   return obj instanceof Double;
}

psvm (String [] args) {
   sout (isInstanceOfDouble (1.1f);
}

..this works fine

Roman
That's not always the same thing. If Double weren't a final class, the two would be different if f happened to be a subclass.
Michael Myers
f instanceof Double gives a compiler error.
kgrad
Yes, as you can see I wrote "interesting", it means "read javadocs for more details".Anyway I prefer OOP solutions
Roman
Heh, +1 for the 'psvm' and 'sout' live templates. If only someone would build a textbox control with code completion...
Outlaw Programmer
You isInstanceOfDouble() example works fine because of auto-boxing and is highly contrived. Why would I ever want to see if a primitive type is an instance of any object, when it is by definition not? (It's wrapper might be but that's not the same).
Software Monkey
A: 

You should be using getType() to get the underlying type, not get class. getType() returns the class of the underlying type as required.

the code looks like this:


  if(f.getType().equals(Double.class))
kgrad
That's the answer to a completely different question. getType() is specific to the Field class, while getClass() applies to all objects.
Michael Myers
The question is specific to reflection...
kgrad
But did you notice that both Roman and I gave answers that didn't make sense? We were both answering your original question. (I've updated my answer to explain the real issue here.)
Michael Myers
I admit the first question probably wasn't as clear as it should have been, however I clearly mentioned reflection both in the question and in my post explaining the question. Additionally, I don't see why my answer is downvoted as it clearly answers my question...
kgrad
+1  A: 

Interesting error message (I didn't know '==' operator would check those). But based on it, I suspect that your comparison is wrong: you are trying to see if Field class (theoretically its super-class, but only in theory -- Field is final) is same as Double.class, which it can not be.

So: yes, comparison should work, iff you give it right arguments. So I suspect you want to do:

if (f.getType() == Double.class)

instead. And that should work, given that Double is a final class. Otherwise "isAssignableFrom" would be more appropriate.

StaxMan