我沒有經驗,但它看起來像鍵入比java更弱。你的問題的第一部分非常簡單。
//this checks for null objects
public static boolean isEmpty(Object o) {
return o == null;
}
但是,在您的示例代碼中,您正在檢查具有特定名稱的特定字段。這在java中要複雜得多。您可以使用reflection,但您需要檢查兩種方法和字段。然後您將需要檢查適當的返回類型:Number,int,long,short,float,double。所以,這是可能的,但是需要一些工作,而且會很慢。
甲簡單,但更嚴格的機制將只檢查常見類型: (更新爲修正錯誤用枚舉和添加陣列;陣列具有被分解爲每個靈長類動物由於陣列中的java是如何工作的)
public static boolean isEmpty(Object o) {
if (o == null) {
return true;
}
else if (o instanceof Object[]) {
return ((Object[]) o).length <= 0;
}
else if (o instanceof boolean[]) {
return ((boolean[]) o).length <= 0;
}
else if (o instanceof byte[]) {
return ((byte[]) o).length <= 0;
}
else if (o instanceof short[]) {
return ((short[]) o).length <= 0;
}
else if (o instanceof char[]) {
return ((char[]) o).length <= 0;
}
else if (o instanceof int[]) {
return ((int[]) o).length <= 0;
}
else if (o instanceof long[]) {
return ((long[]) o).length <= 0;
}
else if (o instanceof float[]) {
return ((float[]) o).length <= 0;
}
else if (o instanceof double[]) {
return ((double[]) o).length <= 0;
}
else if (o instanceof CharSequence) {
return ((CharSequence) o).length() <= 0;
}
else if (o instanceof Collection) {
return ((Collection) o).isEmpty();
}
else if (o instanceof Map) {
return ((Map) o).isEmpty();
}
else if (o instanceof Enumeration) {
return !((Enumeration) o).hasMoreElements();
}
else if (o instanceof Dictionary) {
return ((Dictionary) o).isEmpty();
}
else if (o instanceof Iterable) {
// NOTE: may not be efficient because an iterator is created
return !((Iterable) o).iterator().hasNext();
}
return false;
}
更新:下面是以前的版本與反思檢查方法,代碼可以擴展到支持領域。處理退貨類型並不像我最初想象的那麼困難。即使有反思,它也會貼上自動裝箱作品。我也扔了一個檢查,看看對象是否有一個isEmpty()布爾方法。
public static boolean isEmpty(final Object o) {
if (o == null) {
return true;
}
else if (o instanceof Object[]) {
return ((Object[]) o).length <= 0;
}
else if (o instanceof boolean[]) {
return ((boolean[]) o).length <= 0;
}
else if (o instanceof byte[]) {
return ((byte[]) o).length <= 0;
}
else if (o instanceof short[]) {
return ((short[]) o).length <= 0;
}
else if (o instanceof char[]) {
return ((char[]) o).length <= 0;
}
else if (o instanceof int[]) {
return ((int[]) o).length <= 0;
}
else if (o instanceof long[]) {
return ((long[]) o).length <= 0;
}
else if (o instanceof float[]) {
return ((float[]) o).length <= 0;
}
else if (o instanceof double[]) {
return ((double[]) o).length <= 0;
}
else if (o instanceof CharSequence) {
return ((CharSequence) o).length() <= 0;
}
else if (o instanceof Collection) {
return ((Collection) o).isEmpty();
}
else if (o instanceof Map) {
return ((Map) o).isEmpty();
}
else if (o instanceof Enumeration) {
return !((Enumeration) o).hasMoreElements();
}
else if (o instanceof Dictionary) {
return ((Dictionary) o).isEmpty();
}
else if (o instanceof Iterable) {
// NOTE: may not be efficient because an iterator is created
return !((Iterable) o).iterator().hasNext();
}
// reflection code
final Number length = retrieveNumberFromMethod(o, "length");
if (length != null) {
return length.shortValue() <= 0;
}
final Number size = retrieveNumberFromMethod(o, "size");
if (size != null) {
return size.shortValue() <= 0;
}
final Boolean isEmpty = retrieveBooleanFromMethod(o, "isEmpty");
if (isEmpty != null) {
return isEmpty;
}
return false;
}
static Number retrieveNumberFromMethod(final Object o, final String methodName) {
try {
final Number number = (Number) o.getClass().getMethod(methodName).invoke(o);
return number;
}
catch (final IllegalArgumentException e) {
throw new IllegalStateException("Unable to retrieve number from " + methodName + " on " + o, e);
}
catch (final SecurityException e) {
throw new IllegalStateException("Unable to retrieve number from " + methodName + " on " + o, e);
}
catch (final InvocationTargetException e) {
throw new IllegalStateException("Unable to retrieve number from " + methodName + " on " + o, e);
}
catch (final IllegalAccessException e) {
return null;
}
catch (final NoSuchMethodException e) {
return null;
}
}
static Boolean retrieveBooleanFromMethod(final Object o, final String methodName) {
try {
final Boolean bool = (Boolean) o.getClass().getMethod(methodName).invoke(o);
return bool;
}
catch (final IllegalArgumentException e) {
throw new IllegalStateException("Unable to retrieve boolean from " + methodName + " on " + o, e);
}
catch (final SecurityException e) {
throw new IllegalStateException("Unable to retrieve boolean from " + methodName + " on " + o, e);
}
catch (final InvocationTargetException e) {
throw new IllegalStateException("Unable to retrieve boolean from " + methodName + " on " + o, e);
}
catch (final IllegalAccessException e) {
return null;
}
catch (final NoSuchMethodException e) {
return null;
}
}
你可能會。 Objective-C繼承了C的弱於弱的類型系統,然後在它上面打了一層鴨子。 Java的比較嚴格得多。 – CodaFi