Java Class.isAssignableFrom 混淆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14151883/
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-10-31 15:15:11  来源:igfitidea点击:

Java Class.isAssignableFrom confusion

javareflection

提问by Korben

I find primitive type problem

我发现原始类型问题

System.out.println("Integer.class.isAssignableFrom(int.class) = " + Integer.class.isAssignableFrom(int.class));
System.out.println("int.class.isAssignableFrom(Integer.class) = "+int.class.isAssignableFrom(Integer.class));

both of the statements return false to the caller. so that seems like boxing is not applicable here. My question is if my observation is correct or there are other API who can do this testing correctly?

这两个语句都向调用者返回 false。所以这似乎拳击在这里不适用。我的问题是我的观察是否正确,或者是否有其他 API 可以正确进行此测试?

--------------------------------following up---------------------------------------------

--------------------------------关注---------------- -----------------------------

As I said, I mainly want to check if a Object is assignable to a Field when using reflection. I hope the mechanism could be more precise at run time so I made a implementation like this.

正如我所说,我主要想在使用反射时检查对象是否可以分配给字段。我希望这个机制在运行时可以更精确,所以我做了一个这样的实现。

    public static boolean isAssignableFrom(final Field field, final Object obj) {


        if (field.getType().equals(Integer.class) || field.getType().equals(int.class)) {
            return obj.getClass().equals(Integer.class) || field.getType().equals(int.class);
        } else if (field.getType().equals(Float.class) || field.getType().equals(float.class)) {
            return obj.getClass().equals(Float.class) || field.getType().equals(float.class);
        } else if (field.getType().equals(Double.class) || field.getType().equals(double.class)) {
            return obj.getClass().equals(Double.class) || field.getType().equals(double.class);
        } else if (field.getType().equals(Character.class) || field.getType().equals(char.class)) {
            return obj.getClass().equals(Character.class) || field.getType().equals(char.class);
        } else if (field.getType().equals(Long.class) || field.getType().equals(long.class)) {
            return obj.getClass().equals(Long.class) || field.getType().equals(long.class);
        } else if (field.getType().equals(Short.class) || field.getType().equals(short.class)) {
            return obj.getClass().equals(Short.class) || field.getType().equals(short.class);
        } else if (field.getType().equals(Boolean.class) || field.getType().equals(boolean.class)) {
            return obj.getClass().equals(Boolean.class) || field.getType().equals(boolean.class);
        } else if (field.getType().equals(Byte.class) || field.getType().equals(byte.class)) {
            return obj.getClass().equals(Byte.class) || field.getType().equals(byte.class);
        }
        return field.getType().isAssignableFrom(obj.getClass());
    }

}

That seems the best I can do -_-! thanks

这似乎是我能做的最好的-_-!谢谢

回答by Miro Hudak

I suppose, ClassUtils.isAssignable(Class, Class, boolean)from Apache commons-lang is the one to help.

我想,ClassUtils.isAssignable(Class, Class, boolean)来自 Apache commons-lang 是可以提供帮助的。

JavaDoc

Java文档

回答by Amit Deshpande

int.classand Integer.classare two separate class objects. check thisanswer for more details

int.class并且Integer.class是两个独立的类对象。检查this答案以获取更多详细信息

From Java doc Class#isAssignableFrom

来自 Java 文档 Class#isAssignableFrom

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.

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

回答by Krease

From the documentationon isAssignableFrom:

文档isAssignableFrom

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. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.

此方法测试是否可以通过标识转换或通过扩展引用转换将指定 Class 参数表示的类型转换为此 Class 对象表示的类型。有关详细信息,请参阅 Java 语言规范的第 5.1.1 和 5.1.4 节。

Integercannot be assigned to an int(or vice versa) through this way, so your method will return false - boxing and unboxing are done at compile time, not at runtime - see this articlefor more info on it

Integerint不能通过这种方式分配给 an (或反之亦然),因此您的方法将返回 false - 装箱和拆箱是在编译时完成的,而不是在运行时 -有关更多信息,请参阅本文