2014-01-20 55 views
3

最後,我想要做一些類似於這個的東西,我將使用它來搜索正確的構造函數進行反射。比較兩個類,並考慮它們是否相等考慮原始類和其包裝類

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; 
} 

有沒有一個簡短而準確的方法來實現這個想法?

+0

? – TheLostMind

+0

你可以把這些類放在數組中,並循環它們。 – Justin

+0

@TheLostMind是的,我希望它是真實的,如果兩個類是字節和字節或字節和字節或字節和字節或字節和字節 – user3213904

回答

2

最萬無一失的方法是在做檢查之前保持地圖primitive->盒裝型,並利用它來進行轉換:

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); 
} 

這也是Apache的百科全書ClassUtils庫是怎麼做的。

+0

這一點是這樣的,我不必定義每個單一的原始存在和它的類型。我會看看和Apache共享雖然 – user3213904

+0

我不認爲沒有直接的方式,Java爲此提供...我認爲這是最乾淨的方式來做到這一點。 –

1

這可能是有點髒,但 -

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

防爆 -

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

public static boolean equalWithPrimitive(Class<?> from, Class<?> target){ 
    if(from == target){ 
     return true; 
    } 
    return from.getName().toLowerCase().equals(target.getName().toLowerCase()); 
} 
你要當一個參數是字節,另一個字節返回true
+0

我認爲這會工作,但有int和整數與字符和字符也 – user3213904

+0

可能是你需要專門處理它的情況下 –