Java 比较两个类,看看它们是否相等,考虑原语和它的包装器类

Java 比较两个类,看看它们是否相等,考虑原语和它的包装器类,java,reflection,equals,equality,Java,Reflection,Equals,Equality,最后,我想做一些类似的事情,我将使用它来搜索正确的构造函数进行反射 public static boolean equalWithPrimitive(Class<?> from, Class<?> target){ if(from == target){ return true; }else if((from == Byte.class || from == byte.class) && (target == Byte.cl

最后,我想做一些类似的事情,我将使用它来搜索正确的构造函数进行反射

public static boolean equalWithPrimitive(Class<?> from, Class<?> target){
    if(from == target){
        return true;
    }else if((from == Byte.class || from == byte.class) && (target == Byte.class || target == byte.class)){
        return true;
    }else if((from == Short.class || from == short.class) && (target == Short.class || target == short.class)){
        return true;
    }else if((from == Integer.class || from == int.class) && (target == Integer.class || target == int.class)){
        return true;
    }else if((from == Long.class || from == long.class) && (target == Long.class || target == long.class)){
        return true;
    }else if((from == Float.class || from == float.class) && (target == Float.class || target == float.class)){
        return true;
    }else if((from == Double.class || from == double.class) && (target == Double.class || target == double.class)){
        return true;
    }else if((from == Boolean.class || from == boolean.class) && (target == Boolean.class || target == boolean.class)){
        return true;
    }else if((from == Character.class || from == char.class) && (target == Character.class || target == char.class)){
        return true;
    }
    return false;
}
公共静态布尔equalWithPrimitive(类起始,类目标){
if(from==目标){
返回true;
}else if((from==Byte.class | | from==Byte.class)和&(target==Byte.class | | target==Byte.class)){
返回true;
}else if((from==Short.class | | from==Short.class)和&(target==Short.class | | target==Short.class)){
返回true;
}else if((from==Integer.class | | from==int.class)和&(target==Integer.class | | target==int.class)){
返回true;
}else if((from==Long.class | | from==Long.class)和&(target==Long.class | | target==Long.class)){
返回true;
}else if((from==Float.class | | from==Float.class)和&(target==Float.class | | target==Float.class)){
返回true;
}else if((from==Double.class | | from==Double.class)和&(target==Double.class | | target==Double.class)){
返回true;
}else if((from==Boolean.class | | from==Boolean.class)和&(target==Boolean.class | | target==Boolean.class)){
返回true;
}else if((from==Character.class | | from==char.class)和&(target==Character.class | | target==char.class)){
返回true;
}
返回false;
}

有没有一种更简短、更准确的方法来实现这个想法?

最简单的方法是保留一个primitive->boxed类型的映射,并在进行检查之前将其用于转换:

private static final Map<Class, Class> primitiveWrapperMap = new HashMap();
static {
     primitiveWrapperMap.put(Boolean.TYPE, Boolean.class);
     primitiveWrapperMap.put(Byte.TYPE, Byte.class);
     primitiveWrapperMap.put(Character.TYPE, Character.class);
     primitiveWrapperMap.put(Short.TYPE, Short.class);
     primitiveWrapperMap.put(Integer.TYPE, Integer.class);
     primitiveWrapperMap.put(Long.TYPE, Long.class);
     primitiveWrapperMap.put(Double.TYPE, Double.class);
     primitiveWrapperMap.put(Float.TYPE, Float.class);
     primitiveWrapperMap.put(Void.TYPE, Void.TYPE);
}

public static Class primitiveToWrapper(Class cls) {
    Class convertedClass = cls;
    if (cls != null && cls.isPrimitive()) {
        convertedClass = (Class) primitiveWrapperMap.get(cls);
    }
    return convertedClass;
}

public static boolean equalWithPrimitive(Class<?> from, Class<?> target) {
    return primitiveToWrapper(from) == primitiveToWrapper(to);
}
private static final Map primitiveWrapperMap=new HashMap();
静止的{
primitiveWrapperMap.put(Boolean.TYPE,Boolean.class);
primitiveWrapperMap.put(Byte.TYPE,Byte.class);
primitiveWrapperMap.put(Character.TYPE,Character.class);
primitiveWrapperMap.put(Short.TYPE,Short.class);
primitiveWrapperMap.put(Integer.TYPE,Integer.class);
primitiveWrapperMap.put(Long.TYPE,Long.class);
primitiveWrapperMap.put(Double.TYPE,Double.class);
primitiveWrapperMap.put(Float.TYPE,Float.class);
primitiveWrapperMap.put(Void.TYPE,Void.TYPE);
}
公共静态类原语说话者(类cls){
Class convertedClass=cls;
if(cls!=null&&cls.isPrimitive()){
convertedClass=(类)primitiveWrapperMap.get(cls);
}
返回转换类;
}
公共静态布尔equalWithPrimitive(类起始,类目标){
返回primitiveToWrapper(from)=primitiveToWrapper(to);
}

apachecommons库也是这样做的

可能有点脏,但是-

Get the name of the both classes convert those to lower case / upper case and equals them
前-

from.getName().toLowerCase().equals(target.getName().toLowerCase())

公共静态布尔equalWithPrimitive(类起始,类目标){
if(from==目标){
返回true;
}
从.getName().toLowerCase().equals(target.getName().toLowerCase())返回;
}

当一个参数是byte,另一个参数是byte时,是否要返回true?您可以将类放入数组中,并循环遍历它们。@TheLostMind yes,我希望这是真的,如果两个类是字节和字节,或者字节和字节,或者字节和字节,或者字节和字节byte@Quincunx重点是,我不必定义每一个存在的原语和它的类型,我想这是可行的,但是还有int&integer和char,也有char和Character,这可能是您需要特别处理的情况。重点是,我不必定义存在的每个基元及其类型。我会看看ApacheCommons,虽然我不认为Java提供了直接的方法。。。我认为这是最干净的方法。
public static boolean equalWithPrimitive(Class<?> from, Class<?> target){
    if(from == target){
        return true;
    }
    return from.getName().toLowerCase().equals(target.getName().toLowerCase());
}