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.
我试图使用反射确定类的类类型,然后执行特定的操作。例如,如果类是double,则使用double特定方法。
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>"
“不兼容的操作数类型Class
What is the proper way to do this?
这样做的正确方法是什么?
Edit: to be more clear
编辑:更清楚
f is of type Field. obtained by reflection in a loop
f是Field类型。通过循环反射获得
(Field f : instance.getDeclaredFields())
5 个解决方案
#1
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.
有趣的错误消息(我不知道'=='运算符会检查那些)。但基于它,我怀疑你的比较是错误的:你试图看看Field类(理论上它的超类,但仅在理论上 - Field是final)是否与Double.class相同,它不能。
So: yes, comparison should work, iff you give it right arguments. So I suspect you want to do:
所以:是的,比较应该有效,如果你给它正确的论据。所以我怀疑你想做:
if (f.getType() == Double.class)
if(f.getType()== Double.class)
instead. And that should work, given that Double is a final class. Otherwise "isAssignableFrom" would be more appropriate.
代替。鉴于Double是最后一堂课,那应该有用。否则“isAssignableFrom”会更合适。
#2
If you have objects then use
如果你有对象然后使用
if (f instanceof Double) { }
Another interesting thing is method isAssignableFrom:
另一件有趣的事情是方法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.
回答评论:f instanceof Double工作正常。
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:
聪明的java编译器说你有CE。但:
public static boolean isInstanceOfDouble (Object obj) {
return obj instanceof Double;
}
psvm (String [] args) {
sout (isInstanceOfDouble (1.1f);
}
..this works fine
..这很好用
#3
You should be using getType() to get the underlying type, not get class. getType() returns the class of the underlying type as required.
您应该使用getType()来获取基础类型,而不是获取类。 getType()根据需要返回基础类型的类。
the code looks like this:
代码看起来像这样:
if(f.getType().equals(Double.class))
#4
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.class不能等于Double.class)。使用.equals()将其转换为警告而不是错误,但它仍然是错误的代码。
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.
Field.getType()为您提供字段的类型。由于在编译时无法知道这一点,因此如果您像最初一样使用==运算符,则不会出现错误。
#5
Following code snippet should work perfectly:
以下代码段应该完美地运行:
if (f.getType() == Double.TYPE)
BTW, neither ==
nor equals
works for Number types according to my test (JRE7)
BTW,根据我的测试(JRE7),==或equals都不适用于数字类型
#1
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.
有趣的错误消息(我不知道'=='运算符会检查那些)。但基于它,我怀疑你的比较是错误的:你试图看看Field类(理论上它的超类,但仅在理论上 - Field是final)是否与Double.class相同,它不能。
So: yes, comparison should work, iff you give it right arguments. So I suspect you want to do:
所以:是的,比较应该有效,如果你给它正确的论据。所以我怀疑你想做:
if (f.getType() == Double.class)
if(f.getType()== Double.class)
instead. And that should work, given that Double is a final class. Otherwise "isAssignableFrom" would be more appropriate.
代替。鉴于Double是最后一堂课,那应该有用。否则“isAssignableFrom”会更合适。
#2
If you have objects then use
如果你有对象然后使用
if (f instanceof Double) { }
Another interesting thing is method isAssignableFrom:
另一件有趣的事情是方法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.
回答评论:f instanceof Double工作正常。
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:
聪明的java编译器说你有CE。但:
public static boolean isInstanceOfDouble (Object obj) {
return obj instanceof Double;
}
psvm (String [] args) {
sout (isInstanceOfDouble (1.1f);
}
..this works fine
..这很好用
#3
You should be using getType() to get the underlying type, not get class. getType() returns the class of the underlying type as required.
您应该使用getType()来获取基础类型,而不是获取类。 getType()根据需要返回基础类型的类。
the code looks like this:
代码看起来像这样:
if(f.getType().equals(Double.class))
#4
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.class不能等于Double.class)。使用.equals()将其转换为警告而不是错误,但它仍然是错误的代码。
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.
Field.getType()为您提供字段的类型。由于在编译时无法知道这一点,因此如果您像最初一样使用==运算符,则不会出现错误。
#5
Following code snippet should work perfectly:
以下代码段应该完美地运行:
if (f.getType() == Double.TYPE)
BTW, neither ==
nor equals
works for Number types according to my test (JRE7)
BTW,根据我的测试(JRE7),==或equals都不适用于数字类型