Java 如何通过反射确定字段是否是类型的实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2262322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:24:43  来源:igfitidea点击:

Howto find out if a field is instanceof a type via reflection?

javareflection

提问by c0d3x

I want to find out via reflection if a field is an instance of some type T.

我想通过反射找出字段是否是某种类型的实例T

Lets say I have an object o. Now I want to know if it has any fields which are instance of T. I can get all fields with:

假设我有一个对象o。现在我想知道它是否有任何字段是T. 我可以获得所有字段:

o.getClass().getFields();

I can get the type of the field with:

我可以通过以下方式获取字段的类型:

field.getType();

But now I want to know if this type or any supertype equals T. Do I have to call getSuperclass()recursively to be sure to check all supertypes?

但现在我想知道这种类型或任何超类型是否等于T. 我是否必须getSuperclass()递归调用以确保检查所有超类型?

采纳答案by fastcodejava

You have to use isAssignableFrom.

您必须使用isAssignableFrom

回答by skaffman

The rather baroquely-named Class.isAssignableFromis what you're after. I usually end up having to read the javadoc to make sure I get it the right way around:

相当巴洛克式的名字Class.isAssignableFrom就是你所追求的。我通常最终不得不阅读 javadoc 以确保我以正确的方式得到它:

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.

Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion.

确定此 Class 对象表示的类或接口是否与指定的 Class 参数表示的类或接口相同,或者是其超类或超接口。如果是,则返回 true;否则返回false。如果此 Class 对象表示原始类型,则如果指定的 Class 参数正是此 Class 对象,则此方法返回 true;否则返回false。

具体来说,此方法测试是否可以通过标识转换或通过扩展引用转换将指定 Class 参数表示的类型转换为此 Class 对象表示的类型。

For example:

例如:

public class X {

   public int i;

   public static void main(String[] args) throws Exception {
      Class<?> myType = Integer.TYPE;
      Object o = new X();

      for (Field field : o.getClass().getFields()) {
         if (field.getType().isAssignableFrom(myType)) {
            System.out.println("Field " + field + " is assignable from type " + o.getClass());
         }
      }
   }
}

回答by Steven Lizarazo

If you want to compare the field type of a custom class you should try this, use .class because only primitive types has .TYPE.

如果你想比较自定义类的字段类型,你应该试试这个,使用 .class 因为只有原始类型有 .TYPE。

if(field.getType().isAssignableFrom(**YOURCLASS.class**)){}

if(field.getType().isAssignableFrom(**YOURCLASS.class**)){}